test

Dependencies:   mbed

Committer:
kevin1990
Date:
Fri Jun 02 09:14:50 2017 +0000
Revision:
0:48b56cad57ae
Child:
1:bb09fc532f44
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kevin1990 0:48b56cad57ae 1 #include "mbed.h"
kevin1990 0:48b56cad57ae 2
kevin1990 0:48b56cad57ae 3 SPI spi(D11, D12, D13); // mosi, miso, sclk
kevin1990 0:48b56cad57ae 4 DigitalOut chipSelect(D10);
kevin1990 0:48b56cad57ae 5
kevin1990 0:48b56cad57ae 6 Serial pc(USBTX, USBRX); // tx, rx
kevin1990 0:48b56cad57ae 7
kevin1990 0:48b56cad57ae 8 int main() {
kevin1990 0:48b56cad57ae 9
kevin1990 0:48b56cad57ae 10 int valueToSendToSlave = 20; // Starting value only, this increments
kevin1990 0:48b56cad57ae 11 spi.format(8,3); // Setup: bit data, high steady state clock, 2nd edge capture
kevin1990 0:48b56cad57ae 12 spi.frequency(1000000); //1MHz
kevin1990 0:48b56cad57ae 13
kevin1990 0:48b56cad57ae 14 pc.printf("======================================================\r\n");
kevin1990 0:48b56cad57ae 15 pc.printf("Press any key to start...\r\n");
kevin1990 0:48b56cad57ae 16 pc.getc(); // wait for keyboard
kevin1990 0:48b56cad57ae 17
kevin1990 0:48b56cad57ae 18 int counter = 1;
kevin1990 0:48b56cad57ae 19 while (1) {
kevin1990 0:48b56cad57ae 20
kevin1990 0:48b56cad57ae 21 pc.printf("%d Value to send = %d ", counter++, valueToSendToSlave);
kevin1990 0:48b56cad57ae 22
kevin1990 0:48b56cad57ae 23 chipSelect = 0; // Select device
kevin1990 0:48b56cad57ae 24 int dataFromSlave = spi.write(valueToSendToSlave);
kevin1990 0:48b56cad57ae 25 chipSelect = 1; // Deselect device
kevin1990 0:48b56cad57ae 26
kevin1990 0:48b56cad57ae 27 pc.printf(" returns %d\r\n", dataFromSlave);
kevin1990 0:48b56cad57ae 28
kevin1990 0:48b56cad57ae 29 valueToSendToSlave++;
kevin1990 0:48b56cad57ae 30
kevin1990 0:48b56cad57ae 31 wait(5); // Wait for 5 seconds for readability only
kevin1990 0:48b56cad57ae 32
kevin1990 0:48b56cad57ae 33 }
kevin1990 0:48b56cad57ae 34 }