generated from hulk/gd32e23x_template_cmake_vscode
IIC OK but sensor error
This commit is contained in:
@@ -59,13 +59,13 @@ void soft_i2c_start(void) {
|
||||
\retval none
|
||||
*/
|
||||
void soft_i2c_stop(void) {
|
||||
// sda_out();
|
||||
I2C_SCL_LOW();
|
||||
I2C_SDA_LOW();
|
||||
I2C_SCL_LOW(); // 确保时钟为低
|
||||
I2C_SDA_LOW(); // 拉低数据线
|
||||
soft_i2c_delay();
|
||||
I2C_SCL_HIGH();
|
||||
I2C_SCL_HIGH(); // 拉高时钟
|
||||
soft_i2c_delay();
|
||||
I2C_SDA_HIGH();
|
||||
I2C_SDA_HIGH(); // 在时钟高电平时拉高数据线产生停止条件
|
||||
soft_i2c_delay(); // 添加缺失的延时
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -108,12 +108,13 @@ void soft_i2c_send_nack(void) {
|
||||
\retval 0: ACK received, 1: ACK not received
|
||||
*/
|
||||
uint8_t soft_i2c_wait_ack(void) {
|
||||
I2C_SDA_HIGH();
|
||||
I2C_SDA_HIGH(); // 释放SDA线,让从设备控制
|
||||
soft_i2c_delay();
|
||||
I2C_SCL_HIGH();
|
||||
I2C_SCL_HIGH(); // 拉高时钟
|
||||
soft_i2c_delay();
|
||||
uint8_t ack = !I2C_SDA_READ();
|
||||
I2C_SCL_LOW();
|
||||
uint8_t ack = !I2C_SDA_READ(); // 读取ACK信号(低电平为ACK)
|
||||
I2C_SCL_LOW(); // 拉低时钟
|
||||
soft_i2c_delay(); // 添加缺失的延时
|
||||
return ack;
|
||||
}
|
||||
|
||||
@@ -168,8 +169,13 @@ uint8_t soft_i2c_receive_byte(uint8_t ack) {
|
||||
}
|
||||
|
||||
uint8_t soft_i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data[2]) {
|
||||
/* 参数验证 */
|
||||
if (data == NULL || slave_addr > 0x7F) {
|
||||
return SOFT_I2C_FAIL;
|
||||
}
|
||||
|
||||
soft_i2c_start();
|
||||
soft_i2c_send_byte(slave_addr);
|
||||
soft_i2c_send_byte(slave_addr << 1); // 修复:左移1位,添加写位
|
||||
if (!soft_i2c_wait_ack()) {
|
||||
soft_i2c_stop();
|
||||
return SOFT_I2C_FAIL;
|
||||
@@ -185,15 +191,24 @@ uint8_t soft_i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data
|
||||
return SOFT_I2C_FAIL;
|
||||
}
|
||||
soft_i2c_send_byte(data[1]);
|
||||
if (soft_i2c_wait_ack()){}
|
||||
if (!soft_i2c_wait_ack()) { // 修复:添加错误处理
|
||||
soft_i2c_stop();
|
||||
return SOFT_I2C_FAIL;
|
||||
}
|
||||
soft_i2c_stop();
|
||||
return SOFT_I2C_OK;
|
||||
}
|
||||
|
||||
uint8_t soft_i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data)
|
||||
{
|
||||
/* 参数验证 */
|
||||
if (data == NULL || slave_addr > 0x7F) {
|
||||
return SOFT_I2C_FAIL;
|
||||
}
|
||||
|
||||
/* 写阶段:发送寄存器地址 */
|
||||
soft_i2c_start();
|
||||
soft_i2c_send_byte(slave_addr);
|
||||
soft_i2c_send_byte(slave_addr << 1); // 修复:左移1位,写操作
|
||||
if (!soft_i2c_wait_ack()) {
|
||||
soft_i2c_stop();
|
||||
return SOFT_I2C_FAIL;
|
||||
@@ -203,15 +218,17 @@ uint8_t soft_i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data
|
||||
soft_i2c_stop();
|
||||
return SOFT_I2C_FAIL;
|
||||
}
|
||||
soft_i2c_start();
|
||||
soft_i2c_send_byte(slave_addr | 0x01);
|
||||
|
||||
/* 读阶段:重新开始并读取数据 */
|
||||
soft_i2c_start(); // 重新开始
|
||||
soft_i2c_send_byte((slave_addr << 1) | 0x01); // 修复:正确的读地址
|
||||
if (!soft_i2c_wait_ack()) {
|
||||
soft_i2c_stop();
|
||||
return SOFT_I2C_FAIL;
|
||||
}
|
||||
soft_i2c_delay();
|
||||
data[0] = soft_i2c_receive_byte(1);
|
||||
data[1] = soft_i2c_receive_byte(0);
|
||||
data[0] = soft_i2c_receive_byte(1); // 第一个字节发送ACK
|
||||
data[1] = soft_i2c_receive_byte(0); // 最后一个字节发送NACK
|
||||
soft_i2c_stop();
|
||||
return SOFT_I2C_OK;
|
||||
}
|
Reference in New Issue
Block a user