diff --git a/README.md b/README.md index 1edacdc..14434e3 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,55 @@ ## CAMS DATU :CAMS计算机辅助缝纫管理系统采集模块 +#### 引脚连接 -#### 说明 + 关于输入输出引脚连接说明 + + |-------------------------------------------------------------| + |IN0 IN1 IN2 IN3 IN4 IN5 IN6 IN7 | + | 无 锁1 锁2 锁3 右盖板 左盖板 启停 主轴 | + | | + | 485 | + | 4G模组 屏幕 | + | | + | 232 ADC | + | | + |OUT0 OUT1 OUT2 OUT4 OUT4 OUT5 OUT6 OUT7 | + |闪灯 开锁1 开锁2 开锁3 报警灯 | + |-------------------------------------------------------------| + + +#### 上位机协议指令(modbus 16位CRC校验,DATU不参与计算) + + 44 41 54 55 03 00 01 40 00 读数据 + + 44 41 54 55 06 00 01 50 01 主轴针数清零 + 44 41 54 55 06 00 02 10 00 闪灯 + 44 41 54 55 06 00 03 D1 C0 开锁1 + 44 41 54 55 06 00 04 90 02 开锁2 + 44 41 54 55 06 00 05 51 C2 开锁3 + 44 41 54 55 06 00 06 11 C3 报警灯开 + 44 41 54 55 06 00 07 D0 03 报警灯关 + + 设备发送到屏幕的数据 + 44 41 54 55 00 00 00 04 05 00 00 00 00 01 B4 53 + + 44 41 54 55:DATU固定开头 + 00 00 00 04:主轴针数 + 05 00:张力传感器1,05为小数点前,00为小数点后 + 05 00:张力传感器2,同上 + 00 02:传感器状态, 0000 0010,代表IN1传感器有信号 + B4 53:CRC16 modbus校验码,DATU不参与计算 + + 传感器状态说明 + Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 + 主轴 启停 左盖板检测 右盖板检测 锁3状态 锁2状态 锁1状态 无 + +#### 开发程度说明 - 待完成 RS232协议(未指定) - - 已完成 1.IO输入输出 @@ -26,7 +68,7 @@ 9.命令输出指定log用于调试 - 10 .适配协议CAMS协议,解析与发送(指令接收后具体事项还未确定) + 10.屏幕交互协议适配完成 #### 开发环境 @@ -54,9 +96,8 @@ RT-Thread 软件打开 [点击下载](https://www.rt-thread.org/download.html#do | fram | 读写flash,用以保存读取配置 | | IO | 端口电平等 | | OLED | 屏幕 | -| modbus | modbus 功能,CAMS协议适配 | +| modbus | modbus 功能 | | thread | 线程管理 | -| LTE | 网络配置 | | ADC | ADC读值转换0-10V、3.3V、4-20ma等 | | AIR820 | 4G模组与MQTT代码 | | RS232 | 暂未定协议 | @@ -79,7 +120,6 @@ RT-Thread 软件打开 [点击下载](https://www.rt-thread.org/download.html#do | log_lte | 4G与mqtt的log | | log_moddbus | modbus的log | | log_adc | adc的log | -| log_datu | datu协议栈收发的log | | no_log | 关闭log | | IO_key < 0-8 > < 1 0 >| 指定输出端口改变电平 | diff --git a/applications/ADC/adc.c b/applications/ADC/adc.c index a6b31d3..79c9a74 100644 --- a/applications/ADC/adc.c +++ b/applications/ADC/adc.c @@ -73,11 +73,26 @@ void adc_read() // h_vol = (((float)vol - 0.2) * 6.8 ) / 2 ; ADC.ADC1_0_h_vol = (rt_uint16_t) (((float) ADC.ADC1_0_vol / 100 - 0.2) * 340); - if (ADC.ADC1_mode == 1) //1:0-10V 2:4-20ma + if (ADC.ADC1_mode == 0) //0:0-10V 1:4-20ma { + // 0-10V -> 0 - 100 cN + if (ADC.ADC1_0_h_vol < 30) { + ADC.ADC1_0_h_vol = 0; + } else if (ADC.ADC1_0_h_vol > 1000) { + ADC.ADC1_0_h_vol = 1000; + } +// ADC.ADC1_0_ZL_H = (ADC.ADC1_0_h_vol >> 8) & 0xFF; +// ADC.ADC1_0_ZL_L = ADC.ADC1_0_h_vol & 0xFF; + ADC.ADC1_0_ZL_H = ADC.ADC1_0_h_vol / 10; + ADC.ADC1_0_ZL_L = ADC.ADC1_0_h_vol % 10; + + if (ADC.log) + { + rt_kprintf("ADC1_ZL is :%d.%d \n", ADC.ADC1_0_ZL_H,ADC.ADC1_0_ZL_L); + } } - else if (ADC.ADC1_mode == 2) + else if (ADC.ADC1_mode == 1) { ADC.ADC1_0_Current = (rt_uint16_t) (((float) ADC.ADC1_0_vol / 100 ) / 150 * 340); } @@ -92,11 +107,26 @@ void adc_read() ADC.ADC2_4_value = rt_adc_read(adc2_dev, ADC2_CHANNEL_4); ADC.ADC2_4_vol = ADC.ADC2_4_value * REFER_VOLTAGE / CONVERT_BITS; ADC.ADC2_4_h_vol = (rt_uint16_t) (((float) ADC.ADC2_4_vol / 100 - 0.2) * 340); - if (ADC.ADC2_mode == 1) + if (ADC.ADC2_mode == 0) { + // 0-10V -> 0 - 100 cN + if (ADC.ADC2_4_h_vol < 30) { + ADC.ADC2_4_h_vol = 0; + } else if (ADC.ADC2_4_h_vol > 1000) { + ADC.ADC2_4_h_vol = 1000; + } +// ADC.ADC1_0_ZL_H = (ADC.ADC1_0_h_vol >> 8) & 0xFF; +// ADC.ADC1_0_ZL_L = ADC.ADC1_0_h_vol & 0xFF; + ADC.ADC2_4_ZL_H = ADC.ADC2_4_h_vol / 10; + ADC.ADC2_4_ZL_L = ADC.ADC2_4_h_vol % 10; + + if (ADC.log) + { + rt_kprintf("ADC2_ZL is :%d.%d \n", ADC.ADC2_4_ZL_H,ADC.ADC2_4_ZL_L); + } } - else if (ADC.ADC2_mode == 2) + else if (ADC.ADC2_mode == 1) { ADC.ADC2_4_Current = (rt_uint16_t) (((float) ADC.ADC2_4_vol / 100 ) / 150 * 340); } @@ -124,6 +154,35 @@ void adc_read() } + + +/** + * 获取ADC1张力值 + * @return + */ +rt_uint8_t get_ADC1_0_H_ZL() +{ + return ADC.ADC1_0_ZL_H; +} +rt_uint8_t get_ADC1_0_L_ZL() +{ + return ADC.ADC1_0_ZL_L; +} + +/** + * 获取ADC2张力值 + * @return + */ +rt_uint8_t get_ADC2_4_H_ZL() +{ + return ADC.ADC2_4_ZL_H; +} +rt_uint8_t get_ADC2_4_L_ZL() +{ + return ADC.ADC2_4_ZL_L; +} + + /** * 获取3V3的值ADC * @return @@ -179,7 +238,7 @@ void set_ADC_log(rt_uint8_t log) } /** - * 设置ADC1的模式1:0-10V 2:4-20ma + * 设置ADC1的模式0:0-10V 1:4-20ma * @param mode */ void set_ADC1_mode(rt_uint8_t mode) diff --git a/applications/ADC/adc.h b/applications/ADC/adc.h index ae264cb..4533a19 100644 --- a/applications/ADC/adc.h +++ b/applications/ADC/adc.h @@ -39,6 +39,11 @@ typedef struct rt_uint16_t ADC1_0_Current; // 4-20ma rt_uint16_t ADC2_4_Current; // + rt_uint8_t ADC1_0_ZL_H; // 张力高位 + rt_uint8_t ADC2_4_ZL_H; // + rt_uint8_t ADC1_0_ZL_L; // 张力低位 + rt_uint8_t ADC2_4_ZL_L; // + rt_uint8_t log; rt_uint8_t ADC1_mode; //ADC1模式 1:0-10V 2:4-20ma 3:3.3v rt_uint8_t ADC2_mode;//ADC2模式 @@ -77,10 +82,23 @@ rt_uint16_t get_ADC2_4_h_vol(); */ rt_uint16_t get_ADC2_4_Current(); +/** + * 获取ADC1张力值 + * @return + */ +rt_uint8_t get_ADC1_0_H_ZL(); +rt_uint8_t get_ADC1_0_L_ZL(); +/** + * 获取ADC2张力值 + * @return + */ +rt_uint8_t get_ADC2_4_H_ZL(); +rt_uint8_t get_ADC2_4_L_ZL(); + //ADC log 功能开启 void set_ADC_log(rt_uint8_t log); -//设置ADC1的模式1:0-10V 2:4-20ma +//设置ADC1的模式0:0-10V 1:4-20ma void set_ADC1_mode(rt_uint8_t mode); //设置ADC2的模式1:0-10V 2:4-20ma diff --git a/applications/IO/inout.c b/applications/IO/inout.c index 767eb16..06cea23 100644 --- a/applications/IO/inout.c +++ b/applications/IO/inout.c @@ -9,36 +9,41 @@ */ #include - IO_t IO; //输入端口列表 -rt_base_t input_pins[] = { - GET_PIN(C, 2), - GET_PIN(C, 3), - GET_PIN(C, 4), - GET_PIN(A, 5), - GET_PIN(A, 6), - GET_PIN(A, 7), - GET_PIN(A, 8), - GET_PIN(C, 9), +rt_base_t input_pins[] = { GET_PIN(C, 2), + GET_PIN(C, 3), + GET_PIN(A, 5), + GET_PIN(A, 6), + GET_PIN(A, 7), + GET_PIN(C, 4), + GET_PIN(A, 8), + GET_PIN(C, 9), }; //输出端口列表 rt_base_t output_pins[] = { - GET_PIN(B, 12), - GET_PIN(B, 13), - GET_PIN(B, 14), - GET_PIN(B, 15), - GET_PIN(C, 6), - GET_PIN(C, 7), - GET_PIN(C, 8), - GET_PIN(A, 11), - -}; +// GET_PIN(B, 12), //4 +// GET_PIN(B, 13), //5 +// GET_PIN(B, 14), //6 +// GET_PIN(B, 15), //7 +// GET_PIN(C, 6), //8 +// GET_PIN(C, 7), //3 +// GET_PIN(C, 8), //2 +// GET_PIN(A, 11), //1 + GET_PIN(A, 11), //1 + GET_PIN(C, 8), //2 + GET_PIN(C, 7), //3 + GET_PIN(B, 12), //4 + GET_PIN(B, 13), //5 + GET_PIN(B, 14), //6 + GET_PIN(B, 15), //7 + GET_PIN(C, 6), //8 + }; /** * 端口中断回调函数 @@ -46,41 +51,65 @@ rt_base_t output_pins[] = { */ void gpio_irq_callback(void *args) { - rt_base_t pin = (rt_base_t)(uintptr_t)args; // 获取触发中断的引脚号 - rt_kprintf("pin: %#x", pin); + rt_base_t pin = (rt_base_t) (uintptr_t) args; // 获取触发中断的引脚号 + // rt_kprintf("pin: %#x", pin , IO.IO_Data[0].Temp_Count); switch (pin) { - case IN1: + case IN0: IO.IO_Data[0].Temp_Count++; - //rt_kprintf("pin1:%d\n", IO.IO_Data[0].Temp_Count); + if (IO.IO_Flag.log) { + rt_kprintf("Pin0:%d\n", IO.IO_Data[0].Temp_Count); + } + + break; + case IN1: + IO.IO_Data[1].Temp_Count++; + if (IO.IO_Flag.log) { + rt_kprintf("Pin1:%d\n", IO.IO_Data[1].Temp_Count); + } + break; case IN2: - IO.IO_Data[1].Temp_Count++; - //rt_kprintf("pin2:%d\n", IO.IO_Data[1].Temp_Count); + IO.IO_Data[2].Temp_Count++; + if (IO.IO_Flag.log) { + rt_kprintf("Pin2:%d\n", IO.IO_Data[2].Temp_Count); + } + break; case IN3: - IO.IO_Data[2].Temp_Count++; - //rt_kprintf("pin3:%d\n", IO.IO_Data[2].Temp_Count); + IO.IO_Data[3].Temp_Count++; + if (IO.IO_Flag.log) { + rt_kprintf("Pin3:%d\n", IO.IO_Data[3].Temp_Count); + } + break; case IN4: - IO.IO_Data[3].Temp_Count++; - //rt_kprintf("pin4:%d\n", IO.IO_Data[3].Temp_Count); + IO.IO_Data[4].Temp_Count++; + if (IO.IO_Flag.log) { + rt_kprintf("Pin4:%d\n", IO.IO_Data[4].Temp_Count); + } + break; case IN5: - IO.IO_Data[4].Temp_Count++; - //rt_kprintf("pin5:%d\n", IO.IO_Data[4].Temp_Count); + IO.IO_Data[5].Temp_Count++; + if (IO.IO_Flag.log) { + rt_kprintf("Pin5:%d\n", IO.IO_Data[5].Temp_Count); + } + break; case IN6: - IO.IO_Data[5].Temp_Count++; - //rt_kprintf("pin6:%d\n", IO.IO_Data[5].Temp_Count); + IO.IO_Data[6].Temp_Count++; + if (IO.IO_Flag.log) { + rt_kprintf("Pin6:%d\n", IO.IO_Data[6].Temp_Count); + } + break; case IN7: - IO.IO_Data[6].Temp_Count++; - //rt_kprintf("pin7:%d\n", IO.IO_Data[6].Temp_Count); - break; - case IN8: IO.IO_Data[7].Temp_Count++; - //rt_kprintf("pin8:%d\n", IO.IO_Data[7].Temp_Count); + if (IO.IO_Flag.log) { + rt_kprintf("Pin7:%d\n", IO.IO_Data[7].Temp_Count); + } + break; default: break; @@ -95,7 +124,7 @@ void configure_inpin(rt_base_t pin) rt_kprintf(" pin: %d %#x\r\n", pin, pin); /* 绑定中断,下降沿模式,回调函数名为 */ - rt_pin_attach_irq(pin, PIN_IRQ_MODE_FALLING, gpio_irq_callback, (void *)(uintptr_t)pin); + rt_pin_attach_irq(pin, PIN_IRQ_MODE_FALLING, gpio_irq_callback, (void *) (uintptr_t) pin); /* 使能中断 */ rt_pin_irq_enable(pin, PIN_IRQ_ENABLE); } @@ -105,7 +134,7 @@ void configure_inpin(rt_base_t pin) */ void gpio_input_init(void) { - rt_kprintf(" pin list:\r\n"); + rt_kprintf(" IN pin list:\r\n"); for (rt_uint8_t var = 0; var < INPUT_NUM; var++) { @@ -117,7 +146,8 @@ void gpio_input_init(void) * 输出引脚默认为高电平 * @param pin */ -void configure_outpin(rt_base_t pin) { +void configure_outpin(rt_base_t pin) +{ // 设置引脚模式为输出模式 rt_pin_mode(pin, PIN_MODE_OUTPUT); // 默认为高电平 @@ -125,17 +155,121 @@ void configure_outpin(rt_base_t pin) { } //输出引脚初始化 -void gpio_output_init(void){ - for (rt_uint8_t var = 0; var < OUTPUT_NUM; var++) { +void gpio_output_init(void) +{ + + for (rt_uint8_t var = 0; var < OUTPUT_NUM; var++) + { configure_outpin(output_pins[var]); } } //获取端口电平触发次数 -rt_uint32_t get_IO_count(rt_uint8_t io){ +rt_uint32_t get_IO_count(rt_uint8_t io) +{ return IO.IO_Data[io].Temp_Count; } +//获取端口电平值 +rt_uint8_t get_IO_Value() +{ + return IO.IO_Flag.Value; +} + +rt_uint8_t get_IO_NeedleCountUpdateFlag() +{ + return IO.IO_Flag.NeedleCountUpdateFlag; +} + +void set_IO_Needle_io(rt_uint8_t io) +{ + IO.IO_Flag.Needle_io = io; +} + +rt_uint32_t get_IO_NeedleCount_sum() +{ + return IO.IO_Data[IO.IO_Flag.Needle_io].NeedleCount_Sum; +} + +void set_IO_NeedleCount_sum(rt_uint8_t count) +{ + IO.IO_Data[IO.IO_Flag.Needle_io].NeedleCount_Sum = count; +} + +/** + * @brief 确认端口电平.当端口从高电平变为低电平代表次数+1 + * @param none + * @return: None + */ +void updateKeyValue(void) +{ + + //#define IN0 0x22 //1 + //#define IN1 0x23 //2 锁1状态 + //#define IN2 0x5 //3 锁2状态 + //#define IN3 0x6 //4 锁3状态 + //#define IN4 0x7 //5 右盖板检测 + //#define IN5 0x24 //6 左盖板检测 + //#define IN6 0x8 //7 启停 + //#define IN7 0x29 //8 主轴 + + IO.IO_Flag.Value |= rt_pin_read(IN0) << 0; + IO.IO_Flag.Value |= rt_pin_read(IN1) << 1; + IO.IO_Flag.Value |= rt_pin_read(IN2) << 2; + IO.IO_Flag.Value |= rt_pin_read(IN3) << 3; + IO.IO_Flag.Value |= rt_pin_read(IN4) << 4; + IO.IO_Flag.Value |= rt_pin_read(IN5) << 5; + IO.IO_Flag.Value |= rt_pin_read(IN6) << 6; + IO.IO_Flag.Value |= rt_pin_read(IN7) << 7; + IO.IO_Flag.Value = ~IO.IO_Flag.Value; + +// rt_uint8_t i = 0; +// +// for (i = 0; i < INPUT_NUM; i++) +// { +// //跳过转速和针数口 +// if (i == IO.IO_Flag.Speed_io || i==IO.IO_Flag.Needle_io) +// { +// continue; +// } +// +// +// if (rt_pin_read(input_pins[i]) == 0) +// { +// rt_thread_mdelay(10); +// if (rt_pin_read(input_pins[i]) == 0) +// { +// IO.IO_Data[i].Key = 1; +// IO.IO_Data[i].Key_Flag = 1; +// } +// } +// else +// { +// //rt_kprintf("\n"); +// // rt_kprintf("key 0 \n"); +// IO.IO_Data[i].Key = 0; +// } +// } +// +// +// for (i = 0; i < INPUT_NUM; i++) +// { +// if (IO.IO_Data[i].Key_Flag == 1) +// { +// if (IO.IO_Data[i].Key == 0) +// { +// IO.IO_Data[i].Count += 1; +// IO.IO_Data[i].Key_Flag = 0; +// +// rt_kprintf("\n", IO.IO_Data[i].Count); +// if (IO.IO_Flag.log) { +// rt_kprintf("Count: %d , temp count %d\n", IO.IO_Data[i].Count, IO.IO_Data[i].Temp_Count); +// } +// } +// } +// } +} + /** * @brief 计算转速,500毫秒的中断次数乘2为1秒次数,1秒次数乘60为rpm单位转速 @@ -168,16 +302,17 @@ void updateSpeed(void) IO.IO_Data[IO.IO_Flag.Speed_io].Speed = (tmp_end - tmp_start) * 120; } - if (IO.IO_Flag.log) { - rt_kprintf("Speed: %d %d\n", IO.IO_Flag.Speed_io+1, IO.IO_Data[IO.IO_Flag.Speed_io].Speed); + if (IO.IO_Flag.log) + { + rt_kprintf("Speed: %d %d\n", IO.IO_Flag.Speed_io + 1, IO.IO_Data[IO.IO_Flag.Speed_io].Speed); } - // if(tmp_end < tmp_start){ - // IO.IO_Data[i].Temp_Speed = (tmp_end + 65535 - tmp_start) / ((tick_end - tick_start) / 1000); - // } - // else { - // IO.IO_Data[i].Temp_Speed = (tmp_end - tmp_start) / ((tick_end - tick_start) / 1000); - // } + // if(tmp_end < tmp_start){ + // IO.IO_Data[i].Temp_Speed = (tmp_end + 65535 - tmp_start) / ((tick_end - tick_start) / 1000); + // } + // else { + // IO.IO_Data[i].Temp_Speed = (tmp_end - tmp_start) / ((tick_end - tick_start) / 1000); + // } if (temp != IO.IO_Data[IO.IO_Flag.Speed_io].Speed) { @@ -190,16 +325,61 @@ void updateSpeed(void) } + +/** + * @brief 计算针数,500毫秒的中断次数,NeedleCount为临时针数,会清零,NeedleCount_Sum为针数总和 + * @param none + * @return: None + */ +void updateNeedleCount(void) +{ + rt_uint32_t tempCount = 0; + + tempCount = IO.IO_Data[IO.IO_Flag.Needle_io].Temp_Count; + rt_thread_mdelay(500); + + if ((IO.IO_Data[IO.IO_Flag.Needle_io].Temp_Count - tempCount)) + { + IO.IO_Data[IO.IO_Flag.Needle_io].NeedleCount = IO.IO_Data[IO.IO_Flag.Needle_io].Temp_Count - IO.IO_Data[IO.IO_Flag.Needle_io].NeedleCount_last; + IO.IO_Data[IO.IO_Flag.Needle_io].NeedleCount_Sum += IO.IO_Data[IO.IO_Flag.Needle_io].NeedleCount; + IO.IO_Flag.NeedleCountUpdateFlag = 1; + //微工厂需求针数要没有变化清零,下列注释,NeedleCount_Sum+=变为= + IO.IO_Data[IO.IO_Flag.Needle_io].NeedleCount = 0; + IO.IO_Data[IO.IO_Flag.Needle_io].NeedleCount_last = IO.IO_Data[IO.IO_Flag.Needle_io].Temp_Count; + if (IO.IO_Flag.log) { + rt_kprintf("NeedleCount: %d %d\n", IO.IO_Data[IO.IO_Flag.Needle_io].NeedleCount_Sum, IO.IO_Flag.Needle_io); + } + } + else + { + IO.IO_Flag.NeedleCountUpdateFlag = 0; + IO.IO_Data[IO.IO_Flag.Needle_io].NeedleCount = 0; + IO.IO_Data[IO.IO_Flag.Needle_io].NeedleCount_last = IO.IO_Data[IO.IO_Flag.Needle_io].Temp_Count; + + } + +} + + //控制台更改电平 -void IO_key(int argc, char**argv){ +void IO_key(int argc, char**argv) +{ rt_uint8_t var = atoi(argv[1]); rt_uint8_t var2 = atoi(argv[2]); - if (var2) { + if (var2) + { rt_pin_write(output_pins[var], PIN_HIGH); - } else { + } + else + { rt_pin_write(output_pins[var], PIN_LOW); } } -MSH_CMD_EXPORT(IO_key, chage IO); +MSH_CMD_EXPORT(IO_key, <0-7> <0-1>); + + +void set_IO_log(rt_uint8_t log){ + IO.IO_Flag.log = log; +} diff --git a/applications/IO/inout.h b/applications/IO/inout.h index bd2d7dc..2dfa4ee 100644 --- a/applications/IO/inout.h +++ b/applications/IO/inout.h @@ -17,14 +17,24 @@ #define OUTPUT_NUM 8 #define INPUT_NUM 8 -#define IN1 0x22 -#define IN2 0x23 -#define IN3 0x24 -#define IN4 0x5 -#define IN5 0x6 -#define IN6 0x7 -#define IN7 0x8 -#define IN8 0x29 +// +#define IN0 0x22 //1 +#define IN1 0x23 //2 锁1状态 +#define IN2 0x5 //3 锁2状态 +#define IN3 0x6 //4 锁3状态 +#define IN4 0x7 //5 右盖板检测 +#define IN5 0x24 //6 左盖板检测 +#define IN6 0x8 //7 启停 +#define IN7 0x29 //8 主轴 + +#define OUT0 GET_PIN(A, 11) //1 +#define OUT1 GET_PIN(C, 8) //2 +#define OUT2 GET_PIN(C, 7) //3 +#define OUT3 GET_PIN(B, 12) //4 +#define OUT4 GET_PIN(B, 13) //5 +#define OUT5 GET_PIN(B, 14)//6 +#define OUT6 GET_PIN(B, 15) //7 +#define OUT7 GET_PIN(C, 6) //8 typedef struct @@ -56,6 +66,7 @@ typedef struct rt_uint8_t SpeedUpdateFlag; // 速度更新标志位 rt_uint8_t NeedleCountUpdateFlag; // 针数更新标志位 rt_uint8_t log; + rt_uint8_t Value; } Flag; // IO状态 typedef struct @@ -66,8 +77,8 @@ typedef struct typedef struct { - Out IO_Out[15]; // IO输出端口状态 - Data IO_Data[15]; // IO当前数据 + Out IO_Out[8]; // IO输出端口状态 + Data IO_Data[8]; // IO当前数据 Flag IO_Flag; // IO状态 } IO_t; //IO结构体 @@ -83,6 +94,18 @@ void gpio_output_init(void); //获取端口电平触发次数 rt_uint32_t get_IO_count(rt_uint8_t io); +//获取端口电平值 +rt_uint8_t get_IO_Value(); + +rt_uint8_t get_IO_NeedleCountUpdateFlag(); +void set_IO_Needle_io(rt_uint8_t io); //控制台更改电平 void IO_key(); + +rt_uint32_t get_IO_NeedleCount_sum(); +void set_IO_NeedleCount_sum(rt_uint8_t count); + +void updateKeyValue(void); +void updateNeedleCount(void); +void set_IO_log(rt_uint8_t log); #endif /* APPLICATIONS_IO_INOUT_H_ */ diff --git a/applications/Modbus/DATU.c b/applications/Modbus/DATU.c index 9fed14f..3c2a88e 100644 --- a/applications/Modbus/DATU.c +++ b/applications/Modbus/DATU.c @@ -12,6 +12,7 @@ DATU_t DATU; +//CRC校验 rt_uint16_t CRC16_Calculate(rt_uint8_t *data, rt_uint8_t len) { rt_uint16_t CRC16in = 0xFFFF; @@ -35,6 +36,7 @@ rt_uint16_t CRC16_Calculate(rt_uint8_t *data, rt_uint8_t len) } +//阅读命令 rt_uint8_t read_cmd(rt_uint8_t read_buf[], rt_uint8_t read_len) { @@ -83,7 +85,7 @@ void datu_init() // rt_kprintf("Running."); rt_int8_t send_len; // rt_uint8_t send_buf[128]; - rt_uint8_t send_buf[16] = { 'D', 'A', 'T', 'U', 0x01, 0x02, 0x03, 0x04, 0x05 }; + rt_uint8_t send_buf[16] = { 'D', 'A', 'T', 'U' }; rt_uint8_t read_buf[10]; rt_int8_t read_len; rt_uint8_t i; @@ -99,8 +101,21 @@ void datu_init() */ rt_thread_mdelay(100); - // || 有针数 - if (DATU.send_data ) { + // 主机命令或者有针数时发一条数据上去 + if (DATU.send_data || get_IO_NeedleCountUpdateFlag()) { + rt_uint32_t temp = get_IO_NeedleCount_sum(); + + send_buf[4] = (temp >> 24) & 0xFF; // 获取最高8位 + send_buf[5] = (temp >> 16) & 0xFF; // 获取次高8位 + send_buf[6] = (temp >> 8) & 0xFF; // 获取次低8位 + send_buf[7] = temp & 0xFF; // 获取最低8位 + + send_buf[8] = get_ADC1_0_H_ZL(); + send_buf[9] = get_ADC1_0_L_ZL(); + send_buf[10] = get_ADC2_4_H_ZL(); + send_buf[11] = get_ADC2_4_L_ZL(); + + send_buf[13] = get_IO_Value(); //端口传感器状态 rt_uint16_t crc = CRC16_Calculate(send_buf + 4, 10); // rt_kprintf("CRC %x\n ", crc); send_buf[14] = crc >> 8; @@ -124,6 +139,7 @@ void datu_init() // read_len = rs485_send_then_recv(hinst, (void *) send_buf, sizeof(send_buf), read_buf, sizeof(read_buf)); + //接收主机发送的命令 read_len = rs485_recv(hinst, (void *) read_buf, sizeof(read_buf)); if (read_len < 0) @@ -162,9 +178,13 @@ void datu_init() //CRC校验 rt_uint16_t received_crc = (read_buf[7] << 8) | read_buf[8]; - rt_kprintf("CRC %x\n ", received_crc); rt_uint16_t calculated_crc = CRC16_Calculate(read_buf+4, 3); - rt_kprintf("CRC %x\n ", calculated_crc); + if (DATU.log) + { + rt_kprintf("CRC %x\n ", received_crc); + rt_kprintf("CRC %x\n ", calculated_crc); + } + //判断CRC是否一致 if (calculated_crc == received_crc) { if (DATU.log) { @@ -178,9 +198,10 @@ void datu_init() continue; } + //命令通过验证,读取命令细节 rc = read_cmd(read_buf, read_len); - if (rc == -1) + if (rc == -1) //DATTU开头错误 { if (DATU.log) { @@ -188,7 +209,7 @@ void datu_init() } continue; } - else if (rc == -2) + else if (rc == -2) //命令不是03和06 { if (DATU.log) { @@ -196,7 +217,7 @@ void datu_init() } continue; } - else if (rc == 3) + else if (rc == 3) //命令为03读取数据 { if (DATU.log) { @@ -205,7 +226,7 @@ void datu_init() DATU.send_data =1; continue; } - else if (rc == 6) + else if (rc == 6) //06命令执行细节 { if (DATU.log) { @@ -216,25 +237,32 @@ void datu_init() switch (read_buf[6]) { case 1: //主轴针数清零 + set_IO_NeedleCount_sum(0); break; case 2: //闪灯 // IO_key(1, argv); + DATU.toggle_led = ~DATU.toggle_led; break; case 3: //开锁1 + rt_pin_write(OUT1, 0); break; case 4: //开锁2 + rt_pin_write(OUT2, 0); break; case 5: //开锁3 + rt_pin_write(OUT3, 0); break; case 6: //报警灯开 + rt_pin_write(OUT4, 0); break; case 7: //报警灯关 + rt_pin_write(OUT4, 1); break; default: break; @@ -254,3 +282,7 @@ void set_DATU_log(rt_uint8_t log) DATU.log = log; } +rt_uint8_t get_DATU_toggle_led() +{ + return DATU.toggle_led; +} diff --git a/applications/Modbus/DATU.h b/applications/Modbus/DATU.h index 70d7254..d2e5cc6 100644 --- a/applications/Modbus/DATU.h +++ b/applications/Modbus/DATU.h @@ -17,15 +17,18 @@ #include #include #include - +#include +#include typedef struct { rt_uint8_t send_data;//发送数据 rt_uint8_t log; //log + rt_uint8_t toggle_led; } DATU_t; void datu_init(); void set_DATU_log(rt_uint8_t log); +rt_uint8_t get_DATU_toggle_led(); #endif /* APPLICATIONS_MODBUS_DATU_H_ */ diff --git a/applications/config/console.c b/applications/config/console.c index e85fe80..3a0db6f 100644 --- a/applications/config/console.c +++ b/applications/config/console.c @@ -144,11 +144,11 @@ void read_config() high_temp = hex_char_to_value(adc_1[0]); set_ADC1_mode(high_temp); - rt_kprintf(" ADC1 mode (1:0-10V 2:4-20ma) : %d\r\n", get_ADC1_mode()); + rt_kprintf(" ADC1 mode (0:0-10V 1:4-20ma) : %d\r\n", get_ADC1_mode()); high_temp = hex_char_to_value(adc_2[0]); set_ADC2_mode(high_temp); - rt_kprintf(" ADC2 mode (1:0-10V 2:4-20ma) : %d\r\n", get_ADC2_mode()); + rt_kprintf(" ADC2 mode (0:0-10V 1:4-20ma) : %d\r\n", get_ADC2_mode()); // //IO // set_Flag_Speed_IO((((speed_num[0] - '0') * 10) + (speed_num[1] - '0'))-1); @@ -490,7 +490,7 @@ void change_config() //ADC rt_kprintf("\r\n"); - rt_kprintf(" Set ADC (1:0-10V 2:4-20ma) : \r\n"); + rt_kprintf(" Set ADC (0:0-10V 1:4-20ma) : \r\n"); rt_kprintf(" ADC1_IN0 : "); adc_1[0] = finsh_getchar(); rt_kprintf("%c\n", adc_1[0]); @@ -582,6 +582,11 @@ void log_datu() set_DATU_log(1); } +void log_io() +{ + set_IO_log(1); +} + void no_log() { set_DATU_log(0); @@ -589,8 +594,10 @@ void no_log() set_LTE_log(0); set_mqtt_log(0); set_ADC_log(0); + set_IO_log(0); } +MSH_CMD_EXPORT(log_io, save to flash); MSH_CMD_EXPORT(log_datu, save to flash); MSH_CMD_EXPORT(change_config, save to flash); MSH_CMD_EXPORT(log_modbus, view_log to console); diff --git a/applications/main.c b/applications/main.c index 51135ac..9f52bba 100644 --- a/applications/main.c +++ b/applications/main.c @@ -25,10 +25,10 @@ int main(void) thread_IO_Key(); thread_Wireless(); thread_LCD(); - thread_IO_Speed(); + thread_IO_Needle(); thread_485(); thread_IO_ADC(); - + thread_Check_Save(); while (1) { // LOG_D("Hello RT-Thread!"); diff --git a/applications/thread/thread.c b/applications/thread/thread.c index e8cba64..ed21862 100644 --- a/applications/thread/thread.c +++ b/applications/thread/thread.c @@ -13,7 +13,7 @@ rt_align(RT_ALIGN_SIZE) static rt_uint8_t thread_io_key_stack[THREAD_STACK_SIZE_512]; -static rt_uint8_t thread_io_speed_stack[THREAD_STACK_SIZE_512]; +static rt_uint8_t thread_io_needle_stack[THREAD_STACK_SIZE_512]; static rt_uint8_t thread_io_adc_stack[THREAD_STACK_SIZE_512]; static rt_uint8_t thread_wireless_stack[THREAD_STACK_SIZE_512]; static rt_uint8_t thread_485_stack[THREAD_STACK_SIZE_1024]; @@ -35,7 +35,7 @@ static rt_uint8_t thread_check_save_stack[THREAD_STACK_SIZE_256]; //static rt_uint8_t thread_lcd_stack[THREAD_STACK_SIZE_512]; static struct rt_thread tid_IO_Key; -static struct rt_thread tid_IO_Speed; +static struct rt_thread tid_IO_Needle; static struct rt_thread tid_IO_ADC; static struct rt_thread tid_WL; static struct rt_thread tid_485; @@ -65,19 +65,18 @@ static void Task_IO_Key(void *parameter) gpio_output_init(); while (1) { - // updateKeyValue(); - rt_thread_mdelay(1); + updateKeyValue(); + rt_thread_mdelay(10); } } -static void Task_IO_Speed(void *parameter) +static void Task_IO_Needle(void *parameter) { -// spi_flash_mb85rs_init(); -// read_config(); + set_IO_Needle_io(7); while (1) { -// updateSpeed(); - rt_thread_mdelay(1000); + updateNeedleCount(); + rt_thread_mdelay(1); } } @@ -168,10 +167,23 @@ static void Task_Yields(void *parameter) static void Task_Check_Save(void *parameter) { + rt_uint8_t toggle= 0; + rt_uint8_t no_led = 0; while (1) { - // updateNeedleCount(); - rt_thread_mdelay(1); + if (get_DATU_toggle_led()) { + //rt_kprintf("LED: %d", toggle); + rt_pin_write(OUT0, toggle); + toggle = ~toggle; + no_led =1; + rt_thread_mdelay(1000); + } else { + if (no_led) { + rt_pin_write(OUT0, 1); + no_led = 0; + } + } + rt_thread_mdelay(100); } } @@ -195,17 +207,17 @@ rt_uint8_t thread_IO_Key(void) return 0; } -rt_uint8_t thread_IO_Speed(void) +rt_uint8_t thread_IO_Needle(void) { /* 初始化速度线程 */ - rt_thread_init(&tid_IO_Speed, "Thread_IO_Speed", Task_IO_Speed, - RT_NULL, thread_io_speed_stack, + rt_thread_init(&tid_IO_Needle, "Thread_IO_Speed", Task_IO_Needle, + RT_NULL, thread_io_needle_stack, THREAD_STACK_SIZE_512, THREAD_PRIORITY_6, THREAD_TIMESLICE_5); /* 启动线程 */ - rt_thread_startup(&tid_IO_Speed); + rt_thread_startup(&tid_IO_Needle); return 0; } diff --git a/applications/thread/thread.h b/applications/thread/thread.h index 0e52754..262c68d 100644 --- a/applications/thread/thread.h +++ b/applications/thread/thread.h @@ -40,7 +40,7 @@ #define THREAD_TIMESLICE_10 10 rt_uint8_t thread_IO_Key(void); -rt_uint8_t thread_IO_Speed(void); +rt_uint8_t thread_IO_Needle(void); rt_uint8_t thread_IO_ADC(void); rt_uint8_t thread_Wireless(void); rt_uint8_t thread_485(void);