Kevin Moloney
/
Nucleo_spi_master
test
main.cpp@1:bb09fc532f44, 2017-06-20 (annotated)
- Committer:
- kevin1990
- Date:
- Tue Jun 20 13:19:11 2017 +0000
- Revision:
- 1:bb09fc532f44
- Parent:
- 0:48b56cad57ae
first test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevin1990 | 0:48b56cad57ae | 1 | #include "mbed.h" |
kevin1990 | 0:48b56cad57ae | 2 | |
kevin1990 | 1:bb09fc532f44 | 3 | SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk |
kevin1990 | 1:bb09fc532f44 | 4 | DigitalOut cs(PB_6); |
kevin1990 | 1:bb09fc532f44 | 5 | DigitalOut syncPin (D8); |
kevin1990 | 1:bb09fc532f44 | 6 | |
kevin1990 | 1:bb09fc532f44 | 7 | Serial pc(USBTX, USBRX, 9600); // tx, rx |
kevin1990 | 0:48b56cad57ae | 8 | |
kevin1990 | 0:48b56cad57ae | 9 | int main() { |
kevin1990 | 0:48b56cad57ae | 10 | |
kevin1990 | 1:bb09fc532f44 | 11 | // set sync pin to known state |
kevin1990 | 1:bb09fc532f44 | 12 | syncPin = 0; |
kevin1990 | 1:bb09fc532f44 | 13 | // Chip must be deselected |
kevin1990 | 1:bb09fc532f44 | 14 | cs = 1; |
kevin1990 | 1:bb09fc532f44 | 15 | int value = 4; |
kevin1990 | 1:bb09fc532f44 | 16 | |
kevin1990 | 1:bb09fc532f44 | 17 | spi.format(8,3); // Setup: bit data, high steady state clock, 2nd edge capture |
kevin1990 | 1:bb09fc532f44 | 18 | spi.frequency(1000000); //1MHz |
kevin1990 | 0:48b56cad57ae | 19 | |
kevin1990 | 1:bb09fc532f44 | 20 | // set sync pin high |
kevin1990 | 1:bb09fc532f44 | 21 | syncPin = 1; |
kevin1990 | 1:bb09fc532f44 | 22 | for (int i = 0; i < 6; i++) { |
kevin1990 | 1:bb09fc532f44 | 23 | // Select device |
kevin1990 | 1:bb09fc532f44 | 24 | cs = 0; |
kevin1990 | 1:bb09fc532f44 | 25 | spi.write(value); |
kevin1990 | 1:bb09fc532f44 | 26 | // Deselect device |
kevin1990 | 1:bb09fc532f44 | 27 | cs = 1; |
kevin1990 | 1:bb09fc532f44 | 28 | value++; |
kevin1990 | 0:48b56cad57ae | 29 | } |
kevin1990 | 0:48b56cad57ae | 30 | } |