#include "io_app.h" #include "board.h" //#include "user_app.h" #include "variable.h" #include #define INPUT_BITS_NUMBER 12 #define OUTPUT_BITS_NUMBER 12 #if (INPUT_BITS_NUMBER < IO_PIN_INPUT_NUMBER) #undef INPUT_BITS_NUMBER #define INPUT_BITS_NUMBER IO_PIN_INPUT_NUMBER #endif #if (OUTPUT_BITS_NUMBER < IO_PIN_OUTPUT_NUMBER) #undef OUTPUT_BITS_NUMBER #define OUTPUT_BITS_NUMBER IO_PIN_OUTPUT_NUMBER #endif #define IO_FILTER_H 2 //IO滤波高通计? #define IO_FILTER_L 0 //IO滤波低通计? #define IO_FILTER_JIFTTER 1000 //IO滤波间隔,单位是微秒 int io_inputs_buffer[IO_PIN_INPUT_NUMBER]; byte_bits_t io_inputs[(INPUT_BITS_NUMBER + 7)>> 3]; byte_bits_t io_inputs_old[(INPUT_BITS_NUMBER + 7)>> 3]; byte_bits_t io_outputs[(OUTPUT_BITS_NUMBER + 7)>> 3]; byte_bits_t io_outputsen; static sw_timer_t filter_timer; /** * IO应用初始化函? * * @author lxz (2019/5/29/周三) */ void io_app_init(void) { memset(io_inputs, 0, sizeof(io_inputs)); memset(io_inputs_old, 0, sizeof(io_inputs_old)); memset(io_outputs, 0, sizeof(io_outputs)); sw_timer_start(&filter_timer, 0, 0); //hw_io_output_enable(); } /** * IO滤波输入 * * @author lxz (2019/5/29/周三) */ void io_app_read_input(void) { int index = 0; memcpy(io_inputs_old, io_inputs, sizeof(io_inputs)); if (bSoftwareInputEnable) { //软件输入,输出信号由M0160开始数据替? //IO数据挂到通讯变量,其实这个也可以不这么干 memcpy(io_inputs, &middle_coils[160 >> 3], sizeof(io_inputs)); } else { //读取输入 if (sw_timer_expire(&filter_timer)) { sw_timer_start(&filter_timer, 0, IO_FILTER_JIFTTER); //保存上一次的IO状? for (index = 0; index < IO_PIN_INPUT_NUMBER; index++) { if (hw_io_pin_input(index)) { if (io_inputs_buffer[index] < IO_FILTER_H) { io_inputs_buffer[index]++; } else { io_inputs[index >> 3].value |= 1 << (index & 0x07); } } else if (io_inputs_buffer[index] > IO_FILTER_L) { io_inputs_buffer[index]--; } else { io_inputs[index >> 3].value &= ~(1 << (index & 0x07)); } } } //IO数据挂到通讯变量,其实这个也可以不这么干 memcpy(&middle_coils[(160 + 7) >> 3], io_inputs, sizeof(io_inputs)); } } /** * 主动输出 * * @author lxz (2019/5/29/周三) */ void io_app_write_ouput(void) { int index = 0; if (bSoftwareOutputEnable) { //软件输出,输出信号由M0240开始数据替? for (index = 0; index < IO_PIN_OUTPUT_NUMBER; index++) { hw_io_pin_output(index, (middle_coils[(240 + index) >> 3].value & (1 << (index & 0x07))) != 0); } } else { for (index = 0; index < IO_PIN_OUTPUT_NUMBER; index++) { hw_io_pin_output(index, (io_outputs[index >> 3].bytes[0] & (1 << (index & 0x07))) != 0); } //IO数据挂到通讯变量,其实这个也可以不这么干 memcpy(&middle_coils[(240 + 7) >> 3], io_outputs, sizeof(io_outputs)); } //memcpy(&middle_coils[(256 + 7) >> 3], &io_outputsen, 1); } /** * 关闭输出,由step来确定需要关闭什么样的输入输? * * @author lxz (2019/6/16/周日) * * @param void */ void io_app_close_output(char step) { if (step == 1) { io_outputs[0].value = 0; io_outputs[1].value = 0; } else { io_outputs[0].value = 0; io_outputs[1].value = 0; } } /** * IO应用行函数执 * * @author lxz (2019/5/29/周三) */ #define TEST_APP void io_app_run(void) { int index = 0; #ifdef TEST_APP int flag = 0; static int status = 1; static sw_timer_t timer; // for (index = 0; index<(OUTPUT_BITS_NUMBER + 7)>> 3; index++) { // // io_outputs[index].bytes[0] = io_inputs[index].bytes[0]; // // flag += io_inputs[index].bytes[0]; // } if (flag == 0) { if (sw_timer_expire(&timer)) { status <<= 1; sw_timer_start(&timer, 0, 500000); } io_outputs[0].bytes[0] = status; io_outputs[1].bytes[0] = status>>8; if(status==0x1000) status=1; } else { sw_timer_start(&timer, 0, 500000); } #else for (index = 0; index<(OUTPUT_BITS_NUMBER + 7)>> 3; index++) { if (middle_coils[(200 >> 3) + index].bytes[0] != 0) { io_outputs[index].bytes[0] ^= middle_coils[(200 >> 3) + index].bytes[0]; middle_coils[(200 >> 3) + index].bytes[0] = 0; } } #endif }