Rob Toulson / Mbed 2 deprecated PE_07-09_AsyncComms

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*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. 
00002                                                                  */
00003 #include "mbed.h"
00004 Serial async_port(p9, p10);          //set up TX and RX on pins 9 and 10
00005 DigitalOut red_led(p25);             //red led
00006 DigitalOut green_led(p26);           //green led
00007 DigitalOut strobe(p7);               //a strobe to trigger the scope
00008 DigitalIn switch_ip1(p5);
00009 DigitalIn switch_ip2(p6);
00010 char switch_word ;                   //the word we will send
00011 char recd_val;                       //the received value 
00012   
00013 int main() {
00014   async_port.baud(9600);             //set baud rate to 9600 (ie default)
00015   //accept default format, of 8 bits, no parity
00016   while (1){
00017     //Set up the word to be sent, by testing switch inputs
00018     switch_word=0xa0;                 //set up a recognisable output pattern
00019     if (switch_ip1==1)
00020       switch_word=switch_word|0x01;   //OR in lsb
00021     if (switch_ip2==1)
00022       switch_word=switch_word|0x02;   //OR in next lsb
00023     strobe =1;                        //short strobe pulse 
00024     wait_us(10);
00025     strobe=0;
00026     async_port.putc(switch_word);     //transmit switch_word
00027     if (async_port.readable()==1)     //is there a character to be read?
00028       recd_val=async_port.getc();     //if yes, then read it
00029 
00030     //set leds according to incoming word from slave
00031     red_led=0;              //preset both to 0
00032     green_led=0; 
00033     recd_val=recd_val&0x03; //AND out unwanted bits
00034     if (recd_val==1)
00035       red_led=1;
00036     if (recd_val==2)
00037       green_led=1;
00038     if (recd_val==3){
00039       red_led=1;
00040       green_led=1;
00041     }
00042 
00043   }
00044 }
00045