spi_comm

Dependencies:   mbed

Committer:
ShaolinPoutine
Date:
Wed Jan 11 22:55:47 2017 +0000
Revision:
0:ff086cd5333c
asdf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ShaolinPoutine 0:ff086cd5333c 1 #include "mbed.h"
ShaolinPoutine 0:ff086cd5333c 2
ShaolinPoutine 0:ff086cd5333c 3 SPI spi(p5, p6, p7); // mosi, miso, sclk
ShaolinPoutine 0:ff086cd5333c 4 DigitalOut cs(p8);
ShaolinPoutine 0:ff086cd5333c 5
ShaolinPoutine 0:ff086cd5333c 6 int main() {
ShaolinPoutine 0:ff086cd5333c 7 // Chip must be deselected
ShaolinPoutine 0:ff086cd5333c 8 cs = 1;
ShaolinPoutine 0:ff086cd5333c 9
ShaolinPoutine 0:ff086cd5333c 10 // Setup the spi for 8 bit data, high steady state clock,
ShaolinPoutine 0:ff086cd5333c 11 // second edge capture, with a 1MHz clock rate
ShaolinPoutine 0:ff086cd5333c 12 spi.format(8,3);
ShaolinPoutine 0:ff086cd5333c 13 spi.frequency(1000000);
ShaolinPoutine 0:ff086cd5333c 14
ShaolinPoutine 0:ff086cd5333c 15 // Select the device by seting chip select low
ShaolinPoutine 0:ff086cd5333c 16 cs = 0;
ShaolinPoutine 0:ff086cd5333c 17
ShaolinPoutine 0:ff086cd5333c 18 // Send 0x8f, the command to read the WHOAMI register
ShaolinPoutine 0:ff086cd5333c 19 spi.write(0x8F);
ShaolinPoutine 0:ff086cd5333c 20
ShaolinPoutine 0:ff086cd5333c 21 // Send a dummy byte to receive the contents of the WHOAMI register
ShaolinPoutine 0:ff086cd5333c 22 int whoami = spi.write(0x00);
ShaolinPoutine 0:ff086cd5333c 23 printf("WHOAMI register = 0x%X\n", whoami);
ShaolinPoutine 0:ff086cd5333c 24
ShaolinPoutine 0:ff086cd5333c 25 // Deselect the device
ShaolinPoutine 0:ff086cd5333c 26 cs = 1;
ShaolinPoutine 0:ff086cd5333c 27 }