//文件touchbutton.h#ifndef_TOUCHBUTTON_H_#define_TOUCHBUTTON_H_//=========宏定義===========================================#defineDEF_WATCH_TKREFER//查看參考-用于測試//===========================================================#defineV_REFER1//AD值超過此值更新參考值#defineV_PRESS7//AD值-參考大于此值認(rèn)為按鍵按下-按鍵靈敏度#defineVT_KTc30//更新參考值所需要的次數(shù)//--------按鍵位置定義-----------#defineK_TKONSETB0//ON/OFF#defineK_TKBLSETB1//沸騰#defineK_TKPUSETB2//水泵#defineK_TKSTSETB3//恒溫設(shè)定//#defineV_TKQK1(uint8)5//快速加減時間值typedefstructTCHBUT{uint8KTc1;//更新參考值確認(rèn)時間uint8KRefer1;//按鍵1參考電壓uint8KTc2;//更新參考值確認(rèn)時間uint8KRefer2;//按鍵2參考電壓/*uint8KTc3;//更新參考值確認(rèn)時間uint8KRefer3;//按鍵2參考電壓uint8KTc4;//更新參考值確認(rèn)時間uint8KRefer4;//按鍵2參考電壓uint8KTc5;//更新參考值確認(rèn)時間uint8KRefer5;//按鍵2參考電壓uint8KTc6;//更新參考值確認(rèn)時間uint8KRefer6;//按鍵2參考電壓uint8KTc7;//更新參考值確認(rèn)時間uint8KRefer7;//按鍵2參考電壓//uint8KTc8;//更新參考值確認(rèn)時間//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;//當(dāng)前幾次按鍵的狀態(tài)uint8TKLST;//uint8TKLST1;//uint8TKLST2;//uint8TKLST3;uint8TKSTB;//去抖后按鍵狀態(tài)uint8TKSTBL;//上次去抖后按鍵狀態(tài)uint8TKAVL;//下降沿有效值uint8TKAVLP;//上升沿有效值uint8TKeyStatu;//讀按鍵的當(dāng)前值uint8TKLONG;//長按鍵計(jì)數(shù)//uint8TTconQK;//快速調(diào)節(jié)計(jì)數(shù)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確認(rèn)為長按#defineV_TKLOV220//15//150MS置一次按鍵標(biāo)志//=========================================typedefstructKFlag2//按鍵處理標(biāo)志位定義{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"http://#include"touchbutton.h"/***********************************************觸摸PWM 500K當(dāng)按按鍵時 電壓值抬高 ***********************************************/tButton2 tKey2 ;tKFlg2 KF2 ;xdata tTCHBut tCHBut ;//#defineLKEY_ENB//長按鍵使能//***************************************// 函數(shù)名稱:ReadTKRefer// 函數(shù)功能:讀觸摸按鍵參考// 入口參數(shù):CH_AD 按鍵AD通道 // 出口參數(shù):*a 對應(yīng)該按鍵的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); //處理位數(shù)最大3位 取中值處理 RMov3Data_Byte(R_Save2,Tmp1) ; Tmp1 = C3MidVal(R_Save2); //處理位數(shù)最大3位 取中值處理 R_SaveSum += Tmp1 ; Tcon ++ ; if(Tcon >= (1<<3)) { *a = R_SaveSum>>3 ; Tcon = 0 ; R_SaveSum = 0 ; } }}//***************************************// 函數(shù)名稱:CheckTouch1// 函數(shù)功能:檢測觸摸按鍵// 入口參數(shù):無// 出口參數(shù):無//***************************************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 ; //當(dāng)前按鍵AD值 -- 用于測試 ^^^^^^^^^^^^^^^^^^^^#endif if(!(TKSTB & SETB0)) //此按鍵此時處于抬起狀態(tài) --更改參考值 { 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 }