From e99f7bface5dea60d4e3862e327d114858000265 Mon Sep 17 00:00:00 2001 From: "mr.zhangsan" Date: Sat, 4 May 2024 18:33:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=8C=89=E9=94=AEbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/.vscode/settings.json | 5 ++- source/app/main.c | 10 ++++- source/app/main.h | 11 ++++-- source/app/task_rs485.c | 74 ++++++++++++++++++++++++++++++++++++ source/app/task_rs485.h | 33 ++++++++++++++++ source/bsp/bsp_key.c | 27 +++++++------ source/msp/UART0.C | 2 +- source/msp/UART3.c | 2 - source/msp/UART4.C | 6 +-- source/tpc/debug.c | 23 ++++++----- source/tpc/debug.h | 13 ++++++- 11 files changed, 168 insertions(+), 38 deletions(-) create mode 100644 source/app/task_rs485.c create mode 100644 source/app/task_rs485.h diff --git a/source/.vscode/settings.json b/source/.vscode/settings.json index eabf88c..fcdb329 100644 --- a/source/.vscode/settings.json +++ b/source/.vscode/settings.json @@ -18,6 +18,9 @@ "task_nfc.h": "c", "task_debug.h": "c", "clib.h": "c", - "uart2.h": "c" + "uart2.h": "c", + "task_rfid.h": "c", + "task_rs485.h": "c", + "rs485.h": "c" } } \ No newline at end of file diff --git a/source/app/main.c b/source/app/main.c index a76ed86..d327b28 100644 --- a/source/app/main.c +++ b/source/app/main.c @@ -92,6 +92,7 @@ void L0_main_init(void) L0_uart0_buf_init(); L0_uart2_buf_init(); L0_uart3_buf_init(); + L0_uart4_buf_init(); //打开系统总中断 EA = 1; @@ -108,6 +109,7 @@ void L0_main_init(void) //任务初始化 L3_task_nfc_init(); L3_task_rfid_init(); + L3_task_rs485_init(); // L3_task_adc_init(0); // L3_task_adc_init(1); @@ -116,7 +118,7 @@ void L0_main_init(void) //L3_task_debug_init(); //L3_task_show_init(); //L3_task_reglisten_init(); - L0_uart0_sendArray("v2.7",4); + L0_uart0_sendArray("v1.3",4); } //=============================================== @@ -165,15 +167,19 @@ void main(void) //扫描键盘 key = bsp_key_scan(); if(key != BSP_KEY_NUM_MAX){ + L0_uart0_uc('#'); L0_uart0_uchex(key); } //NFC协议处理 - L3_task_nfc_handler(&_s_task_nfc); + //L3_task_nfc_handler(&_s_task_nfc); //RFID协议处理 L3_task_rfid_handler(&_s_task_rfid); + //RS485协议处理 + //L3_task_rs485_handler(&_s_task_rs485); + //串口调试 // if(ts_uart3_recv_buf.ok == 1){ // // count++; diff --git a/source/app/main.h b/source/app/main.h index 9f44870..44ede50 100644 --- a/source/app/main.h +++ b/source/app/main.h @@ -37,10 +37,6 @@ #ifndef _MAIN_H #define _MAIN_H -#include "../msp/uart0.h" -#include "../msp/uart2.h" -#include "../msp/uart3.h" -#include "../msp/eeprom.h" #include "../bsp/bsp_config.h" #include "../bsp/bsp_config_delay.h" @@ -50,6 +46,12 @@ #include "../bsp/cs1232.h" #include "../bsp/rs485.h" +#include "../msp/uart0.h" +#include "../msp/uart2.h" +#include "../msp/uart3.h" +#include "../msp/uart4.h" +#include "../msp/eeprom.h" + #include "../app/app_config.h" #include "../app/task_modbus.h" #include "../app/task_adc.h" @@ -58,6 +60,7 @@ #include "../app/task_encrypt.h" #include "../app/task_nfc.h" #include "../app/task_rfid.h" +#include "../app/task_rs485.h" ////////////////////////////////////////////////////////////////// diff --git a/source/app/task_rs485.c b/source/app/task_rs485.c new file mode 100644 index 0000000..878b6c0 --- /dev/null +++ b/source/app/task_rs485.c @@ -0,0 +1,74 @@ +//////////////////////////////////////////////////////////////////////////// +///@copyright Copyright (c) 2018, 传控科技 All rights reserved. +///------------------------------------------------------------------------- +/// @file bsp_drv.c +/// @brief bsp @ driver config +///------------------------------------------------------------------------- +/// @version 1.0 +/// @author CC +/// @date 20180331 +/// @note cc_AS_stc02 +////////////////////////////////////////////////////////////////////////////// + +#include "task_rs485.h" +#include "../app/app_config.h" +#include "../bsp/bsp_config.h" +#include "../msp/uart0.h" +#include "../msp/uart4.h" + +S_TASK_RS485 _s_task_rs485; +TPC_RS485 tpc_rs485; + +//============================================= +void L3_task_rs485_init(void) +{ + L1_task_init(&_s_task_rs485.task); + L3_task_s_go(_s_task_rs485,D_task_init); +} + +#define D_task_RS485_READ 0x50 +#define D_task_RS485_PRINT 0x51 +#define D_task_RS485_MODE 0x52 + +void L3_task_rs485_handler(S_TASK_RS485 *s) +{ + TTSS_Task_init() + L2_task_go(D_task_RS485_READ); + + TTSS_Task_step(D_task_RS485_READ) + if(ts_uart4_recv_buf.ok == 1) + { + ts_uart4_recv_buf.ok = 0; + // L0_uart0_uc(ts_uart4_recv_buf.reg); + LED0 ^= 1; + //Lc_buf_copy_uc((U8*)&tpc_rs485, ts_uart4_recv_buf.buf, ts_uart4_recv_buf.num); + //tpc_rs485.ocr = tpc_rs485.buf[tpc_rs485.num[0] << 8 | tpc_rs485.num[1]]; + //L2_task_go_Tdelay(D_task_RS485_PRINT,0); + } + + TTSS_Task_step(D_task_RS485_PRINT) + L0_uart0_sendstr("\r\n--------- Recv RS485 --------\r\n"); + L0_uart0_sendstr("slaveId : "); + L0_uart0_uchex(tpc_rs485.slaveId); + L0_uart0_0d0a(); + L0_uart0_sendstr("cmd: "); + L0_uart0_uchex(tpc_rs485.cmd); + L0_uart0_0d0a(); + L0_uart0_sendstr("num: "); + L0_uart0_uchex(tpc_rs485.num[0]); + L0_uart0_uchex(tpc_rs485.num[1]); + L0_uart0_0d0a(); + L0_uart0_sendstr("dat : "); + L0_uart0_sendArrayHex(tpc_rs485.buf, tpc_rs485.num[0] << 8 | tpc_rs485.num[1]); + L0_uart0_0d0a(); + L0_uart0_sendstr("ocr: "); + L0_uart0_uchex(tpc_rs485.ocr); + L0_uart0_0d0a(); + L2_task_go_Tdelay(D_task_RS485_READ,0); + + TTSS_Task_end(); +} + + + + diff --git a/source/app/task_rs485.h b/source/app/task_rs485.h new file mode 100644 index 0000000..a76a386 --- /dev/null +++ b/source/app/task_rs485.h @@ -0,0 +1,33 @@ +//////////////////////////////////////////////////////////////////////////// +///@copyright Copyright (c) 2018, 传控科技 All rights reserved. +///------------------------------------------------------------------------- +/// @file bsp_drv.h +/// @brief bsp @ driver config +///------------------------------------------------------------------------- +/// @version 1.0 +/// @author CC +/// @date 20180331 +/// @note cc_AS_stc02 + +////////////////////////////////////////////////////////////////////////////// + +#ifndef _APP_TASK_RS485_H +#define _APP_TASK_RS485_H + +#include "../ctask/task.h" +#include "../clib/clib.h" +#include "../tpc/debug.h" + +typedef struct _s_task_rs485 +{ + TS_task task; +}S_TASK_RS485; + +extern S_TASK_RS485 _s_task_rs485; + +extern void L3_task_rs485_init(void); +extern void L3_task_rs485_handler(S_TASK_RS485 *s); + + +#endif // #ifndef _APP_TASK_RS485_H + diff --git a/source/bsp/bsp_key.c b/source/bsp/bsp_key.c index 853b573..3e68356 100644 --- a/source/bsp/bsp_key.c +++ b/source/bsp/bsp_key.c @@ -93,7 +93,6 @@ U8 scan_col(U8 c) U8 bsp_key_scan(void) { - U8 key = BSP_KEY_NUM_MAX; //扫描第1行 scan_row(1); if(scan_col(1) > 0){ @@ -112,47 +111,47 @@ U8 bsp_key_scan(void) //扫描第2行 scan_row(2); if(scan_col(1) > 0){ - key = 4; + return 4; } if(scan_col(2) > 0){ - key = 5; + return 5; } if(scan_col(3) > 0){ - key = 6; + return 6; } if(scan_col(4) > 0){ - key = 7; + return 7; } //扫描第3行 scan_row(3); if(scan_col(1) > 0){ - key = 8; + return 8; } if(scan_col(2) > 0){ - key = 9; + return 9; } if(scan_col(3) > 0){ - key = 10; + return 10; } if(scan_col(4) > 0){ - key = 11; + return 11; } //扫描第4行 scan_row(4); if(scan_col(1) > 0){ - key = 12; + return 12; } if(scan_col(2) > 0){ - key = 13; + return 13; } // if(scan_col(KC3) > 0){ - // key = 14; + // return 14; // } // if(scan_col(KC4) > 0){ - // key = 15; + // return 15; // } - return key; + return BSP_KEY_NUM_MAX; } \ No newline at end of file diff --git a/source/msp/UART0.C b/source/msp/UART0.C index 5de1bfb..bedef75 100644 --- a/source/msp/UART0.C +++ b/source/msp/UART0.C @@ -60,7 +60,7 @@ msp/uartx.c 底层代码 和cpu相关 缓存发送也放在里面 int g_uart0_send_num = 0; //发送缓冲区 -static volatile Ts_uart_send_buf idata ts_uart_send_buf; +static volatile Ts_uart_send_buf ts_uart_send_buf; //接收缓冲区最多存放16个字符,这个值尽量小,但要大于实际处理协议的缓冲区大小 U8 uart0_recv_buf[16]; Ts_uart_recv_buf ts_uart0_recv_buf; diff --git a/source/msp/UART3.c b/source/msp/UART3.c index 3872d8e..37b015d 100644 --- a/source/msp/UART3.c +++ b/source/msp/UART3.c @@ -142,7 +142,6 @@ void INTERRUPT_UART3(void) D_SERVE_UART3// using 3 NOP(); NOP(); NOP(); if(L0_uart3_IntRI()) //如果是U3接收中断 { - // LED0 ^= 1; L0_uart3_IntRIClear(); //清除接收中断标志 ts_uart[uNum3].t->reg = L0_uartN_get(uNum3); g_uart3_recv_num++; @@ -152,7 +151,6 @@ void INTERRUPT_UART3(void) D_SERVE_UART3// using 3 } if(L0_uart3_IntTI()) //如果是U0发送中断 { - // LED0 ^= 1; L0_uart3_IntTIClear(); //清除发送中断标志 if(ts_uart[uNum3].p->max != ts_uart[uNum3].p->now) { diff --git a/source/msp/UART4.C b/source/msp/UART4.C index 1883e6e..9582481 100644 --- a/source/msp/UART4.C +++ b/source/msp/UART4.C @@ -58,7 +58,7 @@ msp/uartx.c 底层代码 和cpu相关 缓存发送也放在里面 #include "../tpc/debug.h" //发送缓冲区 -static volatile Ts_uart_send_buf idata ts_uart_send_buf; +static volatile Ts_uart_send_buf ts_uart_send_buf; //接收缓冲区最多存放16个字符,这个值尽量小,但要大于实际处理协议的缓冲区大小 #define RECV_BUF_SIZE 32 static U8 uart4_recv_buf[RECV_BUF_SIZE]; @@ -112,7 +112,7 @@ void L0_uart4_buf_init(void) ts_uart[uNum4].t->head_0 = 0xAA; ts_uart[uNum4].t->head_1 = 0x55; ts_uart[uNum4].t->maxnum = RECV_BUF_SIZE; - ts_uart[uNum4].tp_handler = L1_s2b_485; + ts_uart[uNum4].tp_handler = L1_s2b_rs485; //串口初始化 L0_uart4_init(); @@ -126,7 +126,7 @@ void L0_uart4_buf_init(void) void L0_uart4_sendArray(U8 * buf, U16 len) { #if (D_UART4_485_TYPE != TYPE_485_NONE) - D_UART4_485_TX() //切换到输出状态 + //D_UART4_485_TX() //切换到输出状态 #endif L0_uartN_sendArray(uNum4,buf,len); } diff --git a/source/tpc/debug.c b/source/tpc/debug.c index fed2fb7..801e6e4 100644 --- a/source/tpc/debug.c +++ b/source/tpc/debug.c @@ -167,14 +167,17 @@ void L1_s2b_rfid (Ts_uart_recv_buf *p) //reentrant } //485协议:AA 55 00 04 01 10 00 00 15 -//帧头:AA 55 -//数据长度:00 04,后续数据的长度,不包括最后的ocr -//SlaveId:01 -//Command: 10轮询读取 11设置参数 -//数据:00 00 -//校验:15 从协议长度到数据字段 [00 04 01 10 00 00] 的所有字节的累加和 -void L1_s2b_485 (Ts_uart_recv_buf *p) //reentrant +//FILTER:固定2个字节 AA55 +//SLAVEID:固定1个字节,代表从机ID +//CMDER: 固定1个字节,代表命令,每个从机自己定义。 +//NUM: 固定2个字节,高字节在前,代表后续DATA的长度(不包括ocr),比如0x0010,代表后续 16个字节 +//DATA:(Num 个字节)数据域 +//OCR:1个字节,代表校验和, = 从SlaveId开始 - DATA结束的所有字节之和。 +void L1_s2b_rs485 (Ts_uart_recv_buf *p) //reentrant { + // LED0 ^= 1; + p->ok = 1; + return; if (0 == p->head) { p->buf[p->index++] = p->reg; @@ -197,10 +200,10 @@ void L1_s2b_485 (Ts_uart_recv_buf *p) //reentrant else { p->buf[++p->index] = p->reg; - if(p->index == 3) + if(p->index == 5) { - //2个字节协议头 + 2字节数据长度 + 数据 + 1ocr - p->num = 4 + 1 + ((p->buf[2] << 8) | (p->buf[3])); + //2个字节协议头 + 1SlaveId + 1Cmder + 2字节数据长度 + 数据 + 1ocr + p->num = 6 + 1 + ((p->buf[4] << 8) | (p->buf[5])); if(p->num > p->maxnum){ //error p->head = 0; diff --git a/source/tpc/debug.h b/source/tpc/debug.h index 300cc51..5020247 100644 --- a/source/tpc/debug.h +++ b/source/tpc/debug.h @@ -61,6 +61,17 @@ typedef struct _tpc_rfid_ U8 tail; }TPC_RFID; +#define TPC_RS485_DAT_MAX 32 +typedef struct _tpc_rs485_ +{ + U8 head[2]; + U8 slaveId; + U8 cmd; + U8 num[2]; + U8 buf[TPC_RS485_DAT_MAX]; + U8 ocr; +}TPC_RS485; + #define D_HETU_FX_buf_max 5 //定长协议 长度为5 #define D_HETU_FX_fi 0x60 @@ -68,7 +79,7 @@ typedef struct _tpc_rfid_ extern void L1_s2b_debug(Ts_uart_recv_buf *p); extern void L1_s2b_nfc (Ts_uart_recv_buf *p); extern void L1_s2b_rfid (Ts_uart_recv_buf *p); -extern void L1_s2b_485 (Ts_uart_recv_buf *p); +extern void L1_s2b_rs485 (Ts_uart_recv_buf *p); extern void L1_s2b_PH1 (Ts_uart_recv_buf *p);// reentrant; #endif /* end __TPC_DEBUG_H_ */