by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
main.cpp@0:75151492e147, 2012-10-15 (annotated)
- Committer:
- robt
- Date:
- Mon Oct 15 21:27:29 2012 +0000
- Revision:
- 0:75151492e147
by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robt | 0:75151492e147 | 1 | /*Program Example 7.9: Sets the mbed up for async communication, and exchanges data with a similar node, sending its own switch positions, and displaying those of the other. |
robt | 0:75151492e147 | 2 | */ |
robt | 0:75151492e147 | 3 | #include "mbed.h" |
robt | 0:75151492e147 | 4 | Serial async_port(p9, p10); //set up TX and RX on pins 9 and 10 |
robt | 0:75151492e147 | 5 | DigitalOut red_led(p25); //red led |
robt | 0:75151492e147 | 6 | DigitalOut green_led(p26); //green led |
robt | 0:75151492e147 | 7 | DigitalOut strobe(p7); //a strobe to trigger the scope |
robt | 0:75151492e147 | 8 | DigitalIn switch_ip1(p5); |
robt | 0:75151492e147 | 9 | DigitalIn switch_ip2(p6); |
robt | 0:75151492e147 | 10 | char switch_word ; //the word we will send |
robt | 0:75151492e147 | 11 | char recd_val; //the received value |
robt | 0:75151492e147 | 12 | |
robt | 0:75151492e147 | 13 | int main() { |
robt | 0:75151492e147 | 14 | async_port.baud(9600); //set baud rate to 9600 (ie default) |
robt | 0:75151492e147 | 15 | //accept default format, of 8 bits, no parity |
robt | 0:75151492e147 | 16 | while (1){ |
robt | 0:75151492e147 | 17 | //Set up the word to be sent, by testing switch inputs |
robt | 0:75151492e147 | 18 | switch_word=0xa0; //set up a recognisable output pattern |
robt | 0:75151492e147 | 19 | if (switch_ip1==1) |
robt | 0:75151492e147 | 20 | switch_word=switch_word|0x01; //OR in lsb |
robt | 0:75151492e147 | 21 | if (switch_ip2==1) |
robt | 0:75151492e147 | 22 | switch_word=switch_word|0x02; //OR in next lsb |
robt | 0:75151492e147 | 23 | strobe =1; //short strobe pulse |
robt | 0:75151492e147 | 24 | wait_us(10); |
robt | 0:75151492e147 | 25 | strobe=0; |
robt | 0:75151492e147 | 26 | async_port.putc(switch_word); //transmit switch_word |
robt | 0:75151492e147 | 27 | if (async_port.readable()==1) //is there a character to be read? |
robt | 0:75151492e147 | 28 | recd_val=async_port.getc(); //if yes, then read it |
robt | 0:75151492e147 | 29 | |
robt | 0:75151492e147 | 30 | //set leds according to incoming word from slave |
robt | 0:75151492e147 | 31 | red_led=0; //preset both to 0 |
robt | 0:75151492e147 | 32 | green_led=0; |
robt | 0:75151492e147 | 33 | recd_val=recd_val&0x03; //AND out unwanted bits |
robt | 0:75151492e147 | 34 | if (recd_val==1) |
robt | 0:75151492e147 | 35 | red_led=1; |
robt | 0:75151492e147 | 36 | if (recd_val==2) |
robt | 0:75151492e147 | 37 | green_led=1; |
robt | 0:75151492e147 | 38 | if (recd_val==3){ |
robt | 0:75151492e147 | 39 | red_led=1; |
robt | 0:75151492e147 | 40 | green_led=1; |
robt | 0:75151492e147 | 41 | } |
robt | 0:75151492e147 | 42 | |
robt | 0:75151492e147 | 43 | } |
robt | 0:75151492e147 | 44 | } |
robt | 0:75151492e147 | 45 |