9 years, 10 months ago.

How to use the spi.read() ?

I want to send a value to the mosi then read a value with miso. But I have an error on spi.read () and I do not understand why!

Here is my program:

  1. include "mbed.h"

SPI spi(p11, p12, p13); mosi, miso, sclk DigitalOut cs1(p21), csADC(p26);; int val = 0, valADC=0;

int main() { cs1 = 1; csADC=0; spi.format(16,1); spi.frequency(1000000);

while (1) {

printf("Enter decimal value :"); scanf("%d", &val); printf("%d\r\n",val);

Send 0x8f, the command to read the WHOAMI register cs1=0; spi.write(val); cs1=1; wait(1); valADC = spi.read(); printf("%d\r\n",valADC); } }

Question relating to:

1 Answer

9 years, 10 months ago.

for SPI you don`t have a different function for reading. i don`t see anywhere in the SPI Class spi.read() and that`s because spi.write(val) writes val to spi and returns the response. For example:

int spi_response=0;
cs=0;
spi_response=spi.write(val);
cs=1;

i hope my answer will help you

Accepted Answer

The returned value corresponds to the value of miso?

posted by LouiseC LouiseC 18 Jun 2014

Yes

posted by Eduard Soltuzu 18 Jun 2014