I want to use the I2C to communicate,This is my code:
uint8 TwoWire::readregister(uint8 slave_addr,uint8 reg_addr, uint8 *value) {
uint8 ret = SUCCESS;
if(0 == i2c_init_flag){
Wire.begin();
i2c_init_flag = 1;
}
i2c_start(port);
i2c_shift_out(port, (slave_addr << 1) | I2C_WRITE);
if (!i2c_get_ack(port)) return ENACKADDR;
ret = writeOneByte(reg_addr);
if (ret) return ret; // SUCCESS is 0
ret= readOneByte(slave_addr, value);
return(ret);
}
But when I use this fucntion like this to test the I2C:
#define DEV_I2C_ADDR 0x18 //0 0 1 1 0 0 X
void test_I2C()
{
uint8 value = 0xff;
if(SUCCESS == Wire.readregister(DEV_I2C_ADDR, 0x0C, &value)){
SerialUSB.print("I2CVALUE=0x");
SerialUSB.println(value,HEX);
SerialUSB.print("\r\n");
}else {
SerialUSB.print("Read error....");
SerialUSB.print("\r\n");
}
Omni.delayMS(1000);
}
The SerialUSB just ouput:I2CVALUE=0x0
So, anyone can tell me why the result is 0x0?Is my function write correct?
Many thanks!