觉得delay超级害人,让我查错误查了4个小时
然后去查了关于定时器来进行延时,按键消抖,数码管延时!
独立按键的中断消抖,先用中断来进行8ms的计时,然后判断按键是否按下,再来执行按键里面的任务
#include
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit wei = P2^7;
sbit duan = P2^6;
sbit key1 = P3^4;
sbit key2 = P3^5;
sbit key3 = P3^6;
sbit key4 = P3^7;
sbit keyout1 = P3^0;
sbit keyout2 = P3^1;
sbit keyout3 = P3^2;
sbit keyout4 = P3^3;
sbit keyin1 = P3^4;
sbit keyin2 = P3^5;
sbit keyin3 = P3^6;
sbit keyin4 = P3^7;
uchar code weitable[6] =
{
~0x20,~0x30,~0x38,~0x3C,~0x3E,~0x3F
};
uchar code duantable[16] =
{
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71
};
bit keystay = 1;
void main(void)
{
bit backup = 1;//储存前一次的按键值
uchar cnt = 0;//按键计数,记录按键按下的次数
P0 = 0x3F;
duan = 1;
duan = 0;
P0 = 0x00;
wei = 1;
wei = 0;
P3 = 0xFF;
TH0 = 0xF8;
TL0 = 0xCD;
EA = 1;
ET0 = 1;
TMOD &= ~(0xF<<0);
TMOD |= 0x1<<0;
TR0 = 1;
while(1)
{
if(keystay!=backup)//当前值与前次的值不相等说明此时按键有动作
{
if(backup==0)//如果前次的值为0,则说明当前是弹起
{
cnt++; //按键计数+1
if(cnt>=16)//加到16就重新开始,分别对应0~f的字符
{
cnt = 0;
}
P0 = duantable[cnt];//计数值显示到数码管上
duan = 1;
duan = 0;
}
backup = keystay;//更新为当前值,预备下次比较
}
}
}
void timer0(void) interrupt 1//中断8ms计时
{
static uchar keybuff = 0xFF;
TH0 = 0xF8;//定时8ms
TL0 = 0xCD;
keybuff = (keybuff << 1) | key3;//扫描8次,对按下的KEY3判断
if(keybuff==0x00)//8次都为0的话,则按下
{
keystay = 0;
}
else if(keybuff==0xFF)//8次都为1的话,则弹起
{
keystay = 1;
}
else{//按键状态未稳定}
}
矩阵按键同理,先将独立按键的代码分析透彻就懂了矩阵按键的中断处理了
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
sbit KEY_IN_1 = P3^4;
sbit KEY_IN_2 = P3^5;
sbit KEY_IN_3 = P3^6;
sbit KEY_IN_4 = P3^7;
sbit KEY_OUT_1 = P3^3;
sbit KEY_OUT_2 = P3^2;
sbit KEY_OUT_3 = P3^1;
sbit KEY_OUT_4 = P3^0;
unsigned char KeySta[4][4] = {//全部矩阵按键的当前状态
{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}
};
void main()
{
unsigned char i, j;
unsigned char backup[4][4] = { //按键值备份,保存前一次的值
{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}};
TMOD = 0x01; //设置 T0 为模式 1
TH0 = 0xFC; //为 T0 赋初值 0xFC67,定时 1ms
TL0 = 0x67;
EA=1;
ET0 = 1; //使能 T0 中断
TR0 = 1; //启动 T0
wela=1;
P0=0xc0;
wela=0;
P0=0xff;
dula=1;
P0 = table[0]; //默认显示 0
dula=0;
while (1){
for (i=0; i<4; i++)
{ //循环检测 4*4 的矩阵按键
for (j=0; j<4; j++)
{
if (backup[i][j] != KeySta[i][j])
{ //检测按键动作
if (backup[i][j] != 0)
{ //按键按下时执行动作
dula=1;
P0 = table[i*4+j]; //将编号显示到数码管
dula=0;
}
backup[i][j] = KeySta[i][j]; //更新前一次的备份值
}
}
}
}
}
/* T0 中断服务函数,扫描矩阵按键状态并消抖 */
void InterruptTimer0() interrupt 1
{
unsigned char i;
static unsigned char keyout = 0; //矩阵按键扫描输出索引
static unsigned char keybuf[4][4] =
{ //矩阵按键扫描缓冲区
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF}
};
TH0 = 0xFC; //重新加载初值
TL0 = 0x67;
//将一行的 4 个按键值移入缓冲区
keybuf[keyout][0] = (keybuf[keyout][0] << 1) | KEY_IN_1;
keybuf[keyout][1] = (keybuf[keyout][1] << 1) | KEY_IN_2;
keybuf[keyout][2] = (keybuf[keyout][2] << 1) | KEY_IN_3;
keybuf[keyout][3] = (keybuf[keyout][3] << 1) | KEY_IN_4;
//消抖后更新按键状态
for (i=0; i<4; i++)
{ //每行 4 个按键,所以循环 4 次
//连续 4 次扫描值为 0,即 4*4ms 内都是按下状态时,可认为按键已稳定的按下
if ((keybuf[keyout][i] & 0x0F) == 0x00)
{
KeySta[keyout][i] = 0;
//连续 4 次扫描值为 1,即 4*4ms 内都是弹起状态时,可认为按键已稳定的弹起
}
else if ((keybuf[keyout][i] & 0x0F) == 0x0F)
{
KeySta[keyout][i] = 1;
}
}
//执行下一次的扫描输出
keyout++; //输出索引递增
keyout = keyout & 0x03; //索引值加到 4 即归零
//根据索引,释放当前输出引脚,拉低下次的输出引脚
switch (keyout){
case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break;
case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break;
case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break;
case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break;
default: break;
}
}
说实话,我觉得这两个方法都超麻烦的
然后自己就将我的原来按键消抖的延时函数换成定时器计时
自己试了下,是可以用的,但消抖的作用大不大就不知道了
代码如下:
先要将定时器的函数写好
//time.h
//先建立好头文件,以备待会调用
void ini()//定时器
{
seccnt=0;
msta=tzsta=0;
TMOD=0x01;
TH0=0xFF; //定时100us
TL0=0x9C;
TR0=1; //开启定时器0
}
//void delay(u16 p)
//{
// while(p--);
//}
矩阵按键消抖
//juzheng.h
//将原先10ms的delay用定时器来编写
char KeyScan1()//矩阵按键
{
KEY_PORT = 0x0f; // P1.0-1.3输出高电平,P1.4-P1.7输出低电平
if (KEY_PORT != 0x0f) // 读取KEY_PORT看是否有按键按下
{
if(m%100==0) // 消抖
{
if (KEY_PORT != 0x0f) // 确认确实有按键按下
{
// 先确定按键发生在第几列
switch (KEY_PORT)
{
case 0x07 : val = 1; break;
case 0x0b : val = 2; break;
case 0x0d : val = 3; break;
case 0x0e : val = 4; break;
default : break;
}
// 再确定按键发生在第几行
KEY_PORT = 0xf0;
switch (KEY_PORT)
{
case 0x70: val = val + 0; break;
case 0xb0: val = val + 4; break;
case 0xd0: val = val + 8; break;
case 0xe0: val = val + 12; break;
}
return val;
}
}
}
return 0;
}
void work5()//按键1
{
}
void work6()//2
{
}
void work7()//3