SPI Hello World
Fork of SPI_HelloWorld_Mbed by
Note
You will need to redefine the SPI pins to match the pins on your board to run this example. The pin mapping can be found on the board's Platform page.
API
API reference
Import librarymbed
main.cpp@0:466ad3f38b6b, 2013-02-12 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Feb 12 17:25:49 2013 +0000
- Revision:
- 0:466ad3f38b6b
- Child:
- 2:34bd7b8d30f9
SPI Hello World
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:466ad3f38b6b | 1 | #include "mbed.h" |
mbed_official | 0:466ad3f38b6b | 2 | |
mbed_official | 0:466ad3f38b6b | 3 | SPI spi(p5, p6, p7); // mosi, miso, sclk |
mbed_official | 0:466ad3f38b6b | 4 | DigitalOut cs(p8); |
mbed_official | 0:466ad3f38b6b | 5 | |
mbed_official | 0:466ad3f38b6b | 6 | int main() { |
mbed_official | 0:466ad3f38b6b | 7 | // Chip must be deselected |
mbed_official | 0:466ad3f38b6b | 8 | cs = 1; |
mbed_official | 0:466ad3f38b6b | 9 | |
mbed_official | 0:466ad3f38b6b | 10 | // Setup the spi for 8 bit data, high steady state clock, |
mbed_official | 0:466ad3f38b6b | 11 | // second edge capture, with a 1MHz clock rate |
mbed_official | 0:466ad3f38b6b | 12 | spi.format(8,3); |
mbed_official | 0:466ad3f38b6b | 13 | spi.frequency(1000000); |
mbed_official | 0:466ad3f38b6b | 14 | |
mbed_official | 0:466ad3f38b6b | 15 | // Select the device by seting chip select low |
mbed_official | 0:466ad3f38b6b | 16 | cs = 0; |
mbed_official | 0:466ad3f38b6b | 17 | |
mbed_official | 0:466ad3f38b6b | 18 | // Send 0x8f, the command to read the WHOAMI register |
mbed_official | 0:466ad3f38b6b | 19 | spi.write(0x8F); |
mbed_official | 0:466ad3f38b6b | 20 | |
mbed_official | 0:466ad3f38b6b | 21 | // Send a dummy byte to receive the contents of the WHOAMI register |
mbed_official | 0:466ad3f38b6b | 22 | int whoami = spi.write(0x00); |
mbed_official | 0:466ad3f38b6b | 23 | printf("WHOAMI register = 0x%X\n", whoami); |
mbed_official | 0:466ad3f38b6b | 24 | |
mbed_official | 0:466ad3f38b6b | 25 | // Deselect the device |
mbed_official | 0:466ad3f38b6b | 26 | cs = 1; |
mbed_official | 0:466ad3f38b6b | 27 | } |