in the datasheet, it's stated that I should apply 32 serial clocks to get the 32 bits of data. How can I program this is Arduino?
You call
I assume the least significant byte is received first. Make sure the SPI mode is the right one, as indicated in your datasheet. If the slave can't handle being deasserted between bytes (which is what |
|||||||||
|
Sparkfun has a good explaination of what SPI is: The Arduino IDE has a SPI library that comes with the IDE. The library comes with two examples: |
|||
|
Objective is to read 32 bits using the (unknown) device's SPI port. If the device will tolerate the SPI Chip Select line activity (going from inactive to active to inactive for each 8 bit byte read) you should be able to get the desired 32 bits of data by performing 4 consecutive 8 bit SPI reads. However, if the device will not tolerate the above SPI Chip Select line activity (that is, if the device requires the SPI Chip Select to be active for the entire 32 bit SPI transaction), you can not separate the 32 bit transaction into 4 individual 8 bit SPI transactions with out controlling the SPI Chip Select line. The following code example illustrates how this is done:
The above example comes from the Arduino SPI library web page and is a 16 bit (not 32 bit) transfer. To code up a 32 bit transfer, continue to call the SPI.transfer method using the SPI_CONTINUE parameter. added later... It appears people are discouraging use of the DueExtendedSPI methods. And SPISettings and SPI.beginTransaction() are to be used? If so this page show example code where the code is explicitly controlling the SPI Chip Select line (look for slaveAPin & slaveBPin). Note how the code is reading 24 bits from slave A and writing 8 bits to slave B. To read 32 instead of 24 bits, we need to alter a segment of the code (along with a few other supporting changes) in this fashion:
|
|||||||||||||
|
byte result[4]; for (i = 0; i < 4; i++) result[i] = SPI.transfer(0x00);
and you will find inresult
the 32 bits you need – frarugi87 Apr 27 at 12:45