Hi,
I am integrating Teensy3.1 with ADE7880(energy meter IC) using I2C interface. I am using i2c_t3 library.
I observe the following.
1. For writing or reading into a slave device I need to right shift the slave address once.(e.g: slave_addr >> 1)
2. I am able to write and read 1 byte register.(e.g: LCYCMODE register, able to retrieve the same data which is written)(This is OK)
3. I am unable to read the correct data if its greater than 1 byte data.(e.g: AIRMS, 3 byte read only register)
4. I am able to read correct values of AIRMS register using other controller with I2C interface.
Thanks for the support and for i2c_t3 library.
Below is the code for reference:
Thank you,
Prasad
I am integrating Teensy3.1 with ADE7880(energy meter IC) using I2C interface. I am using i2c_t3 library.
I observe the following.
1. For writing or reading into a slave device I need to right shift the slave address once.(e.g: slave_addr >> 1)
2. I am able to write and read 1 byte register.(e.g: LCYCMODE register, able to retrieve the same data which is written)(This is OK)
3. I am unable to read the correct data if its greater than 1 byte data.(e.g: AIRMS, 3 byte read only register)
4. I am able to read correct values of AIRMS register using other controller with I2C interface.
Thanks for the support and for i2c_t3 library.
Below is the code for reference:
Code:
void setup() {
Serial.begin(115200);
Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, I2C_RATE_400);
}
void loop() {
uint8_t raw_data[2];
int int_data;
//Read Write register
write_meter(LCYCMODE, 10);
read_meter(LCYCMODE, 1, raw_data);
int_data = raw_data[0]);
Serial.println("LCYCMODE: ");
Serial.println(int_data, HEX);
Serial.print("---------------------------------------------------\n");
Serial.println("I2C READ 1 byte from MemAddr 0\n");
//Read only register
read_meter(AIRMS, 3, raw_data);
int_data = (raw_data[2] << 16 | raw_data[1] << 8 | raw_data[0]);
Serial.println("AIRMS: ");
Serial.println(int_data, HEX);
Serial.print("---------------------------------------------------\n");
Serial.println("I2C READ 1 byte from MemAddr 0\n");
}
void write_meter(int add, int data)
{
uint8_t target = 0x70; //slave addr
Serial.print("---------------------------------------------------\n");
Serial.print("I2C WRITE 1 byte at MemAddr 0\n");
Wire.beginTransmission(target>>1); // slave addr needs to be shifted for ack from slave
Wire.write((add>>8)); // memory address
Wire.write(add); // memory address
Wire.write(data); // data
Wire.endTransmission(I2C_STOP); // blocking I2C Tx (NOSTOP)
print_i2c_status(); // print I2C final status
}
void read_meter(int add, char no_bytes, uint8_t meter_data[])
{
uint8_t target = 0x70; // slave addr
Serial.print("---------------------------------------------------\n");
Serial.print("I2C READ 1 byte from MemAddr 0\n");
Wire.beginTransmission(target>>1); // slave addr
Wire.write((add>>8)); // memory address
Wire.write(add); // memory address
Wire.endTransmission(I2C_NOSTOP); // blocking write (NOSTOP - next cmd is RepSTART)
Wire.requestFrom(target>>1,1,I2C_STOP);// blocking read (NOSTOP - next cmd is RepSTART)
while(Wire.available() && no_bytes)
{
meter_data[no_bytes-1] = Wire.readByte(); //Read byte
no_bytes--;
}
}
Thank you,
Prasad
Last edited: