Michele Perotti
/
lab2_SPI
lab2_SPI
main.cpp@0:f7cd39ae51f7, 2013-12-10 (annotated)
- Committer:
- micheleperotti
- Date:
- Tue Dec 10 19:52:40 2013 +0000
- Revision:
- 0:f7cd39ae51f7
lab2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
micheleperotti | 0:f7cd39ae51f7 | 1 | #include "mbed.h" |
micheleperotti | 0:f7cd39ae51f7 | 2 | |
micheleperotti | 0:f7cd39ae51f7 | 3 | |
micheleperotti | 0:f7cd39ae51f7 | 4 | |
micheleperotti | 0:f7cd39ae51f7 | 5 | DigitalOut redLed(LED_RED); |
micheleperotti | 0:f7cd39ae51f7 | 6 | DigitalOut spiSS(PTD0); |
micheleperotti | 0:f7cd39ae51f7 | 7 | |
micheleperotti | 0:f7cd39ae51f7 | 8 | int main() { |
micheleperotti | 0:f7cd39ae51f7 | 9 | |
micheleperotti | 0:f7cd39ae51f7 | 10 | SPI spiMaster(PTD2, PTD3, PTD1); |
micheleperotti | 0:f7cd39ae51f7 | 11 | spiMaster.format(8,1); |
micheleperotti | 0:f7cd39ae51f7 | 12 | spiMaster.frequency(100000); |
micheleperotti | 0:f7cd39ae51f7 | 13 | spiSS=1; |
micheleperotti | 0:f7cd39ae51f7 | 14 | |
micheleperotti | 0:f7cd39ae51f7 | 15 | while(1){ |
micheleperotti | 0:f7cd39ae51f7 | 16 | //read switch |
micheleperotti | 0:f7cd39ae51f7 | 17 | spiSS=0; |
micheleperotti | 0:f7cd39ae51f7 | 18 | spiMaster.write('#'); |
micheleperotti | 0:f7cd39ae51f7 | 19 | spiMaster.write(0x01); |
micheleperotti | 0:f7cd39ae51f7 | 20 | int switchVal = spiMaster.write(0x55); |
micheleperotti | 0:f7cd39ae51f7 | 21 | spiSS=1; |
micheleperotti | 0:f7cd39ae51f7 | 22 | wait(0.0001); |
micheleperotti | 0:f7cd39ae51f7 | 23 | //write LED |
micheleperotti | 0:f7cd39ae51f7 | 24 | spiSS=0; |
micheleperotti | 0:f7cd39ae51f7 | 25 | spiMaster.write('#'); |
micheleperotti | 0:f7cd39ae51f7 | 26 | spiMaster.write(0x02); |
micheleperotti | 0:f7cd39ae51f7 | 27 | spiMaster.write(switchVal); |
micheleperotti | 0:f7cd39ae51f7 | 28 | spiSS=1; |
micheleperotti | 0:f7cd39ae51f7 | 29 | wait(0.0001); |
micheleperotti | 0:f7cd39ae51f7 | 30 | //read LED |
micheleperotti | 0:f7cd39ae51f7 | 31 | spiSS=0; |
micheleperotti | 0:f7cd39ae51f7 | 32 | spiMaster.write('#'); |
micheleperotti | 0:f7cd39ae51f7 | 33 | spiMaster.write(0x03); |
micheleperotti | 0:f7cd39ae51f7 | 34 | int LEDVal = spiMaster.write(0x55); |
micheleperotti | 0:f7cd39ae51f7 | 35 | spiSS=1; |
micheleperotti | 0:f7cd39ae51f7 | 36 | wait(0.0001); |
micheleperotti | 0:f7cd39ae51f7 | 37 | //read switch another tima |
micheleperotti | 0:f7cd39ae51f7 | 38 | spiSS=0; |
micheleperotti | 0:f7cd39ae51f7 | 39 | spiMaster.write('#'); |
micheleperotti | 0:f7cd39ae51f7 | 40 | spiMaster.write(0x01); |
micheleperotti | 0:f7cd39ae51f7 | 41 | switchVal = spiMaster.write(0x55); |
micheleperotti | 0:f7cd39ae51f7 | 42 | spiSS=1; |
micheleperotti | 0:f7cd39ae51f7 | 43 | wait(0.0001); |
micheleperotti | 0:f7cd39ae51f7 | 44 | //verify correctness of SPI behaviour |
micheleperotti | 0:f7cd39ae51f7 | 45 | if (LEDVal!=switchVal) |
micheleperotti | 0:f7cd39ae51f7 | 46 | { |
micheleperotti | 0:f7cd39ae51f7 | 47 | redLed=0; |
micheleperotti | 0:f7cd39ae51f7 | 48 | } else { |
micheleperotti | 0:f7cd39ae51f7 | 49 | redLed=1; |
micheleperotti | 0:f7cd39ae51f7 | 50 | } |
micheleperotti | 0:f7cd39ae51f7 | 51 | wait (0.02); |
micheleperotti | 0:f7cd39ae51f7 | 52 | } |
micheleperotti | 0:f7cd39ae51f7 | 53 | } |