I changed to this...but still not working...
#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[i]); //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[i] = SPI.transfer(data.bit8[i]); //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.
}