generated from hulk/gd32e23x_template
LDC1612单次读取16bit正常,可以获取厂商ID或者设备ID,但是无法连续获取
This commit is contained in:
parent
3f961a6a3e
commit
dbff482cf3
@ -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
|
||||
|
@ -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;
|
||||
}
|
22
src/main.c
22
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");
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user