Jack Occleshaw
/
w4-spidatalinkmaster
w4spidatalinkmaster
main.cpp@0:c912ef4f5206, 2017-01-12 (annotated)
- Committer:
- occle
- Date:
- Thu Jan 12 15:45:25 2017 +0000
- Revision:
- 0:c912ef4f5206
w4spidatalinkmaster
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
occle | 0:c912ef4f5206 | 1 | /*Sets the mbed up as Master, and exchanges data with a slave, sending its own switch positions, and displaying those of the slave.*/ |
occle | 0:c912ef4f5206 | 2 | #include "mbed.h" |
occle | 0:c912ef4f5206 | 3 | SPI ser_port(PTD2, PTD3, PTD1); //mosi, miso, sclk |
occle | 0:c912ef4f5206 | 4 | DigitalOut red_led(LED1); //red led |
occle | 0:c912ef4f5206 | 5 | DigitalOut green_led(LED2); //green led |
occle | 0:c912ef4f5206 | 6 | DigitalOut cs(PTC16); //this acts as “slave select” |
occle | 0:c912ef4f5206 | 7 | DigitalIn switch_ip1(SW2); |
occle | 0:c912ef4f5206 | 8 | DigitalIn switch_ip2(SW3); |
occle | 0:c912ef4f5206 | 9 | char switch_word ; //word we will send |
occle | 0:c912ef4f5206 | 10 | char recd_val; //value return from slave |
occle | 0:c912ef4f5206 | 11 | |
occle | 0:c912ef4f5206 | 12 | int main() { |
occle | 0:c912ef4f5206 | 13 | while (1){ |
occle | 0:c912ef4f5206 | 14 | //Default settings for SPI Master chosen, no need for further configuration |
occle | 0:c912ef4f5206 | 15 | //Set up the word to be sent, by testing switch inputs |
occle | 0:c912ef4f5206 | 16 | switch_word=0xa0; //set up a recognizable output pattern |
occle | 0:c912ef4f5206 | 17 | if (switch_ip1==0) |
occle | 0:c912ef4f5206 | 18 | switch_word=switch_word|0x01; //OR in lsb, check if the botton is pressed |
occle | 0:c912ef4f5206 | 19 | if (switch_ip2==0) |
occle | 0:c912ef4f5206 | 20 | switch_word=switch_word|0x02; //OR in next lsb, check if the botton is pressed |
occle | 0:c912ef4f5206 | 21 | cs = 0; //select slave ("active low") |
occle | 0:c912ef4f5206 | 22 | recd_val=ser_port.write(switch_word); //send switch_word and receive data. set a initial value for recd_val (dummy) |
occle | 0:c912ef4f5206 | 23 | cs = 1; |
occle | 0:c912ef4f5206 | 24 | wait(0.01);//wait for slave to response |
occle | 0:c912ef4f5206 | 25 | //set leds according to incoming word from slave |
occle | 0:c912ef4f5206 | 26 | red_led=1; //preset both to off |
occle | 0:c912ef4f5206 | 27 | green_led=1; |
occle | 0:c912ef4f5206 | 28 | recd_val=recd_val&0x03; //AND out unwanted bits |
occle | 0:c912ef4f5206 | 29 | if (recd_val==1) |
occle | 0:c912ef4f5206 | 30 | {red_led=0; //red on, green off |
occle | 0:c912ef4f5206 | 31 | green_led=1;} |
occle | 0:c912ef4f5206 | 32 | if (recd_val==2) |
occle | 0:c912ef4f5206 | 33 | {red_led=1; //red off, green on |
occle | 0:c912ef4f5206 | 34 | green_led=0;} |
occle | 0:c912ef4f5206 | 35 | if (recd_val==3){ |
occle | 0:c912ef4f5206 | 36 | red_led=0; //both on |
occle | 0:c912ef4f5206 | 37 | green_led=0; |
occle | 0:c912ef4f5206 | 38 | } |
occle | 0:c912ef4f5206 | 39 | } |
occle | 0:c912ef4f5206 | 40 | } |