V1.0 of SPI Slave Example for Serial Communications Workshop
Dependencies: mbed
main.cpp@0:45714e4b114f, 2015-03-06 (annotated)
- Committer:
- MoffMade
- Date:
- Fri Mar 06 22:01:40 2015 +0000
- Revision:
- 0:45714e4b114f
V1.0 of SPI Slave Example for Serial Communications Workshop
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MoffMade | 0:45714e4b114f | 1 | #include "mbed.h" |
MoffMade | 0:45714e4b114f | 2 | |
MoffMade | 0:45714e4b114f | 3 | SPISlave spi(PTD2, PTD3, PTD1, PTD10); // mosi, miso, sclk, cs |
MoffMade | 0:45714e4b114f | 4 | Serial pc(USBTX, USBRX); // Configure UART |
MoffMade | 0:45714e4b114f | 5 | PwmOut g_led(LED_GREEN); // Configure Green LED |
MoffMade | 0:45714e4b114f | 6 | |
MoffMade | 0:45714e4b114f | 7 | int main() { |
MoffMade | 0:45714e4b114f | 8 | // Setup the spi for 8 bit data, high steady state clock, |
MoffMade | 0:45714e4b114f | 9 | // second edge capture, with a 1MHz clock rate |
MoffMade | 0:45714e4b114f | 10 | spi.format(8,3); |
MoffMade | 0:45714e4b114f | 11 | spi.frequency(1000000); |
MoffMade | 0:45714e4b114f | 12 | // Configure PC/Serial Connection |
MoffMade | 0:45714e4b114f | 13 | pc.baud(9600); |
MoffMade | 0:45714e4b114f | 14 | |
MoffMade | 0:45714e4b114f | 15 | while(1) |
MoffMade | 0:45714e4b114f | 16 | { |
MoffMade | 0:45714e4b114f | 17 | char receivedValue = 0; |
MoffMade | 0:45714e4b114f | 18 | if(spi.receive()) { //Poll SPI module to see if a byte has been received |
MoffMade | 0:45714e4b114f | 19 | receivedValue = spi.read(); // Read Received value |
MoffMade | 0:45714e4b114f | 20 | if(receivedValue != 0x00) { |
MoffMade | 0:45714e4b114f | 21 | spi.reply(receivedValue); // Set reply when next byte is received |
MoffMade | 0:45714e4b114f | 22 | pc.printf("Received Data = 0x%X\n\r",receivedValue); // Output to PC Serial |
MoffMade | 0:45714e4b114f | 23 | g_led = 1.0f * receivedValue; // Set Green LED to be percentage value |
MoffMade | 0:45714e4b114f | 24 | } |
MoffMade | 0:45714e4b114f | 25 | } |
MoffMade | 0:45714e4b114f | 26 | } |
MoffMade | 0:45714e4b114f | 27 | } |