I have referred the data sheet and successfully configure the register.But when I use Wire function to read the data through serial port, it turned out to be 0xffff(65535) all the time.I have checked my hardware connection and it is all right...Maybe there’s something wrong with my code but I can’t find it.
Here is my code.
#include<Wire.h>
int LDC = 0x2A;
int CH0DATA = 0x00;
int CH1DATA = 0x02;
int CH2DATA = 0x04;
int CH3DATA = 0x06;
word test;
unsigned long ini_0 = 0,ini_1 = 0,ini_2 = 0,ini_3 = 0;
word readValue(int LDC,int reg)
{
int a = 0,b = 0;
word val = 0;
Wire.beginTransmission(LDC);
Wire.write(reg);
Wire.endTransmission(LDC);
Wire.requestFrom(LDC,2);
while(Wire.available())
{
a = Wire.read();
b = Wire.read();
}
val = a;
val<<=8;
val+=b;
test = a;
return val;
}
unsigned long readChannel0()
{
unsigned long val = 0;
word a = 0;
a = readValue(LDC,CH0DATA);
val = a;
return val;
}
unsigned long readChannel1()
{
unsigned long val = 0;
word a = 0;
a = readValue(LDC,CH1DATA);
val = a;
return val;
}
unsigned long readChannel2()
{
unsigned long val = 0;
word a = 0;
a = readValue(LDC,CH2DATA);
val = a;
return val;
}
unsigned long readChannel3()
{
unsigned long val = 0;
word a = 0;
a = readValue(LDC,CH3DATA);
val = a;
return val;
}
void writeConfig(int LDC,int reg,int MSB,int LSB)
{
Wire.beginTransmission(LDC);
Wire.write(reg);
Wire.write(MSB);
Wire.write(LSB);
Wire.endTransmission();
}
void Configuration()
{
writeConfig(LDC,0x14,0x10,0x05);//CLOCK_DIVIDERS_CH0
writeConfig(LDC,0x1E,0x60,0x00);//DRIVE_CURRENT_CH0
writeConfig(LDC,0x10,0x00,0x0A);//SETTLECOUNT_CH0
writeConfig(LDC,0x08,0x04,0xD6);//RCOUNT_CH0
writeConfig(LDC,0x0C,0x29,0xFF);//OFFSET0
writeConfig(LDC,0x15,0x10,0x05);//CLOCK_DIVIDERS_CH1
writeConfig(LDC,0x1F,0x70,0x00);//DRIVE_CURRENT_CH1
writeConfig(LDC,0x11,0x00,0x0A);//SETTLECOUNT_CH1
writeConfig(LDC,0x09,0x04,0xD6);//RCOUNT_CH1
writeConfig(LDC,0x0D,0x29,0xFF);//OFFSET1
writeConfig(LDC,0x16,0x10,0x05);//CLOCK_DIVIDERS_CH2
writeConfig(LDC,0x20,0x70,0x00);//DRIVE_CURRENT_CH2
writeConfig(LDC,0x12,0x00,0x0A);//SETTLECOUNT_CH2
writeConfig(LDC,0x0A,0x04,0xD6);//RCOUNT_CH2
writeConfig(LDC,0x0E,0x29,0xFF);//OFFSET2
writeConfig(LDC,0x17,0x10,0x05);//CLOCK_DIVIDERS_CH3
writeConfig(LDC,0x21,0x60,0x00);//DRIVE_CURRENT_CH3
writeConfig(LDC,0x13,0x00,0x0A);//SETTLECOUNT_CH3
writeConfig(LDC,0x0B,0xFF,0xFF);//RCOUNT_CH3
writeConfig(LDC,0x0F,0x29,0xFF);//OFFSET3
writeConfig(LDC,0x18,0x00,0x01);//STATUS
writeConfig(LDC,0x1A,0x00,0x00);//CONFIG
writeConfig(LDC,0x19,0x00,0x00);//ERROR_CONFIG
writeConfig(LDC,0x1B,0xC2,0x0C);//MUX_CONFIG
writeConfig(LDC,0x1C,0x06,0x00);//RESET_DEVICE
}
void Caliberate()
{
ini_0 = readChannel0();
ini_1 = readChannel1();
ini_2 = readChannel2();
ini_3 = readChannel3();
}
void ldc_init()
{
Wire.begin();
Configuration();
delay(500);
Caliberate();
}
void setup()
{
Serial.begin(9600);
void ldc_init();
}
void loop()
{
unsigned long data0 =5;
unsigned long data1 = readChannel1();
unsigned long data2 = readChannel2();
unsigned long data3 = readChannel3();
Serial.print("test:");
Serial.print(data0);
data0 = readValue(LDC,0x7f);
Serial.print("CH0:");
Serial.print(data0);
Serial.print(" CH1:");
Serial.print(data1);
Serial.print(" CH2:");
Serial.print(data2);
Serial.print(" CH3:");
Serial.println(data3);
delay(500);
}