三种按键处理函数
1 void keyScan(void)
2 {
3char i;
4uint8 m;
5uint8 R0 = 0;
6TRISB=0x38; //xls 2012-11-26 增加复位键RB5
7
8R0 = PORTB; //RB3,RB4,RB5按键读取
9R0 &=0x38; //保留键值
10R0 = ~R0; //正逻辑
11/*以下是RC滤波处理,R0,R1對應於KEY1,KEY2的BUF*/
12datakey.allkey = (R0>>3);//将读到的键值移至最右端 比如当前在RB1 顾右移一位 如果为RB4 R3则右移3位
13for (i=0;i<3;i++)//这里根据有几个独立按键进行设置 只有一个按键时则i=1,三个则i=3
14{
15s_kcap[i]=((s_kcap[i]<<3)-s_kcap[i]+(datakey.allkey&0x01)<<4+4)>>3; //rc滤波及4舍5入
16datakey.allkey>>=1;
17if (s_kcap[i]>12)
18{
19datakey.allkey|=0x80;
20}
21}
22m=datakey.allkey;
23datakey.allkey=(datalastkey.allkey^datakey.allkey)&datalastkey.allkey;//後沿處理
24datalastkey.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 _ON1
14 #define _OFF0
15 #define _TRUE1
16 #define _FLASE0
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 bitk0@ (unsigned)&key1*8+0;
7 static volatile bitk1@ (unsigned)&key1*8+1;
8 static volatile bitk2@ (unsigned)&key1*8+2;
9 static volatile bitk3@ (unsigned)&key1*8+3;
10 static volatile bitk4@ (unsigned)&key1*8+4;
11 static volatile bitk5@ (unsigned)&key1*8+5;
12 static volatile bitk6@ (unsigned)&key1*8+6;
13 static volatile bitk7@ (unsigned)&key1*8+7;
14
15 static volatile bitk8@ (unsigned)&key2*8+0;
16 static volatile bitk9@ (unsigned)&key2*8+1;
17 static volatile bitk10@ (unsigned)&key2*8+2;
18 static volatile bitk11@ (unsigned)&key2*8+3;
19
20 static volatile bitlk0@ (unsigned)&lkey1*8+0;
21 static volatile bitlk1@ (unsigned)&lkey1*8+1;
22 static volatile bitlk2@ (unsigned)&lkey1*8+2;
23 static volatile bitlk3@ (unsigned)&lkey1*8+3;
24 static volatile bitlk4@ (unsigned)&lkey1*8+4;
25 static volatile bitlk5@ (unsigned)&lkey1*8+5;
26 static volatile bitlk6@ (unsigned)&lkey1*8+6;
27 static volatile bitlk7@ (unsigned)&lkey1*8+7;
28
29 static volatile bitlk8@ (unsigned)&lkey2*8+0;
30 static volatile bitlk9@ (unsigned)&lkey2*8+1;
31 static volatile bitlk10 @ (unsigned)&lkey2*8+2;
32 static volatile bitlk11 @ (unsigned)&lkey2*8+3;
33
34 uchar keycnt;
35
36 void keyscan(void)//按键扫描
37 {
38keycnt++;
39xs0=xs1=xs2=xs3=xs4=xs5=xs6=xs7=RD7=0; //
40
41TRISD=0X0F;//
42
43RD6=1;RD5=0;RD4=1;//(k4~k7)
44asm("nop");asm("nop");
45R0=(RD^0X0f)&0x0f;
46R0=R0<<4;
47
48RD4=0;RD5=1;RD6=1;//(k0~k3)
49asm("nop");asm("nop");
50R0+=((RD^0X0F)&0X0F);
51
52RD6=0;RD5=1;RD4=1;//(k8~k11
53asm("nop");asm("nop");
54R1=(RD^0X0f)&0x0f;
55
56if(R0|R1)//有键?
57{
58if(keycnt>=12)
59{keycnt=0;
60key1=R0;key2=R1;
61}
62else
63{key1=lkey1;//lkey1电平值,key1,前沿值
64key2=lkey2;
65}
66}
67else
68{key1=key2=0;
69keycnt=0;
70}
71R2=key1;R3=key2;
72key1=key1&(lkey1^key1);lkey1=R2;
73key2=key2&(lkey2^key2);lkey2=R3;
74 }
第三种:
1 BOOL Switch2IsPressed(void)
2 {
3if(sw2 != old_sw2)
4{
5old_sw2 = sw2;// Save new value
6if(sw2 == 0)// If pressed
7return TRUE;// Was pressed
8}//end if
9return FALSE;// Was not pressed
10 }//end Switch2IsPressed
11
12
13 typedef enum _BOOL { FALSE = 0, TRUE } BOOL;
【三种按键处理函数】
推荐阅读
- 沉降缝规范 沉降缝防水处理
- 三种衣帽间的设计流行趋势
- 整体衣柜背板厚度多少比较好 三种整体衣柜背板厚度比较
- 欧式衣柜材质—三种常见欧式衣柜材质的介绍
- 全友家居衣柜—全友家居衣柜三种类型
- 家居木地板基层施工方法
- 舒适木地板靠装修 三种地面施工你须了解
- 卫生间墙面防水做法 卫生间防水处理方法
- 如何防止厨卫渗漏?卫生间防水处理方法
- 卫生间防水处理方法 选购防水材料的原则和技巧