Hello,
I am trying to read values from a CDC, using an Arduino, I'm using I2C ofc and I already tested it to read simple stuff.
Now when I try to read output values it gets stuck in the Wire.available, because after the requestfrom, it is not sending any byte, so I get a deadlock.
I do not think it is a connection error as I have already tested the I2C. The sensor is well connected to the CDC (I have used a circuit from the supplier).
Does anyone know where the error might be? Attached below is the code.
I have not written the code (just adjusted it a bit), but I understand how it should work, except from the rconfig1 (do not know why it is needed).
CODE:
#include <Wire.h>
int chip_addr = 0x48; //AD7150 adress
#define ch1_dataH_addr 0x01 //Address to point the Data High of Ch1
#define ch1_dataL_addr 0x02 //Address to point the Data Low of Ch1
#define ch1_SetupR_addr 0x0B //Setup (range) register address for Ch1
#define ConfigR_addr 0x0F //Configuration register (operation mode -threshold-)
#define Ch1_ThHR_addr 0x09 //Ch1 Threshold High Register
#define Ch1_ThLR_addr 0x0A //Ch1 Threshold Low Register
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
Serial.println("Setup Done");
}
void rconfig1()
{
Wire.beginTransmission(chip_addr);
Wire.write(ch1_SetupR_addr); //Move pointer to the register address
Wire.write(203); //Setup features for range (bx11001011)
Wire.endTransmission();
Serial.println("RConfig1 done");
}
void loop() {
//Get capacitance data
// move the register pointer back to the second register
Serial.println("Starting the loop");
Wire.beginTransmission(chip_addr); // "Hey, AD7150 @ 0x48! Message for you"
Wire.write(1); // "move your register pointer back to 01h"
Wire.endTransmission(); // "Thanks, goodbye..."
// now get the data from the AD7150
Wire.requestFrom(chip_addr, 2); // "Hey - please send me the contents of your first register"
while(Wire.available() == 0); // slave may send less than requested
byte ch1H = Wire.read(); // first received byte stored here
byte ch1L = Wire.read(); // second received byte stored here
Wire.endTransmission();
unsigned int ch1 = ch1H*256 + ch1L; //Concatanates the two bytes into one 16 bit word for capacitor 1.
float Cap1 = (ch1-12288.0)*4/40944.0; // Converts the digital value into capacitance.
//print the data on serial monitor, data that was gotten from the registers that store capacitance values
Serial.print("Ch1=");
Serial.print(ch1, BIN);
Serial.print(" Ch1=");
Serial.print(ch1);
Serial.print(" C1=");
Serial.print(Cap1,2);
delay(700);
}