From 4d98e4dc30688d058bdbb7a888dd940c4846f213 Mon Sep 17 00:00:00 2001 From: yelvlab Date: Fri, 27 Dec 2024 00:09:53 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E5=96=82=E7=8B=97=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E4=B8=BA1S=EF=BC=8C=E5=B9=B6=E6=B7=BB=E5=8A=A0M3?= =?UTF-8?q?=E6=8C=87=E4=BB=A4=E4=B8=BA=E5=A4=8D=E4=BD=8D=E6=8C=87=E4=BB=A4?= =?UTF-8?q?=EF=BC=8C=E4=BD=86=E6=98=AF=E5=8F=AF=E8=83=BD=E6=97=A0=E6=95=88?= =?UTF-8?q?=E3=80=82=E4=BF=AE=E6=94=B9=E4=BA=86=E6=95=B0=E6=8D=AE=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=96=B9=E5=BC=8F=EF=BC=8C=E4=BB=8E=E4=B8=80=E7=9B=B4?= =?UTF-8?q?=E6=8C=81=E7=BB=AD=E8=8E=B7=E5=8F=96=E6=94=B9=E4=B8=BA=E6=AF=8F?= =?UTF-8?q?=E6=AC=A1=E7=94=B3=E8=AF=B7=E8=8E=B7=E5=8F=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/i2c.h | 2 +- inc/ldc1612.h | 11 ++++++----- inc/main.h | 2 ++ inc/rs485.h | 3 +++ src/main.c | 24 ++++++++++++++---------- src/rs485.c | 30 +++++++++++++++++++++--------- 6 files changed, 47 insertions(+), 25 deletions(-) diff --git a/inc/i2c.h b/inc/i2c.h index bdb05c9..a871601 100644 --- a/inc/i2c.h +++ b/inc/i2c.h @@ -28,7 +28,7 @@ /******************************************************************************/ -#define I2C_TIME_OUT (uint16_t)(10000) +#define I2C_TIME_OUT (uint16_t)(5000) #define I2C_OK 1 #define I2C_FAIL 0 #define I2C_END 1 diff --git a/inc/ldc1612.h b/inc/ldc1612.h index 6722785..2e0ada9 100644 --- a/inc/ldc1612.h +++ b/inc/ldc1612.h @@ -43,12 +43,13 @@ /******************************************************************************/ -#define LDC1612_CONVERSION_TIME_CH0 0X0546 //0536 -#define LDC1612_DRIVE_CURRENT 0X9000 //A000 -#define LDC1612_MUX_CONFIG 0X020C // no auto scan and filter bandwidth 3.3MHz -#define LDC1612_SENSOR_CONFIG 0X1601 +#define LDC1612_CONVERSION_TIME_CH0 0x0546 //0536 +#define LDC1612_DRIVE_CURRENT 0x9000 //A000 +#define LDC1612_MUX_CONFIG 0x020C // no auto scan and filter bandwidth 3.3MHz +#define LDC1612_SENSOR_CONFIG 0x1601 #define LDC1612_ERROR_CONFIG 0x0000 -#define LC_STABILIZE_TIME_CH0 0X001E //30 +#define LC_STABILIZE_TIME_CH0 0x001E //30 +#define LDC1612_RESET_DEV 0x8000 //[15:0] 0b1000 0000 0000 0000 /******************************************************************************/ diff --git a/inc/main.h b/inc/main.h index c79017e..88b766c 100644 --- a/inc/main.h +++ b/inc/main.h @@ -39,4 +39,6 @@ OF SUCH DAMAGE. void watchdog_init(void); +void reset_mcu(void); + #endif /* MAIN_H */ diff --git a/inc/rs485.h b/inc/rs485.h index b24f233..dbc5b94 100644 --- a/inc/rs485.h +++ b/inc/rs485.h @@ -13,6 +13,9 @@ #include #include #include +#include "i2c.h" +#include "ldc1612.h" +#include "tmp112.h" /******************************************************************************/ diff --git a/src/main.c b/src/main.c index 0e13bfe..69f8786 100644 --- a/src/main.c +++ b/src/main.c @@ -23,16 +23,24 @@ void watchdog_init(void) { rcu_osci_on(RCU_IRC40K); rcu_osci_stab_wait(RCU_IRC40K); - /* Enable the FWDGT clock */ - // rcu_periph_clock_enable(RCU_FWDGT); - - /* Configure FWDGT counter clock: 40KHz(IRC40K) / 256 = 0.15625 KHz */ - fwdgt_config(625, FWDGT_PSC_DIV256); // Set timeout to 4 seconds (625 / 0.15625 KHz) + /* Configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */ + fwdgt_config(625, FWDGT_PSC_DIV64); // Set timeout to 1 seconds (625 / 0.625 KHz) /* Enable FWDGT */ fwdgt_enable(); } +void reset_mcu(void) { + /* Enable the write access to the FWDGT_CTL register */ + FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE; + + /* Configure FWDGT to trigger a system reset */ + fwdgt_config(5, FWDGT_PSC_DIV4); + + /* Reload the counter to trigger the reset */ + fwdgt_counter_reload(); +} + /*! \brief main function \param[in] none @@ -57,14 +65,10 @@ int main(void) { /* Initialize watchdog */ watchdog_init(); + printf("Hello, world!\n"); while (1) { delay_ms(99); - g_eddy_current_value_uint32 = 0; - g_temperature_uint32 = 0; - g_eddy_current_value_uint32 = ldc1612_get_raw_channel_result(CHANNEL_0); - g_temperature_uint32 = tmp112a_get_raw_channel_result(); - // printf("1"); fwdgt_counter_reload(); } } diff --git a/src/rs485.c b/src/rs485.c index b578061..a1dbbe7 100644 --- a/src/rs485.c +++ b/src/rs485.c @@ -53,8 +53,12 @@ void process_command(uint8_t *cmd, size_t length) { sprintf(combined_str, "%c%c", cmd[3], cmd[4]); if (strcmp(combined_str, "M1") == 0) { eddy_current_value_report(); - } else if (strcmp(combined_str, "M2") == 0) { + } else if (strcmp(combined_str, "M2") == 0) + { tempture_value_report(); + } else if (strcmp(combined_str, "M3") == 0) + { + reset_mcu(); } else { printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C); return; @@ -120,10 +124,14 @@ validation_result_t validate_data_length(uint8_t *data) { } void eddy_current_value_report(void) { - package_data[0] = (g_eddy_current_value_uint32 >> 24) & 0xFF; - package_data[1] = (g_eddy_current_value_uint32 >> 16) & 0xFF; - package_data[2] = (g_eddy_current_value_uint32 >> 8) & 0xFF; - package_data[3] = g_eddy_current_value_uint32 & 0xFF; + static uint32_t eddy_current_value_uint32 = 0; + + eddy_current_value_uint32 = ldc1612_get_raw_channel_result(CHANNEL_0); + + package_data[0] = (eddy_current_value_uint32 >> 24) & 0xFF; + package_data[1] = (eddy_current_value_uint32 >> 16) & 0xFF; + package_data[2] = (eddy_current_value_uint32 >> 8) & 0xFF; + package_data[3] = eddy_current_value_uint32 & 0xFF; uint8_t combined_data[7]; memcpy(combined_data, package_header, 3); @@ -135,10 +143,14 @@ void eddy_current_value_report(void) { } void tempture_value_report(void) { - package_data[0] = (g_temperature_uint32 >> 24) & 0xFF; - package_data[1] = (g_temperature_uint32 >> 16) & 0xFF; - package_data[2] = (g_temperature_uint32 >> 8) & 0xFF; - package_data[3] = g_temperature_uint32 & 0xFF; + static uint32_t temperature_uint32 = 0; + + temperature_uint32 = tmp112a_get_raw_channel_result(); + + package_data[0] = (temperature_uint32 >> 24) & 0xFF; + package_data[1] = (temperature_uint32 >> 16) & 0xFF; + package_data[2] = (temperature_uint32 >> 8) & 0xFF; + package_data[3] = temperature_uint32 & 0xFF; uint8_t combined_data[7]; memcpy(combined_data, package_header, 3);