From f5a936e022c8a10b6243b1f732c71e6c0ca24495 Mon Sep 17 00:00:00 2001 From: yelvlab Date: Tue, 3 Dec 2024 15:34:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0485=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E7=89=B9=E5=AE=9A=E5=91=BD=E4=BB=A4=E6=88=96=E8=80=85=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E5=8A=A0=E5=8F=82=E6=95=B0=E7=9A=84=E5=BD=A2=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/RS485.h | 5 +++++ src/RS485.c | 34 ++++++++++++++++++++-------------- src/gd32e23x_it.c | 25 +++++++++++++++++++++++++ src/ultrasonic_driver.c | 3 +++ 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/inc/RS485.h b/inc/RS485.h index ee2dbd0..a484aae 100644 --- a/inc/RS485.h +++ b/inc/RS485.h @@ -5,7 +5,12 @@ #ifndef RS485_H #define RS485_H +#include "gd32e23x_it.h" #include #include +#define RX_BUFFER_SIZE 64 + +void process_command(char *cmd); + #endif //RS485_H diff --git a/src/RS485.c b/src/RS485.c index 16d6164..afccdb5 100644 --- a/src/RS485.c +++ b/src/RS485.c @@ -11,19 +11,25 @@ #define MAX_CMD_SIZE 16 #define BUFSIZE 8 -static char cmdbuffer[BUFSIZE][MAX_CMD_SIZE]; -static char *strchr_pointer = NULL; +extern uint16_t g_distance_uint16; +extern uint16_t g_temperature_uint16; -static int bufindr = 0; -static int bufindw = 0; -static int buflen = 0; - -bool code_seen(char code) -{ - strchr_pointer = strchr(cmdbuffer[bufindr], code); - return (strchr_pointer != NULL); //Return True if a character was found -} -float code_value(void) -{ - return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL)); +void process_command(char *cmd) { + if (strncmp(cmd, "M1", 2) == 0) { + printf("M1 -=-=- OK!\r\n"); + printf("Distance: %d\r\n", g_distance_uint16); + } else if (strncmp(cmd, "M2", 2) == 0) { + printf("M2 -=-=- OK!\r\n"); + printf("Temperature: %d\r\n", g_temperature_uint16); +// } 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 +// if (param >= 0 && param <= 100) { +// printf("M3 with parameter %d -=-=- OK!\r\n", param); +// } else { +// printf("Invalid parameter for M3 command!\r\n"); +// } + } else { + printf("Invalid Command!\r\n"); + } } \ No newline at end of file diff --git a/src/gd32e23x_it.c b/src/gd32e23x_it.c index a9af911..d02df1c 100644 --- a/src/gd32e23x_it.c +++ b/src/gd32e23x_it.c @@ -37,9 +37,13 @@ OF SUCH DAMAGE. #include "main.h" #include "systick.h" #include "ultrasonic_driver.h" +#include "rs485.h" __IO uint32_t g_capture_value; +char rx_buffer[RX_BUFFER_SIZE]; +uint8_t rx_index = 0; + /*! \brief this function handles NMI exception \param[in] none @@ -62,6 +66,7 @@ void NMI_Handler(void) void HardFault_Handler(void) { /* if Hard Fault exception occurs, go to infinite loop */ + // log_error("HardFault_Handler"); while(1) { } } @@ -156,4 +161,24 @@ void EXTI0_1_IRQHandler(void) { timer_disable(TIMER16); exti_interrupt_disable(EXTI_0); } +} + +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/ultrasonic_driver.c b/src/ultrasonic_driver.c index bc7c7ae..3172129 100644 --- a/src/ultrasonic_driver.c +++ b/src/ultrasonic_driver.c @@ -57,6 +57,9 @@ void usart_config(void) usart_transmit_config(USART0_PHY, USART_TRANSMIT_ENABLE); usart_enable(USART0_PHY); + + nvic_irq_enable(USART0_IRQn, 0); + usart_interrupt_enable(USART0_PHY, USART_INT_RBNE); } void UltraSonic_GPIO_Config(void)