Arduino Forum

Using Arduino => Networking, Protocols, and Devices => Topic started by: nthu on Jun 29, 2020, 01:36 pm

Title: AD7152 TO Arduino
Post by: nthu on Jun 29, 2020, 01:36 pm
Hello everyone, I am using EVAL-AD7152 to get the capacitance data from a 2pF ceramic capacitor, but the Arduino can not read the capacitance value of AD7152, I use 3.3V arduino mega2560 as my master device, and my slave device is 3.3V. Below is my code.



#include <Wire.h>

#define AD7152_I2C_ADDRESS 0x48               //AD7152 adress
#define AD7152_REG_STATUS 0x00
#define AD7152_REG_CH1_DATA_MSB 0x01
#define AD7152_REG_CH1_DATA_LSB 0x02
#define AD7152_REG_CH1_SETUP 0x0B
#define AD7152_REG_CONFIGURATION 0x0F

byte myArray[] = {219, 0, 0, 0, 49, 0, 255};    //SETUP Reg, x, x, x, CONFIG REg

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(AD7152_I2C_ADDRESS);
  Wire.write(AD7152_REG_CH1_SETUP);          //Move pointer to the SETUP register address
  Wire.write(myArray, sizeof(myArray));  //see global arry declaration
  byte busStatus = Wire.endTransmission();
  delay(4);
  if(busStatus !=0)
  {
    Serial.println("I2C Communication problem....!");
    while(1);      //wait for ever
  }
   
}

void loop()
{
  Wire.beginTransmission(AD7152_I2C_ADDRESS);   // "Hey, AD7152 @ 0x48! Message for you"
  Wire.write(AD7152_REG_STATUS);     //pointing Status Register
  Wire.endTransmission();
  delay(4);
  do
  {         
      Wire.requestFrom(AD7152_I2C_ADDRESS, 1); //if conversion complete, we can read data bytes also
  }
  while(Wire.read() != 0x52);     //at busy--Wire.read() = Status Register 0x53; else Wire.read() = 0x52                   
  Wire.beginTransmission(AD7152_I2C_ADDRESS);   // "Hey, AD7152 @ 0x48! Message for you"
  Wire.write(AD7152_REG_CH1_DATA_MSB);     //pointing DATA_H Register
  Wire.endTransmission();
  delay(4);
  Wire.requestFrom(AD7152_I2C_ADDRESS, 2);
  int ch1 = (int)Wire.read() <<8 | (int) Wire.read();
  float Cap1 = (ch1 - 0x8000) / 0xFFF0; //Converts digital value into capacitance, formula from datasheet.
  //print the data on serial monitor
  Serial.print("Ch1="); Serial.print(ch1, BIN);
  Serial.print(" Ch1="); Serial.print(ch1, DEC);
  Serial.print(" C1="); Serial.println(Cap1, 4);
  delay(700);
}
Title: Re: AD7152 TO Arduino
Post by: trethbos on Jul 03, 2020, 10:46 pm
I'm really new to coding... so this may be nonsense, but for an Int the Arduino reference states

"On the Arduino Uno (and other ATMega based boards) an int stores a 16-bit (2-byte) value."

In your code you have
 int ch1 = (int)Wire.read() <<8 | (int) Wire.read();
  float Cap1 = (ch1 - 0x8000) / 0xFFF0; //Converts digital value into capacitance, formula from datasheet.

Doesn't ch1 need to be 4 bytes to work in your formula?

As said I'm learning, so I would like to know the answer.