by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
main.cpp@0:c3cbfdffbac1, 2012-10-15 (annotated)
- Committer:
- robt
- Date:
- Mon Oct 15 21:25:05 2012 +0000
- Revision:
- 0:c3cbfdffbac1
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:c3cbfdffbac1 | 1 | /*Program Example 7.3: Sets the mbed up as Slave, and exchanges data with a Master, sending its own switch positions, and displaying those of the Master. as SPI slave. |
robt | 0:c3cbfdffbac1 | 2 | */ |
robt | 0:c3cbfdffbac1 | 3 | #include "mbed.h" |
robt | 0:c3cbfdffbac1 | 4 | SPISlave ser_port(p11,p12,p13,p14); // mosi, miso, sclk, ssel |
robt | 0:c3cbfdffbac1 | 5 | DigitalOut red_led(p25); //red led |
robt | 0:c3cbfdffbac1 | 6 | DigitalOut green_led(p26); //green led |
robt | 0:c3cbfdffbac1 | 7 | DigitalIn switch_ip1(p5); |
robt | 0:c3cbfdffbac1 | 8 | DigitalIn switch_ip2(p6); |
robt | 0:c3cbfdffbac1 | 9 | char switch_word ; //word we will send |
robt | 0:c3cbfdffbac1 | 10 | char recd_val; //value received from master |
robt | 0:c3cbfdffbac1 | 11 | |
robt | 0:c3cbfdffbac1 | 12 | int main() { |
robt | 0:c3cbfdffbac1 | 13 | //default formatting applied |
robt | 0:c3cbfdffbac1 | 14 | while(1) { |
robt | 0:c3cbfdffbac1 | 15 | //set up switch_word from switches that are pressed |
robt | 0:c3cbfdffbac1 | 16 | switch_word=0xa0; //set up a recognisable output pattern |
robt | 0:c3cbfdffbac1 | 17 | if (switch_ip1==1) |
robt | 0:c3cbfdffbac1 | 18 | switch_word=switch_word|0x01; |
robt | 0:c3cbfdffbac1 | 19 | if (switch_ip2==1) |
robt | 0:c3cbfdffbac1 | 20 | switch_word=switch_word|0x02; |
robt | 0:c3cbfdffbac1 | 21 | |
robt | 0:c3cbfdffbac1 | 22 | if(ser_port.receive()) { //test if data transfer has occurred |
robt | 0:c3cbfdffbac1 | 23 | recd_val = ser_port.read(); // Read byte from master |
robt | 0:c3cbfdffbac1 | 24 | ser_port.reply(switch_word); // Make this the next reply |
robt | 0:c3cbfdffbac1 | 25 | } |
robt | 0:c3cbfdffbac1 | 26 | |
robt | 0:c3cbfdffbac1 | 27 | //set leds according to incoming word from slave |
robt | 0:c3cbfdffbac1 | 28 | red_led=0; //preset both to 0 |
robt | 0:c3cbfdffbac1 | 29 | green_led=0; |
robt | 0:c3cbfdffbac1 | 30 | recd_val=recd_val&0x03; //AND out unwanted bits |
robt | 0:c3cbfdffbac1 | 31 | if (recd_val==1) |
robt | 0:c3cbfdffbac1 | 32 | red_led=1; |
robt | 0:c3cbfdffbac1 | 33 | if (recd_val==2) |
robt | 0:c3cbfdffbac1 | 34 | green_led=1; |
robt | 0:c3cbfdffbac1 | 35 | if (recd_val==3){ |
robt | 0:c3cbfdffbac1 | 36 | red_led=1; |
robt | 0:c3cbfdffbac1 | 37 | green_led=1; |
robt | 0:c3cbfdffbac1 | 38 | } |
robt | 0:c3cbfdffbac1 | 39 | } |
robt | 0:c3cbfdffbac1 | 40 | } |
robt | 0:c3cbfdffbac1 | 41 |