lab2_SPI

Dependencies:   mbed

main.cpp

Committer:
micheleperotti
Date:
2013-12-10
Revision:
0:f7cd39ae51f7

File content as of revision 0:f7cd39ae51f7:

#include "mbed.h"



DigitalOut redLed(LED_RED);
DigitalOut spiSS(PTD0);

int main() {

   SPI spiMaster(PTD2, PTD3, PTD1);
   spiMaster.format(8,1);
   spiMaster.frequency(100000);
   spiSS=1;
   
   while(1){
   //read switch
   spiSS=0;
   spiMaster.write('#');
   spiMaster.write(0x01);
   int switchVal = spiMaster.write(0x55);
   spiSS=1;
   wait(0.0001);
   //write LED
   spiSS=0;
   spiMaster.write('#');
   spiMaster.write(0x02);
   spiMaster.write(switchVal);
   spiSS=1;
   wait(0.0001);
   //read LED
   spiSS=0;
   spiMaster.write('#');
   spiMaster.write(0x03);
   int LEDVal = spiMaster.write(0x55);
   spiSS=1;
   wait(0.0001);  
   //read switch another tima
   spiSS=0;
   spiMaster.write('#');
   spiMaster.write(0x01);
   switchVal = spiMaster.write(0x55);
   spiSS=1;
   wait(0.0001);
   //verify correctness of SPI behaviour 
   if (LEDVal!=switchVal)
   {
     redLed=0;
   } else {
    redLed=1;
        }
   wait (0.02);
   }
}