Go Down

Topic: SPI 32-bits Fetching data for IC (Read 681 times) previous topic - next topic

Jaay

Hi everybody,
         Currently i'm now trying to get a data from my energy ic chip CS5464....but I dun know why my program is not working.Help me please..thanks a lothttp://www.cirrus.com/en/pubs/proDatasheet/CS5464_F3.pdf

Code: [Select]
#include <SPI.h>

int CS = 10;


union FourByte{
    struct {
      unsigned long value:24; //24bit register values go in here
      byte command:8; //8bit command goes in here.
    };
    byte bit8[4]; //this is just used for efficient conversion of the above into 4 bytes.
};

void setup(){
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE0);
 
  Serial.begin(9600);
  pinMode(CS, OUTPUT);
  digitalWrite(CS, HIGH);
  //Page
  //example of reading data
// unsigned long voltage = SPI_read(0x04);//Instantaneous Voltage Channel 1
 
  //example of writing data
  union FourByte INI;
  INI.command = 0b11111111; //Write to config register
  INI.value = 0xFFFFFE; //This is the default value from datasheet, just using it as an example.
  SPI_write(INI);
 
 
  union FourByte data;
  data.command = 0b11101000; //Write to config register
  data.value = 0xFFFFFE; //This is the default value from datasheet, just using it as an example.
  SPI_write(data);
}

void loop(){
  unsigned long voltage = SPI_read(0x04);
  Serial.print("Voltage=");
  Serial.println(voltage);
  delay(1000);
}

void SPI_write(union FourByte data) {
  digitalWrite(CS,LOW); //Using CS pin, so sync1/sync0 bytes not needed
  for(char i = 3; i >= 0; i--){
    SPI.transfer(data.bit8); //transfer all 4 bytes of data - command first, then Big Endian transfer of the 24bit value.
  }
  digitalWrite(CS,HIGH);
}

unsigned long SPI_read(byte command){
  digitalWrite(CS,LOW); //SS goes low to mark start of transmission
  union FourByte data = {0xFEFEFE,command}; //generate the data to be sent, i.e. your command plus the Sync bytes.
  for(char i = 3; i >= 0; i--){
    data.bit8 = SPI.transfer(data.bit8); //send the data whilst reading in the result
  }
  digitalWrite(CS,HIGH); //SS goes high to mark end of transmission
  return data.value; //return the 24bit value recieved.
}

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

What is the intention of this line?

Code: [Select]

    data.bit8 = SPI.transfer(data.bit8); //send the data whilst reading in the result
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

- Moderator
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Jaay

The program is not working..but it can compile...that line is to be able to make 32 bits into 1 line

Grumpy_Mike


The program is not working..but it can compile...that line is to be able to make 32 bits into 1 line

Well it won't.
SPI transfer only transfers 8 bits at a time.

Nick Gammon


The program is not working..but it can compile...


Not for me it doesn't:

Code: [Select]
sketch_nov22a.cpp: In function 'void SPI_write(FourByte)':
sketch_nov22a:48: error: invalid conversion from 'byte*' to 'byte'
sketch_nov22a:48: error: initializing argument 1 of 'static byte SPIClass::transfer(byte)'
sketch_nov22a.cpp: In function 'long unsigned int SPI_read(byte)':
sketch_nov22a:57: error: invalid conversion from 'byte*' to 'byte'
sketch_nov22a:57: error: initializing argument 1 of 'static byte SPIClass::transfer(byte)'
sketch_nov22a:57: error: incompatible types in assignment of 'byte' to 'byte [4]'
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Jaay

ouch...  :smiley-sad-blue: How can I repair the Write and Read Functions?Is my Initializing part(syncing and conversion) correct?

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Tom Carpenter

I think this thread has spun off from this one:
http://arduino.cc/forum/index.php/topic,133133.15.html
In which case in my original code, this line:
Code: [Select]
data.bit8 = SPI.transfer(data.bit8);
was:
Code: [Select]
data.bit8[i] = SPI.transfer(data.bit8[i]);
In which four special bytes are sent to recieve 3 bytes in return (the first of the four recieved bytes gets ignored when the function returns the 24bit field in the struct)

similarly in the write function, this:
Code: [Select]
    SPI.transfer(data.bit8); //transfer all 4 bytes of data - command first, then Big Endian transfer of the 24bit value.
was/should be
Code: [Select]
    SPI.transfer(data.bit8[i]); //transfer all 4 bytes of data - command first, then Big Endian transfer of the 24bit value.
~Tom~

Go Up