From 834865242582dc5ef330bfd0e9456dc89961d5f9 Mon Sep 17 00:00:00 2001 From: yelvlab Date: Tue, 3 Jun 2025 00:06:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=B2=E7=BB=8F=E6=8C=89=E7=85=A7=E4=BE=8B?= =?UTF-8?q?=E7=A8=8B=E5=AE=9E=E7=8E=B0=E5=9F=BA=E6=9C=AC=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E4=BD=86=E6=98=AF=E4=B8=BA=E4=BB=80=E4=B9=88?= =?UTF-8?q?=E4=B8=B2=E5=8F=A3=E8=BD=AC=E5=8F=91=E7=9A=84=E6=98=AF=E5=89=8D?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/rs485.h | 7 +++++++ src/gd32e23x_it.c | 13 +++++++------ src/main.c | 19 ++++++++++++++++--- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/inc/rs485.h b/inc/rs485.h index 3200d5e..a6366f3 100644 --- a/inc/rs485.h +++ b/inc/rs485.h @@ -9,6 +9,7 @@ #include "gd32e23x.h" #include "systick.h" #include +#include #include #include "ldc1612.h" #include "tmp112.h" @@ -36,6 +37,12 @@ typedef enum /******************************************************************************/ +uint8_t Command_Write(uint8_t *data, uint8_t length); + +uint8_t Command_GetCommand(uint8_t *command); + +/******************************************************************************/ + void rs485_config(void); void process_command(uint8_t* cmd, size_t length); diff --git a/src/gd32e23x_it.c b/src/gd32e23x_it.c index 6893a93..24ab88e 100644 --- a/src/gd32e23x_it.c +++ b/src/gd32e23x_it.c @@ -39,6 +39,8 @@ OF SUCH DAMAGE. #include "rs485.h" #include "led.h" +extern uint8_t readBuffer[16]; + /*! \brief this function handles NMI exception \param[in] none @@ -119,22 +121,21 @@ void TIMER16_IRQHandler(void) { void USART0_IRQHandler(void) { static uint8_t rx_index = 0; - static uint8_t rx_buffer[RX_BUFFER_SIZE]; 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); + // 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 (rx_index < sizeof(readBuffer) - 1) { + readBuffer[rx_index++] = usart_data_receive(USART0); } } if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) { usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE); - process_command(rx_buffer, rx_index); // 处理指令 + Command_Write(readBuffer, rx_index);// 处理指令 rx_index = 0; // 重置缓冲区索引 return; diff --git a/src/main.c b/src/main.c index 6c6b0d8..a342d50 100644 --- a/src/main.c +++ b/src/main.c @@ -8,6 +8,8 @@ bool g_statusSwitch = false; +uint8_t readBuffer[16]; + /*! \brief main function \param[in] none @@ -40,12 +42,23 @@ int main(void) { /* Initialize watchdog */ watchdog_init(); + uint8_t command[10]; + uint8_t command_length = 0; + while (1) { - delay_ms(10); + // delay_ms(10); fwdgt_counter_reload(); - if (g_statusSwitch) - {eddy_current_value_report();} + command_length = Command_GetCommand(command); + if (command_length != 0) + { + for (int i = 0; i < command_length; i++) + { + printf("%c", command[i]); + } + } + // if (g_statusSwitch) + // {eddy_current_value_report();} } }