generated from hulk/gd32e23x_template
finish tmp112a
This commit is contained in:
parent
0910a26072
commit
de211abf4e
@ -60,4 +60,6 @@ void I2C_scan(void);
|
||||
|
||||
int LDC1612_IIC_read_16bits(void);
|
||||
|
||||
uint32_t TMP112A_ReadTemperature(void);
|
||||
|
||||
#endif //LDC1612_H
|
||||
|
110
src/LDC1612.c
110
src/LDC1612.c
@ -185,4 +185,114 @@ int LDC1612_IIC_read_16bits(void) {
|
||||
|
||||
printf("device id = %x\r\n", (data[0] <<8 | data[1]));
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t TMP112A_ReadTemperature(void) {
|
||||
uint8_t data[2] = {0};
|
||||
uint16_t raw_temp = 0;
|
||||
uint16_t timeout = 0;
|
||||
uint32_t temperature = 0;
|
||||
|
||||
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
|
||||
|
||||
while (i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) //判断IIC总线是否忙,发送起始信号
|
||||
timeout++;
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
i2c_start_on_bus(I2C0);
|
||||
timeout = 0;
|
||||
} else {
|
||||
printf("err\r\n");
|
||||
return -1; // 超时返回错误
|
||||
}
|
||||
|
||||
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) //判断起始位是否发送,设置sensor地址并设置为写
|
||||
timeout++;
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
i2c_master_addressing(I2C0, TMP112A_ADDR, I2C_TRANSMITTER);
|
||||
timeout = 0;
|
||||
} else {
|
||||
return -1; // 超时返回错误
|
||||
}
|
||||
|
||||
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
||||
timeout++;
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
timeout = 0;
|
||||
} else {
|
||||
return -1; // 超时返回错误
|
||||
}
|
||||
|
||||
while (!i2c_flag_get(I2C0, I2C_FLAG_TBE) && (timeout < I2C_TIME_OUT)) //判断地址是否发送完成,然后发送寄存器地址
|
||||
timeout++;
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
i2c_data_transmit(I2C0, 0x00);
|
||||
timeout = 0;
|
||||
// i2c_start_on_bus(I2C0);
|
||||
} else {
|
||||
return -1; // 超时返回错误
|
||||
}
|
||||
|
||||
while (i2c_flag_get(I2C0, I2C_FLAG_BTC) && (timeout < I2C_TIME_OUT)) //判断发送缓冲器是否为空,为空后(发送完毕)重新发送开始信号
|
||||
timeout++;
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
i2c_start_on_bus(I2C0);
|
||||
timeout = 0;
|
||||
} else {
|
||||
return -1; // 超时返回错误
|
||||
}
|
||||
|
||||
while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
i2c_master_addressing(I2C0, TMP112A_ADDR, I2C_RECEIVER);
|
||||
timeout = 0;
|
||||
} else {
|
||||
return -1; // 超时返回错误
|
||||
}
|
||||
|
||||
while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) && (timeout < I2C_TIME_OUT))
|
||||
timeout++;
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
|
||||
timeout = 0;
|
||||
} else {
|
||||
return -1; // 超时返回错误
|
||||
}
|
||||
|
||||
// 读取第一个字节的数据
|
||||
while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE) && (timeout < I2C_TIME_OUT)) {
|
||||
timeout++;
|
||||
}
|
||||
if (timeout < I2C_TIME_OUT) {
|
||||
data[0] = i2c_data_receive(I2C0);
|
||||
timeout = 0;
|
||||
} else {
|
||||
return -1; // 超时返回错误
|
||||
}
|
||||
|
||||
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);
|
||||
timeout = 0;
|
||||
} else {
|
||||
return -1; // 超时返回错误
|
||||
}
|
||||
|
||||
i2c_stop_on_bus(I2C0);
|
||||
|
||||
// printf("data[0]: %x, data[1]: %x\r\n", data[0], data[1]);
|
||||
raw_temp = ((uint16_t) (data[0] << 4) | (data[1]>>4));
|
||||
// printf("raw_temp: %x\r\n", raw_temp);
|
||||
// printf("raw_temp(dec): %d\r\n", raw_temp);
|
||||
temperature = raw_temp * 625;
|
||||
// printf("temperature: %d\r\n", temperature);
|
||||
|
||||
return temperature;
|
||||
}
|
@ -12,6 +12,8 @@
|
||||
#include "RS485.h"
|
||||
#include "LDC1612.h"
|
||||
|
||||
uint32_t g_temperature_uint32;
|
||||
|
||||
void led_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(LED_RCU);
|
||||
@ -64,6 +66,8 @@ int main(void)
|
||||
delay_ms(1000);
|
||||
|
||||
while(1){
|
||||
g_temperature_uint32 = TMP112A_ReadTemperature();
|
||||
delay_ms(500);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user