Gerardo CR
/
Inst_SPI-Master
SPI-Master
main.cpp@0:795863b0916e, 2014-09-26 (annotated)
- Committer:
- gerardo_carmona
- Date:
- Fri Sep 26 00:04:44 2014 +0000
- Revision:
- 0:795863b0916e
SPI-Master
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gerardo_carmona | 0:795863b0916e | 1 | /*Program Example 7.2. Sets the mbed up as Master, and exchanges data with a |
gerardo_carmona | 0:795863b0916e | 2 | slave, sending its own switch positions, and displaying those of the slave. |
gerardo_carmona | 0:795863b0916e | 3 | */ |
gerardo_carmona | 0:795863b0916e | 4 | |
gerardo_carmona | 0:795863b0916e | 5 | #include "mbed.h" |
gerardo_carmona | 0:795863b0916e | 6 | |
gerardo_carmona | 0:795863b0916e | 7 | SPI ser_port(D11, D12, D13); // mosi, miso, sclk |
gerardo_carmona | 0:795863b0916e | 8 | DigitalOut red_led(LED_RED); //red led |
gerardo_carmona | 0:795863b0916e | 9 | DigitalOut green_led(LED_GREEN); //green led |
gerardo_carmona | 0:795863b0916e | 10 | DigitalOut cs(D2); //this acts as “slave select” |
gerardo_carmona | 0:795863b0916e | 11 | DigitalIn switch_ip1(SW2); |
gerardo_carmona | 0:795863b0916e | 12 | DigitalIn switch_ip2(SW3); |
gerardo_carmona | 0:795863b0916e | 13 | |
gerardo_carmona | 0:795863b0916e | 14 | char switch_word ; //word we will send |
gerardo_carmona | 0:795863b0916e | 15 | char recd_val; //value return from slave |
gerardo_carmona | 0:795863b0916e | 16 | |
gerardo_carmona | 0:795863b0916e | 17 | //continued over |
gerardo_carmona | 0:795863b0916e | 18 | |
gerardo_carmona | 0:795863b0916e | 19 | int main() { |
gerardo_carmona | 0:795863b0916e | 20 | switch_ip1.mode(PullUp); |
gerardo_carmona | 0:795863b0916e | 21 | switch_ip2.mode(PullUp); |
gerardo_carmona | 0:795863b0916e | 22 | red_led = 1; |
gerardo_carmona | 0:795863b0916e | 23 | green_led=1; |
gerardo_carmona | 0:795863b0916e | 24 | while (1){ |
gerardo_carmona | 0:795863b0916e | 25 | //Default settings for SPI Master chosen, no need for further configuration |
gerardo_carmona | 0:795863b0916e | 26 | //Set up the word to be sent, by testing switch inputs |
gerardo_carmona | 0:795863b0916e | 27 | switch_word=0xa0; //set up a recognisable output pattern |
gerardo_carmona | 0:795863b0916e | 28 | if (switch_ip1==0) |
gerardo_carmona | 0:795863b0916e | 29 | switch_word=switch_word|0x01; //OR in lsb |
gerardo_carmona | 0:795863b0916e | 30 | if (switch_ip2==0) |
gerardo_carmona | 0:795863b0916e | 31 | switch_word=switch_word|0x02; //OR in next lsb |
gerardo_carmona | 0:795863b0916e | 32 | cs = 0; //select slave |
gerardo_carmona | 0:795863b0916e | 33 | recd_val=ser_port.write(switch_word); //send switch_word and receive data |
gerardo_carmona | 0:795863b0916e | 34 | cs = 1; |
gerardo_carmona | 0:795863b0916e | 35 | wait(0.01); |
gerardo_carmona | 0:795863b0916e | 36 | //set leds according to incoming word from slave |
gerardo_carmona | 0:795863b0916e | 37 | red_led=1; //preset both to 0 |
gerardo_carmona | 0:795863b0916e | 38 | green_led=1; |
gerardo_carmona | 0:795863b0916e | 39 | recd_val=recd_val&0x03; //AND out unwanted bits |
gerardo_carmona | 0:795863b0916e | 40 | |
gerardo_carmona | 0:795863b0916e | 41 | if (recd_val==0x1) |
gerardo_carmona | 0:795863b0916e | 42 | red_led=0; |
gerardo_carmona | 0:795863b0916e | 43 | |
gerardo_carmona | 0:795863b0916e | 44 | if (recd_val==0x2) |
gerardo_carmona | 0:795863b0916e | 45 | green_led=0; |
gerardo_carmona | 0:795863b0916e | 46 | |
gerardo_carmona | 0:795863b0916e | 47 | if (recd_val==0x3){ |
gerardo_carmona | 0:795863b0916e | 48 | red_led=0; |
gerardo_carmona | 0:795863b0916e | 49 | green_led=0; |
gerardo_carmona | 0:795863b0916e | 50 | } |
gerardo_carmona | 0:795863b0916e | 51 | |
gerardo_carmona | 0:795863b0916e | 52 | } |
gerardo_carmona | 0:795863b0916e | 53 | } |