From dbff482cf37be2aec4a0c84eaca9355c1d81d217 Mon Sep 17 00:00:00 2001 From: yelvlab Date: Mon, 16 Dec 2024 11:41:26 +0800 Subject: [PATCH] =?UTF-8?q?LDC1612=E5=8D=95=E6=AC=A1=E8=AF=BB=E5=8F=9616bi?= =?UTF-8?q?t=E6=AD=A3=E5=B8=B8=EF=BC=8C=E5=8F=AF=E4=BB=A5=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=8E=82=E5=95=86ID=E6=88=96=E8=80=85=E8=AE=BE?= =?UTF-8?q?=E5=A4=87ID=EF=BC=8C=E4=BD=86=E6=98=AF=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E8=BF=9E=E7=BB=AD=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/LDC1612.h | 2 +- src/LDC1612.c | 16 ++++++++-------- src/main.c | 22 +++++++++++++++++----- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/inc/LDC1612.h b/inc/LDC1612.h index 0ba7392..58a5b72 100644 --- a/inc/LDC1612.h +++ b/inc/LDC1612.h @@ -58,6 +58,6 @@ typedef enum { void I2C_config(void); void I2C_scan(void); -int LDC1612_IIC_read_16bits(void); +int LDC1612_IIC_read_16bits(uint8_t reg, uint16_t *data); #endif //LDC1612_H diff --git a/src/LDC1612.c b/src/LDC1612.c index b14e252..8344db1 100644 --- a/src/LDC1612.c +++ b/src/LDC1612.c @@ -84,9 +84,7 @@ void I2C_scan(void) { } } -int LDC1612_IIC_read_16bits(void) { - uint8_t data[2] = {0}; - uint16_t raw_temp = 0; +int LDC1612_IIC_read_16bits(uint8_t reg, uint16_t *data) { uint16_t timeout = 0; i2c_ack_config(I2C0, I2C_ACK_ENABLE); @@ -122,13 +120,15 @@ int LDC1612_IIC_read_16bits(void) { while (!i2c_flag_get(I2C0, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT)) //判断地址是否发送完成,然后发送寄存器地址 timeout++; if (timeout < I2C_TIME_OUT) { - i2c_data_transmit(I2C0, READ_DEVICE_ID); + i2c_data_transmit(I2C0, reg); timeout = 0; // i2c_start_on_bus(I2C0); } else { return -4; // 超时返回错误 } + delay_us(10); + while (i2c_flag_get(I2C0, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) //判断发送缓冲器是否为空,为空后(发送完毕)重新发送开始信号 timeout++; if (timeout < I2C_TIME_OUT) { @@ -162,20 +162,20 @@ int LDC1612_IIC_read_16bits(void) { timeout++; } if (timeout < I2C_TIME_OUT) { - data[0] = i2c_data_receive(I2C0); + *data = i2c_data_receive(I2C0) << 8; timeout = 0; } else { return -8; // 超时返回错误 } - // i2c_ack_config(I2C0, I2C_ACK_DISABLE); // 关闭发送ACK,它会在下一个字节完成后发送NAK + i2c_ack_config(I2C0, I2C_ACK_DISABLE); // 关闭发送ACK,它会在下一个字节完成后发送NAK // 读取第二个字节的数据 while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE) && (timeout < I2C_TIME_OUT)) { timeout++; } if (timeout < I2C_TIME_OUT) { - data[1] = i2c_data_receive(I2C0); + *data |= i2c_data_receive(I2C0); timeout = 0; } else { return -9; // 超时返回错误 @@ -183,6 +183,6 @@ int LDC1612_IIC_read_16bits(void) { i2c_stop_on_bus(I2C0); - printf("device id = %x\r\n", (data[0] <<8 | data[1])); + // printf("device id = %x\r\n", (data[0] <<8 | data[1])); return 0; } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 1d6c352..12eeb40 100644 --- a/src/main.c +++ b/src/main.c @@ -46,8 +46,7 @@ void led_config(void) \param[out] none \retval none */ -int main(void) -{ +int main(void) { /* configure systick */ systick_config(); RS485_config(); @@ -58,12 +57,25 @@ int main(void) printf("XLSW-3DP-LDC1612! V0.0.1\r\n"); printf("\r\n"); + delay_ms(500); - // I2C_scan(); + // int result = 0; + uint16_t data = 0; + uint16_t device_id = 0; + LDC1612_IIC_read_16bits(READ_DEVICE_ID, &data); + // printf("result = %d\r\n", result); - delay_ms(1000); - while(1){ + // delay_ms(1000); + + + LDC1612_IIC_read_16bits(READ_DEVICE_ID, &device_id); + printf("Manufacturer: 0x%x\r\n", data); + printf("Device: 0x%x\r\n", device_id); + + while (1) { + delay_ms(1000); + printf("OK!!!\r\n"); } }