单片机处理触摸按键例程
扫描二维码
随时随地手机看文章
//文件touchbutton.h#ifndef_TOUCHBUTTON_H_#define_TOUCHBUTTON_H_//=========宏定义===========================================#defineDEF_WATCH_TKREFER//查看参考-用于测试//===========================================================#defineV_REFER1//AD值超过此值更新参考值#defineV_PRESS7//AD值-参考大于此值认为按键按下-按键灵敏度#defineVT_KTc30//更新参考值所需要的次数//--------按键位置定义-----------#defineK_TKONSETB0//ON/OFF#defineK_TKBLSETB1//沸腾#defineK_TKPUSETB2//水泵#defineK_TKSTSETB3//恒温设定//#defineV_TKQK1(uint8)5//快速加减时间值typedefstructTCHBUT{uint8KTc1;//更新参考值确认时间uint8KRefer1;//按键1参考电压uint8KTc2;//更新参考值确认时间uint8KRefer2;//按键2参考电压/*uint8KTc3;//更新参考值确认时间uint8KRefer3;//按键2参考电压uint8KTc4;//更新参考值确认时间uint8KRefer4;//按键2参考电压uint8KTc5;//更新参考值确认时间uint8KRefer5;//按键2参考电压uint8KTc6;//更新参考值确认时间uint8KRefer6;//按键2参考电压uint8KTc7;//更新参考值确认时间uint8KRefer7;//按键2参考电压//uint8KTc8;//更新参考值确认时间//uint8KRefer8;//按键2参考电压*/uint8KTVal1;uint8KTVal2;//uint8KTVal3;//uint8KTVal4;}tTCHBut;externxdatatTCHButtCHBut;#defineKTc1tCHBut.KTc1#defineKRefer1tCHBut.KRefer1#defineKTc2tCHBut.KTc2#defineKRefer2tCHBut.KRefer2#defineKTc3tCHBut.KTc3#defineKRefer3tCHBut.KRefer3#defineKTc4tCHBut.KTc4#defineKRefer4tCHBut.KRefer4#defineKTc5tCHBut.KTc5#defineKRefer5tCHBut.KRefer5#defineKTc6tCHBut.KTc6#defineKRefer6tCHBut.KRefer6#defineKTc7tCHBut.KTc7#defineKRefer7tCHBut.KRefer7//#defineKTc8tCHBut.KTc8//#defineKRefer8tCHBut.KRefer8#defineKTVal1tCHBut.KTVal1#defineKTVal2tCHBut.KTVal2#defineKTVal3tCHBut.KTVal3#defineKTVal4tCHBut.KTVal4//=======按键检测==============typedefstructBUTTON2{uint8TKNOW;//当前几次按键的状态uint8TKLST;//uint8TKLST1;//uint8TKLST2;//uint8TKLST3;uint8TKSTB;//去抖后按键状态uint8TKSTBL;//上次去抖后按键状态uint8TKAVL;//下降沿有效值uint8TKAVLP;//上升沿有效值uint8TKeyStatu;//读按键的当前值uint8TKLONG;//长按键计数//uint8TTconQK;//快速调节计数111555101010}tButton2;externtButton2tKey2;#defineTKNOWtKey2.TKNOW#defineTKLSTtKey2.TKLST#defineTKLST1tKey2.TKLST1//#defineKLST2tKey.TKLST2//#defineKLST3tKey.TKLST3#defineTKSTBtKey2.TKSTB#defineTKSTBLtKey2.TKSTBL#defineTKAVLtKey2.TKAVL#defineTKAVLPtKey2.TKAVLP#defineTKeyStatutKey2.TKeyStatu#defineTKLONGtKey2.TKLONG#defineTTconQKtKey2.TTconQK#defineV_TKLOV1200//1S确认为长按#defineV_TKLOV220//15//150MS置一次按键标志//=========================================typedefstructKFlag2//按键处理标志位定义{uint8Flg1:1;uint8Flg2:1;uint8Flg3:1;uint8Flg4:1;uint8Flg5:1;uint8Flg6:1;uint8Flg7:1;uint8Flg8:1;}tKFlg2;externtKFlg2KF2;//#defineF_TRdKeyOKKF2.Flg1//读按键有效#defineF_TKLOKF2.Flg2//长按有效//#defineF_TKQK1KF2.Flg4//#defineF_TKQK2KF2.Flg5#defineF_TKEYOKKF2.Flg6//===========================================//================================externvoidReadTKRefer(uint8CH_AD,uint8*a);externvoidCheckTouch1(void);externvoidCheckTouch2(void);externvoidCheckTKey(void);#endif//文件touchbutton.c#include"global.h"#include"touchbutton.h"//#include"touchbutton.h"/***********************************************触摸PWM 500K当按按键时 电压值抬高 ***********************************************/tButton2 tKey2 ;tKFlg2 KF2 ;xdata tTCHBut tCHBut ;//#defineLKEY_ENB//长按键使能//***************************************// 函数名称:ReadTKRefer// 函数功能:读触摸按键参考// 入口参数:CH_AD 按键AD通道 // 出口参数:*a 对应该按键的AD参考值//***************************************void ReadTKRefer(uint8 CH_AD,uint8 *a){uint8R_Save[3];uint8R_Save2[3];uint16 R_SaveSum = 0 ; uint8 Tcon = 0 ; uint8 Tmp1 ; uint8 i ; for(i=40;i>0;i--) { AdcSwitch(CH_AD) ; //读按键1电压 Tmp1 = ADDH ; RMov3Data_Byte(R_Save,Tmp1) ; Tmp1 = C3MidVal(R_Save); //处理位数最大3位 取中值处理 RMov3Data_Byte(R_Save2,Tmp1) ; Tmp1 = C3MidVal(R_Save2); //处理位数最大3位 取中值处理 R_SaveSum += Tmp1 ; Tcon ++ ; if(Tcon >= (1<<3)) { *a = R_SaveSum>>3 ; Tcon = 0 ; R_SaveSum = 0 ; } }}//***************************************// 函数名称:CheckTouch1// 函数功能:检测触摸按键// 入口参数:无// 出口参数:无//***************************************void CheckTouch1(void) { uint8 a[3]; uint8 Tmp1 ; uint8 TmpAD ; //临时参考值 AdcSwitch(V_ADCH_tch1) ; //读按键电压 a[0] = ADDH ; _nop_();_nop_();_nop_();_nop_();_nop_(); AdcSwitch(V_ADCH_tch1) ; //读按键电压 a[1] = ADDH ; _nop_();_nop_();_nop_();_nop_();_nop_(); AdcSwitch(V_ADCH_tch1) ; //读按键电压 a[2] = ADDH ; //----以下为三次AD值的取中间值处理----- TmpAD = C3MidVal(a) ;#ifdefDEF_WATCH_TKREFER KTVal1 = TmpAD ; //当前按键AD值 -- 用于测试 ^^^^^^^^^^^^^^^^^^^^#endif if(!(TKSTB & SETB0)) //此按键此时处于抬起状态 --更改参考值 { if(TmpAD < KRefer1) { Tmp1 = KRefer1 - TmpAD ; } else { Tmp1 = TmpAD - KRefer1 ; } if(Tmp1 >= V_REFER) //更新参考门槛值 { KTc1 -- ; if(KTc1 == 0) { KTc1 = VT_KTc ; if(KRefer1 == TmpAD) //KRefer1 = TmpAD ; {} else if(KRefer1 < TmpAD) { KRefer1 ++ ; //更新参考 每次加减1 }