From e0b9aab10338e01f3e9669d2eda945d86c818465 Mon Sep 17 00:00:00 2001 From: yelvlab Date: Sat, 7 Dec 2024 16:30:43 +0800 Subject: [PATCH] fix IIC speed --- inc/LDC1612.h | 2 +- src/LDC1612.c | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/inc/LDC1612.h b/inc/LDC1612.h index 0b0bb49..d31f3aa 100644 --- a/inc/LDC1612.h +++ b/inc/LDC1612.h @@ -44,7 +44,7 @@ typedef enum { I2C_STOP } i2c_process_enum; -#define I2C_SPEED 400000 +#define I2C_SPEED 100000 #define RCU_IR_GPIO RCU_GPIOF #define RCU_I2C RCU_I2C0 #define I2C_SCL_PORT GPIOF diff --git a/src/LDC1612.c b/src/LDC1612.c index 075dec9..d9093e5 100644 --- a/src/LDC1612.c +++ b/src/LDC1612.c @@ -101,6 +101,8 @@ void I2C_scan(void) { uint8_t IIC_read_16bit(uint8_t reg, uint16_t *value) { uint32_t timeout = 0; + i2c_ack_config(I2C0, I2C_ACK_ENABLE); + // 发送启动信号 i2c_start_on_bus(I2C0); timeout = 0; @@ -129,7 +131,7 @@ uint8_t IIC_read_16bit(uint8_t reg, uint16_t *value) { while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) { if ((timeout++) > I2C_TIME_OUT) return 4; // 超时 } - + printf("test1\n"); // 发送从设备地址和读信号 i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_RECEIVER); timeout = 0; @@ -145,6 +147,8 @@ uint8_t IIC_read_16bit(uint8_t reg, uint16_t *value) { } *value = i2c_data_receive(I2C0) << 8; // 读取高8位 + // i2c_ack_config(I2C0, I2C_ACK_DISABLE); // 关闭发送ACK,它会在下一个字节完成后发送NAK + timeout = 0; while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) { if ((timeout++) > I2C_TIME_OUT) return 7; // 超时 @@ -174,12 +178,12 @@ void LDC1612_read_sensor_infomation(void) printf("result: %d\n", result); } - result = IIC_read_16bit(READ_MANUFACTURER_ID, &manufacturer_id); - - if (result == 0) { - printf("manufacturer ID: 0x%04X\n", manufacturer_id); - } else { - printf("Failed to read manufacturer ID\n"); - printf("result: %d\n", result); - } + // result = IIC_read_16bit(READ_MANUFACTURER_ID, &manufacturer_id); + // + // if (result == 0) { + // printf("manufacturer ID: 0x%04X\n", manufacturer_id); + // } else { + // printf("Failed to read manufacturer ID\n"); + // printf("result: %d\n", result); + // } } \ No newline at end of file