Rob Toulson / Mbed 2 deprecated PE_07-03_SPISlave

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*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.                                                                          
00002                                                                              */
00003 #include "mbed.h"
00004 SPISlave ser_port(p11,p12,p13,p14); // mosi, miso, sclk, ssel
00005 DigitalOut red_led(p25);            //red led
00006 DigitalOut green_led(p26);          //green led
00007 DigitalIn  switch_ip1(p5);
00008 DigitalIn  switch_ip2(p6);
00009 char switch_word ;                  //word we will send
00010 char recd_val;                      //value received from master
00011 
00012 int main() {
00013   //default formatting applied
00014   while(1) {
00015     //set up switch_word from switches that are pressed 
00016     switch_word=0xa0;             //set up a recognisable output pattern
00017     if (switch_ip1==1)
00018       switch_word=switch_word|0x01;
00019     if (switch_ip2==1)
00020       switch_word=switch_word|0x02;
00021     
00022     if(ser_port.receive()) {      //test if data transfer has occurred
00023       recd_val = ser_port.read();   // Read byte from master
00024       ser_port.reply(switch_word);  // Make this the next reply
00025     } 
00026     
00027     //set leds according to incoming word from slave
00028     red_led=0;              //preset both to 0
00029     green_led=0; 
00030     recd_val=recd_val&0x03; //AND out unwanted bits
00031     if (recd_val==1)
00032       red_led=1;
00033     if (recd_val==2)
00034       green_led=1;
00035     if (recd_val==3){
00036       red_led=1;
00037       green_led=1;
00038     }
00039   }
00040 }
00041