11 years, 4 months ago.

How to read SPI data from MISO

Hello all I have question: how to read SPI data? I have encoder and I sent a clock and get the data. The data should be 9 bits of pulses. How I can get the binary data from the MISO?

  1. include "mbed.h"

SPI spi(NC, p6, p7);

short byte[8];

int main() {

spi.format(9,2); Setup the spi for 9 bit data spi.frequency(900000); Create 900KHz clock

while (1) { for (int i = 0; i < 3; i++) { int i; byte[i] = spi.write(i);

pc.printf("%d ", byte[i] ); } pc.printf("\r\n"); wait(1); } }

Question relating to:

1 Answer

8 years, 11 months ago.

Every loop of your for() loop, int i gets redefined (first line of code inside the loop). Remove this: int i is already defined between the for() brackets

Also, why is your byte[] array 8 indexes big, while you only read the MISO pin 3 times?