三种按键处理函数

时间:2022-07-21 16:14:59
 1 void keyScan(void)
 2 {
 3      char i;
 4      uint8 m;
 5      uint8 R0 = 0;
 6      TRISB=0x38; //xls 2012-11-26 增加复位键RB5
 7 
 8      R0 = PORTB; //RB3,RB4,RB5按键读取
 9     R0 &=0x38; //保留键值
10     R0 = ~R0; //正逻辑
11     /*以下是RC滤波处理,R0,R1對應於KEY1,KEY2的BUF*/
12      datakey.allkey = (R0>>3);//将读到的键值移至最右端 比如当前在RB1 顾右移一位 如果为RB4 R3则右移3位
13     for (i=0;i<3;i++)//这里根据有几个独立按键进行设置 只有一个按键时则i=1,三个则i=3  
14     {
15        s_kcap[i]=((s_kcap[i]<<3)-s_kcap[i]+(datakey.allkey&0x01)<<4+4)>>3; //rc滤波及4舍5入
16      datakey.allkey>>=1;
17       if (s_kcap[i]>12)
18      {
19         datakey.allkey|=0x80;
20      } 
21   } 
22    m=datakey.allkey;
23    datakey.allkey=(datalastkey.allkey^datakey.allkey)        &datalastkey.allkey;//後沿處理
24    datalastkey.allkey=m;
25 
26 //datakey.allkey=(datalastkey.allkey^datakey.allkey)&datakey.allkey;  //前沿處理
27 //datalastkey.allkey=m;
28 }

按键是接:RB3'4'5 ;没键为高,按下去 为0

 1 extern volatile union key datakey;
 2 extern volatile union lastkey datalastkey;
 3 extern volatile union fistkey datafistkey;
 4 //板上按键定义
 5 #define K2 (datakey.allkey&0x40) 
 6 #define K3 (datakey.allkey&0x80) //
 7 #define K1 (datakey.allkey&0x20) //
 8 
 9 #define LK2  (datalastkey.allkey&0x40)
10 #define LK3  (datalastkey.allkey&0x80) //extern volatile union var key;
11 #define LK1  (datalastkey.allkey&0x20)
 1 #ifndef _MYDEFINE_H
 2 #define _MYDEFINE_H
 3 
 4 #define BIT bit
 5 #define uint8  unsigned char
 6 #define uint16 unsigned int
 7 #define uint32 unsigned long int
 8 
 9 #define BT_CFG2_6  RA0 
10 #define BT_CFG1_6  RA2
11 #define ON_OFF_SLEEP_WAKE_B RA3
12 
13 #define _ON                                1
14 #define _OFF                               0
15 #define _TRUE                           1
16 #define _FLASE                            0
17 
18 #define _PRINTF                  _OFF
19 
20 #define Acsii(x)               x+0x30 


三种按键处理函数一般放主循环里 10ms或20ms一次
第二种:
 1 static volatile bank3 uchar key1  @ 0x6b;        //定义十二个按键,电平触发
 2 static volatile bank3 uchar key2  @ 0x6c;
 3 static volatile bank3 uchar lkey1 @ 0x6d;        //定义十二个按键,前沿触发
 4 static volatile bank3 uchar lkey2 @ 0x6f;
 5 
 6 static volatile  bit    k0    @ (unsigned)&key1*8+0;
 7 static volatile  bit    k1    @ (unsigned)&key1*8+1;
 8 static volatile  bit    k2    @ (unsigned)&key1*8+2;
 9 static volatile  bit    k3    @ (unsigned)&key1*8+3;
10 static volatile  bit    k4    @ (unsigned)&key1*8+4;
11 static volatile  bit    k5    @ (unsigned)&key1*8+5;
12 static volatile  bit    k6    @ (unsigned)&key1*8+6;
13 static volatile  bit    k7    @ (unsigned)&key1*8+7;
14 
15 static volatile bit    k8    @ (unsigned)&key2*8+0;
16 static volatile bit    k9    @ (unsigned)&key2*8+1;
17 static volatile bit    k10    @ (unsigned)&key2*8+2;
18 static volatile bit    k11    @ (unsigned)&key2*8+3;
19 
20 static volatile bit    lk0    @ (unsigned)&lkey1*8+0;
21 static volatile bit    lk1    @ (unsigned)&lkey1*8+1;
22 static volatile bit    lk2    @ (unsigned)&lkey1*8+2;
23 static volatile bit    lk3    @ (unsigned)&lkey1*8+3;
24 static volatile bit    lk4    @ (unsigned)&lkey1*8+4;
25 static volatile bit    lk5    @ (unsigned)&lkey1*8+5;
26 static volatile bit    lk6    @ (unsigned)&lkey1*8+6;
27 static volatile bit    lk7    @ (unsigned)&lkey1*8+7;
28 
29 static volatile bit    lk8    @ (unsigned)&lkey2*8+0;
30 static volatile bit    lk9    @ (unsigned)&lkey2*8+1;
31 static volatile bit    lk10 @ (unsigned)&lkey2*8+2;
32 static volatile bit    lk11 @ (unsigned)&lkey2*8+3;
33 
34 uchar keycnt;
35 
36 void keyscan(void)                    //按键扫描
37 {
38     keycnt++;
39     xs0=xs1=xs2=xs3=xs4=xs5=xs6=xs7=RD7=0; //
40 
41     TRISD=0X0F;                    //
42 
43     RD6=1;RD5=0;RD4=1;            //(k4~k7)
44     asm("nop");asm("nop");
45     R0=(RD^0X0f)&0x0f;
46     R0=R0<<4;
47 
48     RD4=0;RD5=1;RD6=1;            //(k0~k3)
49     asm("nop");asm("nop");
50     R0+=((RD^0X0F)&0X0F);
51 
52     RD6=0;RD5=1;RD4=1;            //(k8~k11
53     asm("nop");asm("nop");
54     R1=(RD^0X0f)&0x0f;
55 
56     if(R0|R1)                    //有键?
57      {     
58           if(keycnt>=12)
59             {keycnt=0;
60               key1=R0;key2=R1;
61             }
62            else
63             {key1=lkey1;//lkey1电平值,key1,前沿值
64              key2=lkey2;
65             }
66     }
67     else
68         {key1=key2=0;
69          keycnt=0;
70         }
71     R2=key1;R3=key2;
72     key1=key1&(lkey1^key1);    lkey1=R2;
73     key2=key2&(lkey2^key2);    lkey2=R3;
74 }

第三种:

 1 BOOL Switch2IsPressed(void)
 2 {
 3     if(sw2 != old_sw2)
 4     {
 5         old_sw2 = sw2;                  // Save new value
 6         if(sw2 == 0)                    // If pressed
 7             return TRUE;                // Was pressed
 8     }//end if
 9     return FALSE;                       // Was not pressed
10 }//end Switch2IsPressed
11 
12 
13 typedef enum _BOOL { FALSE = 0, TRUE } BOOL;