原帖链接:https://www.cnblogs.com/wangh0802PositiveANDupward/p/3178471.html
按键是接:RB3'4'5 ;没键为高,按下去 为0
void keyScan(void)
{
char i;
uint8 m;
uint8 R0 = 0;
TRISB=0x38; //xls 2012-11-26 增加复位键RB5
R0 = PORTB; //RB3,RB4,RB5按键读取
R0 &=0x38; //保留键值
R0 = ~R0; //正逻辑
/*以下是RC滤波处理,R0,R1對應於KEY1,KEY2的BUF*/
datakey.allkey = (R0>>3);//将读到的键值移至最右端 比如当前在RB1 顾右移一位 如果为RB4 R3则右移3位
for (i=0;i<3;i++)//这里根据有几个独立按键进行设置 只有一个按键时则i=1,三个则i=3
{
s_kcap[i]=((s_kcap[i]<<3)-s_kcap[i]+(datakey.allkey&0x01)<<4+4)>>3; //rc滤波及4舍5入
datakey.allkey>>=1;
if (s_kcap[i]>12)
{
datakey.allkey|=0x80;
}
}
m=datakey.allkey;
datakey.allkey=(datalastkey.allkey^datakey.allkey) &datalastkey.allkey;//後沿處理
datalastkey.allkey=m;
//datakey.allkey=(datalastkey.allkey^datakey.allkey)&datakey.allkey; //前沿處理
//datalastkey.allkey=m;
}
extern volatile union key datakey;
extern volatile union lastkey datalastkey;
extern volatile union fistkey datafistkey;
//板上按键定义
#define K2 (datakey.allkey&0x40)
#define K3 (datakey.allkey&0x80) //
#define K1 (datakey.allkey&0x20) //
#define LK2 (datalastkey.allkey&0x40)
#define LK3 (datalastkey.allkey&0x80) //extern volatile union var key;
#define LK1 (datalastkey.allkey&0x20)
#ifndef _MYDEFINE_H
#define _MYDEFINE_H
#define BIT bit
#define uint8 unsigned char
#define uint16 unsigned int
#define uint32 unsigned long int
#define BT_CFG2_6 RA0
#define BT_CFG1_6 RA2
#define ON_OFF_SLEEP_WAKE_B RA3
#define _ON 1
#define _OFF 0
#define _TRUE 1
#define _FLASE 0
#define _PRINTF _OFF
#define Acsii(x) x+0x30
一般放主循环里 10ms或20ms一次
|