Hi,
I have a IIC device with an address for read, 0xDD and an address for write, 0xDC. The address is marked as 7bits, which means the lowest bit is for marking reading/writing. So, the 7bits address of the device should be 0x6E.
To read the device data, I need to write 0xDD, 0x03 to the device. like this:
My code is here:
The .h file:
#ifndef BATTERY_PACK_H
#define BATTERY_PACK_H
#include "Arduino.h"
#include <Wire.h>
#define BP_ADDRESS 0x6E //
#define BP_REG 0x03
//=========================================================================
// Parameter structures
//=========================================================================
typedef struct
{
uint8_t BMS_addr;
uint8_t REG_addr;
uint16_t voltage;
uint16_t current;
uint16_t capacity;
uint16_t state;
uint8_t version;
uint8_t SOC;
uint8_t FET;
uint8_t battery_serial;
uint8_t max_temp;
} Battery_state;
class Battery_pack {
private:
public:
void read_BP_state(Battery_state *data);
};
#endif /* BATTERY_PACK_H */
The .cpp file:
#include "battery_pack.h"
void Battery_pack:: read_BP_state(Battery_state *data){
Wire.beginTransmission(BP_ADDRESS); // I2C 0, for GIGA, it's Pin 20 (SDA), Pin 21 (SCL)
Wire.write(BP_REG);
Wire.endTransmission();
Wire.requestFrom(BP_ADDRESS,15);
if (Wire.available()) {
Serial.println("Data received");
// Read and assign values to Battery_state struct
data->BMS_addr = Wire.read();
Serial.println(data->BMS_addr, HEX);
data->REG_addr = Wire.read();
data->voltage = Wire.read() << 8 | Wire.read();
data->current = Wire.read() << 8 | Wire.read();
data->capacity = Wire.read() << 8 | Wire.read();
data->state = Wire.read() << 8 | Wire.read();
data->version = Wire.read();
data->SOC = Wire.read();
data->FET = Wire.read();
data->battery_serial = Wire.read();
data->max_temp = Wire.read();
}
}
I also tried this .ccp file:
#include "battery_pack.h"
void Battery_pack:: read_BP_state(Battery_state *data){
Wire.beginTransmission(BP_ADDRESS); // I2C 0, for GIGA, it's Pin 20 (SDA), Pin 21 (SCL)
Wire.write(BP_REG);
Wire.requestFrom(BP_ADDRESS,15);
if (Wire.available()) {
Serial.println("Data received");
// Read and assign values to Battery_state struct
data->BMS_addr = Wire.read();
Serial.println(data->BMS_addr, HEX);
data->REG_addr = Wire.read();
data->voltage = Wire.read() << 8 | Wire.read();
data->current = Wire.read() << 8 | Wire.read();
data->capacity = Wire.read() << 8 | Wire.read();
data->state = Wire.read() << 8 | Wire.read();
data->version = Wire.read();
data->SOC = Wire.read();
data->FET = Wire.read();
data->battery_serial = Wire.read();
data->max_temp = Wire.read();
}
Wire.endTransmission();
}
The main code is here:
#include <battery_pack.h>
Battery_pack battery;
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
Wire.begin();
}
void loop() {
Battery_state data;
battery.read_BP_state(&data);
}
The problem is, the IIC read nothing from the device. I am not sure what happened.
If you have any advice? Thanks very much!