button.c
#include <N76E003.H>
#include <SFR_Macro.h> //N76E003 SFR寄存器设置宏定义
#include <Function_define.h> //功能定义
#include "button.h"
#include "data.h"
/*---------------------------------------- 数据宏定义 ------------------------------------------*/
/*---------------------------------------- 按键初始化 ------------------------------------------*/
// 初始化按键
void Init_button(void){
//button1
P1M1 |= (0x00 << button0_Num); // 设置为1
P1M2 &= ~(0x01 << button0_Num); // 设置为0
}
/*---------------------------------------- 功能模块 ------------------------------------------*/
// 获取按键
void Button_Process(struct Button_data* Button,bit status){
switch(Button->Button_State_machine_mode){
case Button_status_S0:{ // 等待按键按下
if(Button_UP == status){
Button->Button_status = Button_UP; // 按键状态抬起
Button->Button_State_machine_mode = Button_status_S0; // 无操作
Button->Button_statusmode = Button_mode_NoOperation; // 按键无动作
DATA.Button0.Button_LiftTime = DATA.Button0.Button_LiftTime + 1; // 低状态计数
}
else if(Button_DOWN == status){ // 按键按下
Button->Button_State_machine_mode = Button_status_S1; // 切换去抖模块
Button->Button_PressTime = Button_TimeDebounce; // 设置去抖时间
}
}break;
case Button_status_S1:{ // 去抖验证
if(Button->Button_PressTime){Button->Button_PressTime = Button->Button_PressTime - 1;} // 等待时间
else if(0 == Button->Button_PressTime){ // 去抖时间到
if(Button_UP == status){
Button->Button_State_machine_mode = Button_status_S0; // 无操作
DATA.Button0.Button_LiftTime = 0; // 清空按键抬起计数
}
else if(Button_DOWN == status){ // 短按成立
Button->Button_status = Button_DOWN; // 按键按下
Button->Button_State_machine_mode = Button_status_S2; // 切换等待按键松开
Button->Button_PressTime = 0; // 清零按下时间
}
}
}break;
case Button_status_S2:{ // 按下时间(计算是不是长按)
if(Button_DOWN == status){ // 按键按下
// 计时
Button->Button_PressTime = Button->Button_PressTime + 1; // 按键计时
if(Button->Button_PressTime > Button_TimeEvenPressinterval){
Button->Button_PressTime = Button_TimeEvenPressinterval;
}
}
else if (Button_UP == status){ // 按键松开
if(Button->Button_PressTime == Button_TimeEvenPressinterval){
Button->Button_status = Button_UP;
Button->Button_State_machine_mode = Button_status_S0;
DATA.Button0.Button_LiftTime = 0; // 清空按键抬起计数
Button->Button_statusmode = Button_mode_longPress; // 长按
}else{
Button->Button_State_machine_mode = Button_status_S3; // 短按 或 双击
}
Button->Button_LiftTime = 0; // 按键松开计时
}
}break;
case Button_status_S3:{ // 快安判断(判断是不是短按)
Button->Button_LiftTime = Button->Button_LiftTime + 1; //时间记录
if(Button->Button_PressTime > Button_TimeShort_even){ // 大于快安值
Button->Button_status = Button_UP;
Button->Button_State_machine_mode = Button_status_S0; // 短按
DATA.Button0.Button_LiftTime = 0; // 清空按键抬起计数
Button->Button_statusmode = Button_mode_ShortPress; // 短按
}else{
if(Button_DOWN == status){ // 按键按下
Button->Button_State_machine_mode = Button_status_S4; // 双击去抖
Button->Button_PressTime = Button_TimeDebounce; // 设置去抖时间
}
}
}break;
case Button_status_S4:{ // 双击去抖
Button->Button_LiftTime = Button->Button_LiftTime + 1; //时间记录
if(Button->Button_PressTime){Button->Button_PressTime = Button->Button_PressTime - 1;} // 等待时间
else if(0 == Button->Button_PressTime){ // 去抖时间到
if(Button_UP == status){ // 去抖失败
Button->Button_status = Button_UP;
Button->Button_State_machine_mode = Button_status_S0; // 短按
DATA.Button0.Button_LiftTime = 0; // 清空按键抬起计数
Button->Button_statusmode = Button_mode_ShortPress; // 短按
}
else if(Button_DOWN == status){ // 双击成立
Button->Button_status = Button_DOWN; // 按键按下
Button->Button_State_machine_mode = Button_status_S5; // 切换等待按键松开
}
}
}break;
case Button_status_S5:{ // 等待快安退出
if(Button_UP == status){
Button->Button_status = Button_UP;
Button->Button_State_machine_mode = Button_status_S0; // 双击
DATA.Button0.Button_LiftTime = 0; // 清空按键抬起计数
Button->Button_statusmode = Button_mode_evenPress; // 双击
}
}break;
default:{
Button->Button_State_machine_mode = Button_status_S0;
DATA.Button0.Button_LiftTime = 0; // 清空按键抬起计数
}break; // 有问题转等待按键
}
// Button->Button_status = status;
}
/*---------------------------------------- 中断调用模块 ------------------------------------------*/
// 批处理函数
void All_Buttonrefresh(void){
Button_Process(&DATA.Button0,(bit)Git_button0);
}
|