From 1af7a98a5ff72461f117789b66fa1322c3bb8116 Mon Sep 17 00:00:00 2001 From: yelvlab Date: Wed, 8 Jan 2025 00:35:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BB=E5=87=BD=E6=95=B0=E4=B8=AD=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E6=89=A7=E8=A1=8C=EF=BC=8C500ms=E9=97=B4=E9=9A=94?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=B8=A9=E5=BA=A6=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/rs485_protocol.h | 2 +- src/fwdgt.c | 2 +- src/gd60914.c | 20 ++++++++++++++------ src/main.c | 4 +++- src/rs485_protocol.c | 18 +++++++++++++++--- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/inc/rs485_protocol.h b/inc/rs485_protocol.h index b0d2702..baf471e 100644 --- a/inc/rs485_protocol.h +++ b/inc/rs485_protocol.h @@ -49,7 +49,7 @@ validation_result_t validate_package_type(uint8_t* data); validation_result_t validate_data_length(uint8_t* data); - +void gd60914_tempture(void); void ultrasonic_distance_report(void); diff --git a/src/fwdgt.c b/src/fwdgt.c index 688572f..4beebce 100644 --- a/src/fwdgt.c +++ b/src/fwdgt.c @@ -15,7 +15,7 @@ void watchdog_init(void) { rcu_osci_stab_wait(RCU_IRC40K); /* Configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */ - fwdgt_config(625, FWDGT_PSC_DIV256); // Set timeout to 1 seconds (625 / 0.625 KHz) + fwdgt_config(625, FWDGT_PSC_DIV64); // Set timeout to 1 seconds (625 / 0.625 KHz) /* Enable FWDGT */ fwdgt_enable(); diff --git a/src/gd60914.c b/src/gd60914.c index ccdeb21..b9662dc 100644 --- a/src/gd60914.c +++ b/src/gd60914.c @@ -11,23 +11,31 @@ void gd60914_get_object_tempture(void) { i2c_config(); #endif - uint8_t data[2] = {0}; + static uint8_t sensor_validation_data[2]; + extern uint8_t g_temperature_uint8[2]; #ifdef SOFTWARE_IIC soft_i2c_read_16bits(GD60914_ADDR, GD60914_OBJ_TEMP, data); #else - i2c_read_16bits(GD60914_ADDR, GD60914_OBJ_TEMP, data); + i2c_read_16bits(GD60914_ADDR, GD60914_OBJ_TEMP, sensor_validation_data); #endif - delay_ms(300); + if (sensor_validation_data[0] != 0xAA || sensor_validation_data[1] != 0x55) { +#ifdef DEBUG_VERBOES + printf("sensor error\r\n"); +#endif + return; + } + + delay_ms(350); #ifdef SOFTWARE_IIC - soft_i2c_read_16bits(GD60914_ADDR, GD60914_TEMP_REG, data); + soft_i2c_read_16bits(GD60914_ADDR, GD60914_TEMP_REG, g_temperature_uint8); #else - i2c_read_16bits(GD60914_ADDR, GD60914_TEMP_REG, data); + i2c_read_16bits(GD60914_ADDR, GD60914_TEMP_REG, g_temperature_uint8); #endif - printf("%d\r\n", data[1] << 8 | data[0]); + // printf("%d\r\n", g_temperature_uint8[1] << 8 | g_temperature_uint8[0]); } void gd60914_read_temp(void) { diff --git a/src/main.c b/src/main.c index eb6e21c..7bd8ed8 100644 --- a/src/main.c +++ b/src/main.c @@ -6,6 +6,8 @@ */ #include "main.h" +volatile uint8_t g_temperature_uint8[2] = {0}; + extern uint32_t g_capture_value; uint16_t g_distance_uint16; @@ -40,7 +42,7 @@ int main(void) // gpio_bit_write(RS485_EN_PORT, RS485_EN_PIN, RESET); while(1){ // printf("hello world!\r\n"); - delay_ms(500); + // delay_ms(500); gd60914_get_object_tempture(); delay_ms(50); diff --git a/src/rs485_protocol.c b/src/rs485_protocol.c index 165993c..4ffed9e 100644 --- a/src/rs485_protocol.c +++ b/src/rs485_protocol.c @@ -4,6 +4,8 @@ #include "rs485_protocol.h" +extern uint8_t g_temperature_uint8[2]; + void process_command(uint8_t *cmd, size_t length) { char combined_str[3]; validation_result_t validate = VALIDATION_SUCCESS; @@ -18,11 +20,9 @@ void process_command(uint8_t *cmd, size_t length) { // printf("%d", length); sprintf(combined_str, "%c%c", cmd[3], cmd[4]); if (strcmp(combined_str, "M1") == 0) { - ultrasonic_distance_report(); } else if (strcmp(combined_str, "M2") == 0) { - printf("%c%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0x6B, 0xCC); - // tempture_value_report(); + gd60914_tempture(); } else if (strcmp(combined_str, "M3") == 0) { printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC); @@ -130,3 +130,15 @@ void ultrasonic_distance_report(void) { printf("%c%c%c%c", package_data[0], package_data[1], package_data[2], package_data[3]); printf("%c", calculate_crc(combined_data, 8)); } + +void gd60914_tempture(void) { + static uint8_t package_header[3] = {0xB5, 0xF0, 0x02}; + + uint8_t combined_data[5]; + memcpy(combined_data, package_header, 3); + memcpy(combined_data + 3, g_temperature_uint8, 2); + + printf("%c%c%c", package_header[0], package_header[1], package_header[2]); + printf("%c%c", g_temperature_uint8[1], g_temperature_uint8[0]); + printf("%c", calculate_crc(combined_data, 6)); +} \ No newline at end of file