#include "awtk.h" #include "key_protocol.h" #include "gpio_protocol.h" #include "board.h" #include "chip.h" #include "data_port.h" #include "gear_protocol.h" #include "moto_config.h" extern void check_key(void); // 定义定时器句柄 TimerHandle_t xTimer; uint8_t GPIO_value_high[7]; #ifdef UI_VIEW_QUICKLY_ARRANGE #define IO_KEEP 1 //采样大于1次 #define CNT_KEEP 1 //20ms一次判断 #else #define IO_KEEP 2 //采样大于两次 #define CNT_KEEP 3 //30ms一次判断 #endif uint8_t gpio_pins[7] = {GPIO_D6, GPIO_D5, GPIO_D4, GPIO_D3, GPIO_D2, GPIO_D1, GPIO_N}; void grade_key(void){ #ifdef UI_VIEW_QUICKLY_ARRANGE static uint8_t Gear_value = 7; #else // uint8_t Gear_value = 7; static uint8_t Gear_value = 7; #endif static uint8_t cnt =0; //数据顺序 N 1 2 3 4 5 6 ABS TCS FAN ENG YG R L RMT //挡位高电平触发, 灯光、ABS、TCS、FAN ENG 低电平触发 if(gpio_get_value(GPIO_GRADE_N)) { GPIO_value_high[GPIO_N]++; } else GPIO_value_high[GPIO_N] = 0; if(gpio_get_value(GPIO_GRADE_D1)) { GPIO_value_high[GPIO_D1]++; } else GPIO_value_high[GPIO_D1] = 0; if(gpio_get_value(GPIO_GRADE_D2)) { GPIO_value_high[GPIO_D2]++; } else GPIO_value_high[GPIO_D2] = 0; if(gpio_get_value(GPIO_GRADE_D3)) { GPIO_value_high[GPIO_D3]++; } else GPIO_value_high[GPIO_D3] = 0; if(gpio_get_value(GPIO_GRADE_D4)) { GPIO_value_high[GPIO_D4]++; } else GPIO_value_high[GPIO_D4] = 0; if(gpio_get_value(GPIO_GRADE_D5)) { GPIO_value_high[GPIO_D5]++; } else GPIO_value_high[GPIO_D5] = 0; if(gpio_get_value(GPIO_GRADE_D6)) { GPIO_value_high[GPIO_D6]++; } else GPIO_value_high[GPIO_D6] = 0; cnt++; if(cnt>=CNT_KEEP){ // if(cnt){ // 针对当前档位进行赋值 for (uint8_t i = 0; i < 7; i++) { if (GPIO_value_high[gpio_pins[i]] >= IO_KEEP) { Gear_value = (6-i); // 根据索引设置 Gear_value break; // 找到第一个符合条件的引脚后退出循环 } } Set_sys_grade(Gear_value); cnt=0; memset(GPIO_value_high, 0, 7); //buff初始化 } } // 定时器回调函数 void vTimerCallback(TimerHandle_t xTimer) { // 定时器到期时执行的代码 //按键检测 check_key(); //档位检测 grade_key(); } static void gear_gpio_init(void){ gpio_direction_input(GPIO_GRADE_N); gpio_direction_input(GPIO_GRADE_D1); gpio_direction_input(GPIO_GRADE_D2); gpio_direction_input(GPIO_GRADE_D3); gpio_direction_input(GPIO_GRADE_D4); gpio_direction_input(GPIO_GRADE_D5); gpio_direction_input(GPIO_GRADE_D6); } void Moto_gpio_timer(void) { gear_gpio_init(); // 创建定时器 xTimer = xTimerCreate("MyTimer", // 定时器名称 pdMS_TO_TICKS(5), // 定时器周期(1000毫秒) pdTRUE, // 自动重载定时器 (void *)0, // 定时器 ID vTimerCallback); // 回调函数 // 启动定时器 if (xTimerStart(xTimer, 0) != pdPASS) { // 启动定时器失败的处理 printf("xTimer error!!!!!!!!!!!!!!!!!.\r\n"); } } TimerHandle_t xspeedTimer; static uint8_t speed_count = 0; static uint32_t current_timer = 0; static uint32_t past_timer = 0; uint8_t time_diff=0; uint16_t speed_diff=0; extern uint16_t can_speed; extern uint16_t diff_can_speed; extern uint32_t mile_total; // void can_loss_data_restore(void){ // past_timer = 0; // } // void past_timer_refresh(void){ // if(!past_timer){ // past_timer = current_timer; // return; // } // printf("time_diff = %d.\r\n",time_diff); // time_diff = current_timer-past_timer; // if(time_diff==1){//相差为1,未漏帧 // past_timer = current_timer; // return; // }else{ // time_diff-1; // } // speed_diff = (uint8_t)((can_speed+diff_can_speed)/2); // mile_total+= (speed_diff*time_diff); // past_timer = current_timer; // } // extern uint8_t abs_speed_flag; // volatile uint8_t cnt = 0; void vTimerSpeedback(TimerHandle_t xTimer) { // printf("cnt=%d,mile_total=%d,can_speed=%d.\r\n",cnt,mile_total,can_speed); // if(cnt>=2){ // can_speed =0; // }else{ // cnt++; // } // if(abs_speed_flag){ // mile_total +=can_speed; // } // speed_count++; // if(speed_count>=10){ speed_convert_mile_calculation(); // speed_count = 0; // } } // volatile uint32_t ulHighFrequencyTimerCounts3 = 0; // static void prvRunTimer_Handler( void *para ) // { // vTimerClrInt((uint32_t)para); // // ulHighFrequencyTimerCounts3++; // // printf("=%d.\r\n",ulHighFrequencyTimerCounts3); // // printf("cnt=%d,mile_total=%d,can_speed=%d.\r\n",cnt,mile_total,can_speed); // if(cnt>=2){ // can_speed =0; // }else{ // cnt++; // } // // if(abs_speed_flag){ // mile_total +=can_speed; // // } // speed_count++; // if(speed_count>=10){ // speed_convert_mile_calculation(); // speed_count = 0; // } // } void Moto_speed_timer(void){ xspeedTimer = xTimerCreate("MyspeeSTimer", // 定时器名称 pdMS_TO_TICKS(100), // 定时器周期(1000毫秒) pdTRUE, // 自动重载定时器 (void *)0, // 定时器 ID vTimerSpeedback); // 回调函数 // 启动定时器 if (xTimerStart(xspeedTimer, 0) != pdPASS) { // 启动定时器失败的处理 printf("xTimer error!!!!!!!!!!!!!!!!!.\r\n"); } // vTimerInit(TIMER_ID3, 1, 1, pdMS_TO_TICKS(100)); // request_irq(TIMER2_IRQn, 1, prvRunTimer_Handler, (void*)TIMER_ID3); // vTimerEnable(TIMER_ID3); }