Jack Occleshaw / Mbed 2 deprecated w4-spidatalinkmaster

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*Sets the mbed up as Master, and exchanges data with a slave, sending its own switch positions, and displaying those of the slave.*/
00002 #include "mbed.h"
00003 SPI ser_port(PTD2, PTD3, PTD1); //mosi, miso, sclk
00004 DigitalOut red_led(LED1); //red led
00005 DigitalOut green_led(LED2); //green led
00006 DigitalOut cs(PTC16); //this acts as “slave select”
00007 DigitalIn switch_ip1(SW2);
00008 DigitalIn switch_ip2(SW3);
00009 char switch_word ; //word we will send
00010 char recd_val; //value return from slave
00011 
00012 int main() {
00013    while (1){
00014 //Default settings for SPI Master chosen, no need for further configuration
00015 //Set up the word to be sent, by testing switch inputs
00016    switch_word=0xa0; //set up a recognizable output pattern
00017      if (switch_ip1==0)
00018      switch_word=switch_word|0x01; //OR in lsb, check if the botton is pressed
00019      if (switch_ip2==0)
00020      switch_word=switch_word|0x02; //OR in next lsb, check if the botton is pressed
00021      cs = 0; //select slave ("active low")
00022      recd_val=ser_port.write(switch_word); //send switch_word and receive data. set a initial value for recd_val (dummy)
00023      cs = 1;
00024      wait(0.01);//wait for slave to response
00025 //set leds according to incoming word from slave
00026      red_led=1; //preset both to off
00027      green_led=1;
00028      recd_val=recd_val&0x03; //AND out unwanted bits
00029      if (recd_val==1)
00030      {red_led=0; //red on, green off
00031       green_led=1;}
00032      if (recd_val==2)
00033      {red_led=1; //red off, green on
00034       green_led=0;}
00035      if (recd_val==3){
00036       red_led=0; //both on
00037       green_led=0;
00038    }
00039  }
00040 }