读取代码如下:uint8_t read_len(uint8_t addr, uint8_t reg, uint8_t len, uint8_t *buf)
{
while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY));
i2c_start_on_bus(I2CX);
while(!i2c_flag_get(I2CX, I2C_FLAG_SBSEND));
i2c_master_addressing(I2CX, addr << 1, I2C_TRANSMITTER);
while(!i2c_flag_get(I2CX, I2C_FLAG_ADDSEND));
i2c_flag_clear(I2CX, I2C_FLAG_ADDSEND);
while(!i2c_flag_get(I2CX, I2C_FLAG_TBE));
i2c_data_transmit(I2CX, reg);
while(!i2c_flag_get(I2CX, I2C_FLAG_BTC));
i2c_start_on_bus(I2CX);
while(!i2c_flag_get(I2CX, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2CX, addr << 1, I2C_RECEIVER);
while(!i2c_flag_get(I2CX, I2C_FLAG_ADDSEND));
i2c_flag_clear(I2CX, I2C_FLAG_ADDSEND);
i2c_ackpos_config(I2CX, I2C_ACKPOS_NEXT);
while(len){
if (1 == len){
i2c_ackpos_config(I2CX, I2C_ACKPOS_CURRENT);
i2c_ack_config(I2CX, I2C_ACK_DISABLE);
}
if (i2c_flag_get(I2CX, I2C_FLAG_RBNE)){ // 读到1个字节之后,if语句就再也不能为真了,导致读不到更多内容。
*buf = i2c_data_receive(I2CX);
i2c_ack_config(I2CX, I2C_ACK_ENABLE);
buf++;
len--;
}
}
i2c_stop_on_bus(I2CX);
while(I2C_CTL0(I2CX) & 0x0200);
return 0;
}
使用的开发板为GD32F330C8T6。用GD32F3x0_Firmware_Library_V2.2.1里面的I2C_EEPROM例子程序也是同样的问题,只能读取一个字节,好让人费解啊。请各位不吝赐教,万分感谢。
|