Pages: [1]   Go Down
Author Topic: SPI 32-bits Fetching data for IC  (Read 517 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not compiling or not working?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the intention of this line?

Code:
    data.bit8 = SPI.transfer(data.bit8); //send the data whilst reading in the result
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Threads merged.

- Moderator
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 508
Posts: 31416
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Not for me it doesn't:

Code:
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]'
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did it compile for you or not?
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 71
Posts: 1641
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
data.bit8 = SPI.transfer(data.bit8);

was:
Code:
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:
    SPI.transfer(data.bit8); //transfer all 4 bytes of data - command first, then Big Endian transfer of the 24bit value.
was/should be
Code:
    SPI.transfer(data.bit8[i]); //transfer all 4 bytes of data - command first, then Big Endian transfer of the 24bit value.
Logged

~Tom~

Pages: [1]   Go Up
Jump to: