Need to read and write 4 bytes data for SPI


hi everyone,

i need read , write adas1000 arduino uno. each register 32bits , 4 bytes data  has read , write @ same time.

this datasheet link.
http://www.analog.com/media/en/technical-documentation/data-sheets/adas1000_1000-1_1000-2.pdf
people trying byyrdkrx62n spi proticol code.( https://wiki.analog.com/resources/tools-software/uc-drivers/renesas/adas1000.) looks complicated , device need configured.


kindly, me following code. need read , write, send @ least 1 register value.

// spi
#define dout 11 //mosi
#define din 12 //miso
#define spick 13 //sck

// pins
#define pin_cs    7
#define pin_drdy  6

// adas1000 communicates using spi, include library:
#include <spi.h>


 int drdy = 6;
 int cs = 7;

union fourbyte{
   struct {
     unsigned long value:32; //32bit register values go in here
     byte command:8; //8bit command goes in here.
   };
   byte bit8[4]; //this used efficient conversion of above 4 bytes.
};

void setup() {
  serial.begin(115200);

  // start spi library:
  spi.begin();
  spi.setdatamode(spi_mode3);
  spi.setclockdivider(spi_clock_div16);
  spi.setbitorder(msbfirst);
 
  // initalize the  data ready , chip select pins:
  pinmode(drdy, input);
  pinmode(cs, output);

}

void loop(){
 unsigned long register = spi_read(0x01);
 serial.print("register=");
 serial.println(register);
 delay(100);
}

void spi_write(union fourbyte data) {
 digitalwrite(cs,low); //using cs pin, sync1/sync0 bytes not needed
 for(char = 3; >= 0; i--){
   spi.transfer(data.bit8); //transfer 4 bytes of data - command first, big endian transfer of 32bit value.
 }
 digitalwrite(cs,high);
}

unsigned long spi_read(byte command){
 digitalwrite(cs,low); //ss goes low mark start of transmission
 union fourbyte data = {0xf804be,command}; //generate data sent, i.e. command plus sync bytes.
 for(char = 3; >= 0; i--){
   data.bit8 = spi.transfer(data.bit8); //send data whilst reading in result
 }
 digitalwrite(cs,high); //ss goes high mark end of transmission
 return data.value; //return 32bit value recieved.
}

quote
and 4 bytes data  has read , write @ same time.
no. 4 bytes need read 1 of other, , reassembled long or unsigned long or float, appropriate.

the code posted incorrectly something. expected something. 0 3. down minors you.


Arduino Forum > Using Arduino > Programming Questions > Need to read and write 4 bytes data for SPI


arduino

Comments