From 1140221ef079ff59c9f36447b6ea07c2fb6e7404 Mon Sep 17 00:00:00 2001 From: "mr.zhangsan" Date: Sat, 4 May 2024 20:39:16 +0800 Subject: [PATCH] =?UTF-8?q?485=E6=B5=8B=E8=AF=95=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/app/app_config.c | 173 ++++++++++++++++++++-------------------- source/app/app_config.h | 80 +++++++++---------- source/app/main.c | 4 +- source/app/main.h | 4 - source/app/task_adc.c | 14 ++-- source/app/task_rs485.c | 70 +++++++++++++++- source/tpc/debug.h | 11 +++ 7 files changed, 215 insertions(+), 141 deletions(-) diff --git a/source/app/app_config.c b/source/app/app_config.c index eab6844..f7857f9 100644 --- a/source/app/app_config.c +++ b/source/app/app_config.c @@ -5,103 +5,106 @@ GlobalParam G; GlobalRegister R; +int i; -void L3_param_init(void) -{ -#if 0 - int i; - for(i=0;i>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Register CONFIG -//寄存器编号从1开始 -//#define D_COUNT_WEIGHT(adc) (U32)(1000.0 * R.p.weight_max * (adc) / (2 * 0x7FFFFF / 1000 * D_ADS1213_GAIN_VAL * R.p.lmd)) -//#define D_blur_threshold_2_1(threshold) (U32) ( 1.0 * threshold *(2 * 0x7FFFFF / 1000 * D_ADS1213_GAIN_VAL * R.p.lmd) / (1000.0 * R.p.weight_max )) -#define D_COUNT_WEIGHT(adc) (U32)((adc) / D_ADS1213_GAIN_VAL / R.p.lmd * R.p.weight_max * 1000 / (2 * 0x7FFFFF / 1000 )) -#define D_COUNT_ADC(wInG) (U32)((wInG) * D_ADS1213_GAIN_VAL * R.p.lmd * (2 * 0x7FFFFF / 1000) / R.p.weight_max / 1000) - -#define ADC_status_chx_Ready_BASE 0x01 -#define ADC_status_ch1_Ready 0x01 -#define ADC_status_ch2_Ready 0x02 -#define ADC_status_ch3_Ready 0x04 -#define ADC_status_ch4_Ready 0x08 -#define D_ADC_CHANNEL_NUM 4 +// #define D_COUNT_WEIGHT(adc) (U32)((adc) / D_ADS1213_GAIN_VAL / R.p.lmd * R.p.weight_max * 1000 / (2 * 0x7FFFFF / 1000 )) +// #define D_COUNT_ADC(wInG) (U32)((wInG) * D_ADS1213_GAIN_VAL * R.p.lmd * (2 * 0x7FFFFF / 1000) / R.p.weight_max / 1000) + + +// //STEP 1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Register CONFIG +// //寄存器编号从1开始 +// //#define D_COUNT_WEIGHT(adc) (U32)(1000.0 * R.p.weight_max * (adc) / (2 * 0x7FFFFF / 1000 * D_ADS1213_GAIN_VAL * R.p.lmd)) +// //#define D_blur_threshold_2_1(threshold) (U32) ( 1.0 * threshold *(2 * 0x7FFFFF / 1000 * D_ADS1213_GAIN_VAL * R.p.lmd) / (1000.0 * R.p.weight_max )) +// #define D_COUNT_WEIGHT(adc) (U32)((adc) / D_ADS1213_GAIN_VAL / R.p.lmd * R.p.weight_max * 1000 / (2 * 0x7FFFFF / 1000 )) +// #define D_COUNT_ADC(wInG) (U32)((wInG) * D_ADS1213_GAIN_VAL * R.p.lmd * (2 * 0x7FFFFF / 1000) / R.p.weight_max / 1000) + +// #define ADC_status_chx_Ready_BASE 0x01 +// #define ADC_status_ch1_Ready 0x01 +// #define ADC_status_ch2_Ready 0x02 +// #define ADC_status_ch3_Ready 0x04 +// #define ADC_status_ch4_Ready 0x08 +// #define D_ADC_CHANNEL_NUM 4 + typedef struct global_register { + //本机ID + U8 slave_id; + //低功耗模式 U8 low_power_mode; - //RO Register - U16 reserved1; - U32 total_weight; //净重(显示重量)*100 - U32 total_zweight; //皮重*100 - //RW Register - U16 reserved2; - U16 zero; //清0标志,写入任何值清0(去皮) - U16 status_eep_save; //eep写入寄存器,1则写入eep并清0 - //U16 reset; //reset标志,写入任何值,所有参数恢复初始值 - struct - { //需要持久化的参数,不能超过72字节,否则会导致eeprom溢出 - U16 slaver_id; - U16 adc_ch_status; - U16 weight_max; //量程 - U16 lmd; //2mv/v - U16 adc_blur_mid; //中值滤波 - U16 adc_blur_avg; //均值滤波 - U16 adc_blur_shift[3]; //移位滤波点数 - U16 adc_blur_threshold[2]; //移位滤波阀值 - }p; - U16 reserved3; - U32 adval[D_ADC_CHANNEL_NUM]; //4路重量 - U32 ch_weight[D_ADC_CHANNEL_NUM]; //4路重量 - U32 rough_weight; // 毛重 + //功能按键 + U8 func_btn; + + //NFC缓冲区 + struct { + //nfc卡号,只取后2个字节 + U8 nfc_no[2]; + }nfc; + + //RFID缓冲区 + struct { + //rfid卡号,只取后2个字节 + U8 rfid_no[2]; + //rfid信号强度 + U8 rssi; + //rfid天线编号 + U8 ant; + }rfids[4]; }GlobalRegister; extern GlobalRegister R; diff --git a/source/app/main.c b/source/app/main.c index 2996149..7526a85 100644 --- a/source/app/main.c +++ b/source/app/main.c @@ -101,9 +101,9 @@ void L0_main_init(void) L1_tick_init(); L0_timer0_Init(); - // L3_reg_init(); + //全局寄存器初始化 + L3_reg_init(); // L3_param_init(); - // L2_485_init(); // L0_ADS1213_INIT(); //任务初始化 diff --git a/source/app/main.h b/source/app/main.h index 44ede50..44a97f8 100644 --- a/source/app/main.h +++ b/source/app/main.h @@ -53,11 +53,7 @@ #include "../msp/eeprom.h" #include "../app/app_config.h" -#include "../app/task_modbus.h" -#include "../app/task_adc.h" #include "../app/task_debug.h" -#include "../app/task_register.h" -#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_adc.c b/source/app/task_adc.c index ce38ace..543da69 100644 --- a/source/app/task_adc.c +++ b/source/app/task_adc.c @@ -51,13 +51,13 @@ U32 Abs(S32 a) void L3_task_adc_init(U8 ch) { //通道采集任务初始化 - L1_task_init(&ts_adc_channel_samples[ch].task); - L3_task_s_go(ts_adc_channel_samples[ch],D_task_init); - ts_adc_channel_samples[ch].status = (R.p.adc_ch_status >> ch) & 0x1; - ts_adc_channel_samples[ch].pool = 0; - ts_adc_channel_samples[ch].adcval = 0; - ts_adc_channel_samples[ch].n = 0; - ts_adc_channel_samples[ch].ch = D_ADCCH_1 + ch; + // L1_task_init(&ts_adc_channel_samples[ch].task); + // L3_task_s_go(ts_adc_channel_samples[ch],D_task_init); + // ts_adc_channel_samples[ch].status = (R.p.adc_ch_status >> ch) & 0x1; + // ts_adc_channel_samples[ch].pool = 0; + // ts_adc_channel_samples[ch].adcval = 0; + // ts_adc_channel_samples[ch].n = 0; + // ts_adc_channel_samples[ch].ch = D_ADCCH_1 + ch; #if 0 //通道中值滤波任务初始化 diff --git a/source/app/task_rs485.c b/source/app/task_rs485.c index 398ce54..7c4472c 100644 --- a/source/app/task_rs485.c +++ b/source/app/task_rs485.c @@ -18,6 +18,11 @@ S_TASK_RS485 _s_task_rs485; TPC_RS485 tpc_rs485; +TPC_RS485_ACK tpc_rs485_ack; +static U8 acklen = 0; +static U8 i = 0; + +U8 constructor_rs485_ack(); //============================================= void L3_task_rs485_init(void) @@ -28,7 +33,8 @@ void L3_task_rs485_init(void) #define D_task_RS485_READ 0x50 #define D_task_RS485_PRINT 0x51 -#define D_task_RS485_MODE 0x52 +#define D_task_RS485_ACK 0x52 +#define D_task_RS485_MODE 0x53 void L3_task_rs485_handler(S_TASK_RS485 *s) { @@ -41,8 +47,29 @@ void L3_task_rs485_handler(S_TASK_RS485 *s) ts_uart4_recv_buf.ok = 0; 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); + // L2_task_go_Tdelay(D_task_RS485_PRINT,0); + L2_task_go_Tdelay(D_task_RS485_ACK,0); + } + + TTSS_Task_step(D_task_RS485_ACK) + if(tpc_rs485.slaveId == R.slave_id) + { + //读取 + if(tpc_rs485.cmd == 0x10) + { + //buf[0] 低功耗模式 + R.low_power_mode = tpc_rs485.buf[0]; + //buf[1-2], rfid映射 + //TODO + + + //构造响应包,并且返回 + acklen = constructor_rs485_ack(); + //写出 + L0_uart4_sendArray((U8*)&tpc_rs485_ack, acklen); + } } + L2_task_go_Tdelay(D_task_RS485_READ,0); TTSS_Task_step(D_task_RS485_PRINT) L0_uart0_sendstr("\r\n--------- Recv RS485 --------\r\n"); @@ -67,6 +94,45 @@ void L3_task_rs485_handler(S_TASK_RS485 *s) TTSS_Task_end(); } +U8 constructor_rs485_ack() +{ + U8 num = (1 + sizeof(R.nfc) + sizeof(R.rfids)); + tpc_rs485_ack.head[0] = 0xAA; + tpc_rs485_ack.head[1] = 0x55; + tpc_rs485_ack.slaveId = R.slave_id; + tpc_rs485_ack.cmd = 0x10; + tpc_rs485_ack.num[0] = (num >> 8) & 0xFF; + tpc_rs485_ack.num[1] = num & 0xFF; + tpc_rs485_ack.buf[0] = R.func_btn; + Lc_buf_copy_uc((U8*)&tpc_rs485_ack.buf[1], (U8*)&R.nfc, sizeof(R.nfc)); + Lc_buf_copy_uc((U8*)&tpc_rs485_ack.buf[1+sizeof(R.nfc)], (U8*)R.rfids, sizeof(R.rfids)); + tpc_rs485_ack.ocr = 0; + tpc_rs485_ack.ocr += tpc_rs485_ack.slaveId; + tpc_rs485_ack.ocr += tpc_rs485_ack.cmd; + tpc_rs485_ack.ocr += tpc_rs485_ack.num[0]; + tpc_rs485_ack.ocr += tpc_rs485_ack.num[1]; + for(i=0;i