赞
踩
计算方法80M/(period+1)*(counter+1)得到定时频率,倒数为定时时长。
新建一个interrupt.c和interrupt.h的文件
interrut.h
- #ifndef _INTERRUPT_H_
- #define _INTERRUPT_H_
-
- #include"main.h"
- #include"stdbool.h"//引用头文件使用bool类型节约空间
- struct keys
- {
- uchar judge_sta;
- bool key_sta;
- bool key_flag;
- };
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
-
- #endif
interrupt.c
- #include"interrupt.h"
- struct keys key[4]={0,0,0};
-
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if(htim->Instance==TIM3)
- {
- key[0].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);
- key[1].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);
- key[2].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);
- key[3].key_sta=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
-
- for(uchar i=0;i<4;i++)
- {
- switch(key[i].judge_sta)
- {
- case 0:
- {
- if(key[i].key_sta==0) key[i].judge_sta=1;
- }
- break;
- case 1:
- {
- if(key[i].key_sta==0)
- {
- key[i].judge_sta=2;
- key[i].key_flag=1; //0->1 按键消抖
- }
- else
- key[i].judge_sta=0;
- }
- break;
- case 2:
- {
- if(key[i].key_sta==1)
- {
- key[i].judge_sta=0; //判断松手过程
- }
- }
- break;
- }
- }
- }
- }

按键处理函数
- void key_scan()
- {
- if(key[0].key_flag==1)
- {
- char text[30];
- sprintf(text," KEY0down ");
- LCD_DisplayStringLine(Line4,(uint8_t *)text);
- led_disp(0x81);
- key[0].key_flag=0;
- }
- if(key[1].key_flag==1)
- {
- char text[30];
- sprintf(text," KEY1down ");
- LCD_DisplayStringLine(Line4,(uint8_t *)text);
- led_disp(0x42);
- key[1].key_flag=0;
- }
- if(key[2].key_flag==1)
- {
- char text[30];
- sprintf(text," KEY2down ");
- LCD_DisplayStringLine(Line4,(uint8_t *)text);
- led_disp(0x24);
- key[2].key_flag=0;
- }
- if(key[3].key_flag==1)
- {
- char text[30];
- sprintf(text," KEY3down ");
- LCD_DisplayStringLine(Line4,(uint8_t *)text);
- led_disp(0x18);
- key[3].key_flag=0;
- }
- }

主函数头文件引用
- #include"led.h"
- #include"lcd.h"
- #include"stdio.h"
- #include"interrupt.h"
-
- extern struct keys key[4];//声明外部变量,在主函数中使用
主函数调用上面的按键处理函数即可。
while(1)前面记得开启定时器3的中断HAL_TIM_Base_Start_IT(&htim3);//开启中断
interrupt.h
- #ifndef _INTERRUPT_H_
- #define _INTERRUPT_H_
-
- #include"main.h"
- #include"stdbool.h"
- struct keys
- {
- uchar judge_sta;
- bool key_sta;
- bool short_flag;
- uint key_count;
- bool long_flag;
-
- };
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
-
- #endif

interrupt.c
- #include"interrupt.h"
- struct keys key[4]={0,0,0};
-
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if(htim->Instance==TIM3)
- {
- key[0].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);
- key[1].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);
- key[2].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);
- key[3].key_sta=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
-
- for(uchar i=0;i<4;i++)
- {
- switch(key[i].judge_sta)
- {
- case 0:
- {
- if(key[i].key_sta==0)
- {
- key[i].judge_sta=1;
- key[i].key_count=0;
- }
- }
- break;
- case 1:
- {
- if(key[i].key_sta==0)
- {
- key[i].judge_sta=2;
- }
- else
- key[i].judge_sta=0;
- }
- break;
- case 2:
- {
- if(key[i].key_sta==1)
- {
- key[i].judge_sta=0; //判断松手过程
- if(key[i].key_count<70) key[i].short_flag=1;
- }
- else
- {
- key[i].key_count++;
- if(key[i].key_count>=70) key[i].long_flag=1;
- }
- }
- break;
- }
- }
- }
- }

interrupt.h
- #ifndef _INTERRUPT_H_
- #define _INTERRUPT_H_
-
- #include"main.h"
- #include"stdbool.h"
- struct keys
- {
- uchar judge_sta;
- bool key_sta;
- bool short_flag;
- uint key_count;
- bool long_flag;
- bool double_timerEN;
- uchar double_timer;
- bool double_flag;
- };
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
-
- #endif

interrupt.c
- #include"interrupt.h"
- struct keys key[4]={0};
-
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if(htim->Instance==TIM3)
- {
- key[0].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);
- key[1].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);
- key[2].key_sta=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);
- key[3].key_sta=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);
-
- for(uchar i=0;i<4;i++)
- {
- switch(key[i].judge_sta)
- {
- case 0:
- {
- if(key[i].key_sta==0)
- {
- key[i].judge_sta=1;
- key[i].key_count=0;
- }
- }
- break;
- case 1:
- {
- if(key[i].key_sta==0)
- {
- key[i].judge_sta=2;
- }
- else
- key[i].judge_sta=0;
- }
- break;
- case 2:
- {
- if(key[i].key_sta==1&&key[i].key_count<70)
- {
- if(key[i].double_timerEN==0)
- {
- key[i].double_timerEN=1;
- key[i].double_timer=0;
- }
- else
- {
- key[i].double_flag=1;
- key[i].double_timerEN=0;
- }
- key[i].judge_sta=0;
- }
- else if(key[i].key_sta==1&&key[i].key_count>=70) key[i].judge_sta=0;
- else
- {
- key[i].key_count++;
- if(key[i].key_count>=70) key[i].long_flag=1;
- }
- }
- break;
- }
-
- if(key[i].double_timerEN==1)
- {
- key[i].double_timer++;
- if(key[i].double_timer>35)
- {
- key[i].short_flag=1;
- key[i].double_timerEN=0;
- }
- }
- }
- }
- }

双击代码需要理解其中的逻辑就比较简单。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。