SPI Hello World

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002  
00003 SPI spi(p5, p6, p7); // mosi, miso, sclk
00004 DigitalOut cs(p8);
00005  
00006 int main() {
00007     // Chip must be deselected
00008     cs = 1;
00009 
00010     // Setup the spi for 8 bit data, high steady state clock,
00011     // second edge capture, with a 1MHz clock rate
00012     spi.format(8,3);
00013     spi.frequency(1000000);
00014  
00015     // Select the device by seting chip select low
00016     cs = 0;
00017  
00018     // Send 0x8f, the command to read the WHOAMI register
00019     spi.write(0x8F);
00020  
00021     // Send a dummy byte to receive the contents of the WHOAMI register
00022     int whoami = spi.write(0x00);
00023     printf("WHOAMI register = 0x%X\n", whoami);
00024  
00025     // Deselect the device
00026     cs = 1;
00027 }