diff --git a/inc/LDC1612.h b/inc/LDC1612.h index 0b0bb49..96172f0 100644 --- a/inc/LDC1612.h +++ b/inc/LDC1612.h @@ -14,6 +14,7 @@ #include #define LDC1612_ADDR (0x2B << 1) +#define TMP112A_ADDR (0x49 << 1) /*Register Rddr*/ /***************************************************************************/ @@ -57,9 +58,9 @@ typedef enum { void I2C_config(void); void I2C_scan(void); -uint8_t IIC_read_16bit(uint8_t reg, uint16_t *value); -void LDC1612_read_sensor_infomation(void); - +// uint8_t IIC_read_16bit(uint8_t reg, uint16_t *value); +// void LDC1612_read_sensor_infomation(void); +uint32_t TMP112A_ReadTemperature(void); #endif //LDC1612_H diff --git a/src/LDC1612.c b/src/LDC1612.c index 075dec9..d1ceb2c 100644 --- a/src/LDC1612.c +++ b/src/LDC1612.c @@ -21,21 +21,21 @@ void I2C_config(void) { i2c_clock_config(I2C0, I2C_SPEED, I2C_DTCY_2); - // i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, LDC1612_ADDR); + i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, 0x72); i2c_enable(I2C0); i2c_ack_config(I2C0, I2C_ACK_ENABLE); } -void LDC1612_Init(void) { - uint8_t RCOUNT0_ALL[3]={SET_CONVERSION_TIME_REG_START,0x05,0x36};//csdn - uint8_t SETTLECOUNT0_ALL[3]={SET_LC_STABILIZE_REG_START,0x00,0x0a}; - uint8_t CLOCK_DIVIDERS0_ALL[3]={SET_FREQ_REG_START,0x10,0x02}; - uint8_t ERROR_CONFIG_ALL[3]={ERROR_CONFIG_REG,0x00,0x00}; - uint8_t MUX_CONFIG_ALL[3]={MUL_CONFIG_REG,0x82,0x0c}; - uint8_t DRIVE_CURRENT0_ALL[3]={SET_DRIVER_CURRENT_REG,0x90,0x00}; - uint8_t CONFIG_ALL[3]={SENSOR_CONFIG_REG,0x14,0x01};//csdn - -} +// void LDC1612_Init(void) { +// uint8_t RCOUNT0_ALL[3]={SET_CONVERSION_TIME_REG_START,0x05,0x36};//csdn +// uint8_t SETTLECOUNT0_ALL[3]={SET_LC_STABILIZE_REG_START,0x00,0x0a}; +// uint8_t CLOCK_DIVIDERS0_ALL[3]={SET_FREQ_REG_START,0x10,0x02}; +// uint8_t ERROR_CONFIG_ALL[3]={ERROR_CONFIG_REG,0x00,0x00}; +// uint8_t MUX_CONFIG_ALL[3]={MUL_CONFIG_REG,0x82,0x0c}; +// uint8_t DRIVE_CURRENT0_ALL[3]={SET_DRIVER_CURRENT_REG,0x90,0x00}; +// uint8_t CONFIG_ALL[3]={SENSOR_CONFIG_REG,0x14,0x01};//csdn +// +// } void I2C_scan(void) { @@ -91,95 +91,112 @@ void I2C_scan(void) { } } +uint32_t TMP112A_ReadTemperature(void) { + uint8_t data[2] = {0}; + uint16_t raw_temp = 0; + uint16_t timeout = 0; + uint32_t temperature = 0; -/** - * @brief 读取16位I2C数据,带有超时机制 - * @param reg: 要读取的寄存器地址 - * @param value: 存储读取数据的指针 - * @retval 0: 成功,1: 超时 - */ -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; - while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) { - if ((timeout++) > I2C_TIME_OUT) return 1; // 超时 + 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; // 超时返回错误 } - // 发送从设备地址和写信号 - i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_TRANSMITTER); - timeout = 0; - while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) { - if ((timeout++) > I2C_TIME_OUT) return 2; // 超时 - } - i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND); - - // 发送寄存器地址 - i2c_data_transmit(I2C0, reg); - timeout = 0; - while (!i2c_flag_get(I2C0, I2C_FLAG_TBE)) { - if ((timeout++) > I2C_TIME_OUT) return 3; // 超时 + 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; // 超时返回错误 } - // 重新启动信号 - i2c_start_on_bus(I2C0); - timeout = 0; - while (!i2c_flag_get(I2C0, I2C_FLAG_SBSEND)) { - if ((timeout++) > I2C_TIME_OUT) return 4; // 超时 + 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; // 超时返回错误 } - // 发送从设备地址和读信号 - i2c_master_addressing(I2C0, LDC1612_ADDR, I2C_RECEIVER); - timeout = 0; - while (!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)) { - if ((timeout++) > I2C_TIME_OUT) return 5; // 超时 + 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; // 超时返回错误 } - i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND); - // 读取数据 - timeout = 0; - while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) { - if ((timeout++) > I2C_TIME_OUT) return 6; // 超时 + 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; // 超时返回错误 } - *value = i2c_data_receive(I2C0) << 8; // 读取高8位 - timeout = 0; - while (!i2c_flag_get(I2C0, I2C_FLAG_RBNE)) { - if ((timeout++) > I2C_TIME_OUT) return 7; // 超时 + 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; // 超时返回错误 } - *value |= i2c_data_receive(I2C0); // 读取低8位 - // 发送停止信号 i2c_stop_on_bus(I2C0); - timeout = 0; - return 0; // 成功 + // 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; } - -void LDC1612_read_sensor_infomation(void) -{ - uint16_t device_id; - uint16_t manufacturer_id; - uint8_t result; - - // 读取设备ID - result = IIC_read_16bit(READ_DEVICE_ID, &device_id); - - if (result == 0) { - printf("Device ID: 0x%04X\n", device_id); - } else { - printf("Failed to read Device 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 diff --git a/src/RS485.c b/src/RS485.c index 2db76b7..557f9c9 100644 --- a/src/RS485.c +++ b/src/RS485.c @@ -4,6 +4,8 @@ #include "RS485.h" +extern uint32_t g_temperature_uint32; + void RS485_config(void) { rcu_periph_clock_enable(RS485_GPIO_RCU); rcu_periph_clock_enable(RS485_RCU); @@ -35,7 +37,8 @@ void process_command(char *cmd) { if (strncmp(cmd, "M1", 2) == 0) { printf("M1 -=-=- OK!\r\n"); } else if (strncmp(cmd, "M2", 2) == 0) { - printf("M2 -=-=- OK!\r\n"); + // printf("M2 -=-=- OK!\r\n"); + printf("Temperature: %lu\r\n", g_temperature_uint32); // } else if (strncmp(cmd, "M3", 2) == 0) { // char *param_str = cmd + 2; // Skip "M3" // int param = atoi(param_str + 1); // Skip "S" and convert to integer diff --git a/src/gd32e23x_it.c b/src/gd32e23x_it.c index 12b5a1e..9a85154 100644 --- a/src/gd32e23x_it.c +++ b/src/gd32e23x_it.c @@ -36,6 +36,10 @@ OF SUCH DAMAGE. #include "main.h" #include "systick.h" #include "LDC1612.h" +#include "RS485.h" + +char rx_buffer[RX_BUFFER_SIZE]; +uint8_t rx_index = 0; /*! \brief this function handles NMI exception @@ -140,4 +144,24 @@ void TIMER16_IRQHandler(void) { } led_status = !led_status; } +} + +void USART0_IRQHandler(void) { + if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) + { + usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE); + uint8_t received_data = (uint8_t)usart_data_receive(USART0); + + // 将接收到的数据存储到缓冲区 + if(rx_index < RX_BUFFER_SIZE - 1) { + rx_buffer[rx_index++] = received_data; + } + + // 检查是否接收到换行符,表示指令结束 + if(received_data == '\n') { + rx_buffer[rx_index] = '\0'; // 添加字符串结束符 + process_command(rx_buffer); // 处理指令 + rx_index = 0; // 重置缓冲区索引 + } + } } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 7c816e7..2b9796b 100644 --- a/src/main.c +++ b/src/main.c @@ -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); @@ -59,14 +61,11 @@ int main(void) printf("XLSW-3DP-LDC1612! V0.0.1\r\n"); printf("\r\n"); - I2C_scan(); - LDC1612_read_sensor_infomation(); - - - - + // I2C_scan(); while(1){ + g_temperature_uint32 = TMP112A_ReadTemperature(); + delay_ms(500); } }