我正在写一段C代码,我将一些值写入寄存器(来自i2c器件),从寄存器读回值并比较值是否相同。它看起来像这样:
typedef struct
{
uint8 address; //address of the i2c slave register
uint8 value; // value which have to be written into the register
} messages;
messages *pMsg;
void write_to_i2c( uint8 address, uint8 value)
{
i2c_Instance()->DTR = address; //DTR is the Data Transmit Register of i2c
i2c_Instance()->DTR = value;
}
uint8 read_from_i2c( uint8 address )
{
i2c_Instance()->DTR = address;
return i2c_Instance()->DTR);
}
write_to_i2c( pMsg->address, pMsg->value )
tempReadback = read_from_i2c( pMsg->address); //write the read value into a variable
if (tempReadback == pMsg->value) //compare if the written value is the same as the read value
{
return OK;
}
else
{
return NOT_OK
}
到目前为止,好的。函数void write_to_i2c将消息写入i2c寄存器。消息是i2c从器件的寄存器地址和必须写入寄存器的值。
功能" read_from_i2c"只读回写入i2c从设备寄存器的值。当我调试代码时,我可以在寄存器中看到正确的值,但每次都在else循环中结束。所以,问题是:我将这些值与正确值进行比较的方式是什么?