w4spidatalinkslave

Dependencies:   mbed

Committer:
occle
Date:
Thu Jan 12 15:45:44 2017 +0000
Revision:
0:14d431c4b518
w4spidatalinkslave

Who changed what in which revision?

UserRevisionLine numberNew contents of line
occle 0:14d431c4b518 1 /* Sets the mbed up as Slave, and exchanges data with a Master,
occle 0:14d431c4b518 2 sending its own switch positions, and displaying those of the Master. as SPI slave.*/
occle 0:14d431c4b518 3 #include "mbed.h"
occle 0:14d431c4b518 4 SPISlave ser_port(PTD2,PTD3,PTD1,PTC16); // mosi, miso, sclk, ssel
occle 0:14d431c4b518 5 DigitalOut red_led(LED1); //red led
occle 0:14d431c4b518 6 DigitalOut green_led(LED2); //green led
occle 0:14d431c4b518 7 DigitalIn switch_ip1(SW2);
occle 0:14d431c4b518 8 DigitalIn switch_ip2(SW3);
occle 0:14d431c4b518 9 char switch_word ; //word we will send
occle 0:14d431c4b518 10 char recd_val; //value received from master
occle 0:14d431c4b518 11
occle 0:14d431c4b518 12 int main() {
occle 0:14d431c4b518 13 recd_val=0x00;
occle 0:14d431c4b518 14 //default formatting applied
occle 0:14d431c4b518 15 while(1) {
occle 0:14d431c4b518 16 //set up switch_word from switches that are pressed
occle 0:14d431c4b518 17 switch_word=0xa0; //set up a recognizable output pattern
occle 0:14d431c4b518 18 if (switch_ip1==0)
occle 0:14d431c4b518 19 switch_word=switch_word|0x01;
occle 0:14d431c4b518 20 if (switch_ip2==0)
occle 0:14d431c4b518 21 switch_word=switch_word|0x02;
occle 0:14d431c4b518 22 if(ser_port.receive()) { //test if data transfer has occurred
occle 0:14d431c4b518 23 recd_val = ser_port.read(); // Read byte from master
occle 0:14d431c4b518 24 ser_port.reply(switch_word); // Make this the next reply
occle 0:14d431c4b518 25 }
occle 0:14d431c4b518 26 //now set leds according to received word.
occle 0:14d431c4b518 27 red_led=1; //preset both to off
occle 0:14d431c4b518 28 green_led=1;
occle 0:14d431c4b518 29 recd_val=recd_val&0x03; //AND out unwanted bits
occle 0:14d431c4b518 30 if (recd_val==1)
occle 0:14d431c4b518 31 {red_led=0; //red on, green off
occle 0:14d431c4b518 32 green_led=1;}
occle 0:14d431c4b518 33 if (recd_val==2)
occle 0:14d431c4b518 34 {red_led=1; //red off, green on
occle 0:14d431c4b518 35 green_led=0;}
occle 0:14d431c4b518 36 if (recd_val==3){
occle 0:14d431c4b518 37 red_led=0; //both on
occle 0:14d431c4b518 38 green_led=0;
occle 0:14d431c4b518 39 }
occle 0:14d431c4b518 40 }
occle 0:14d431c4b518 41 }