51单片机_独立按键延时消抖_独立按键定时器消抖_矩阵键盘定时器消抖
1.独立按键_延时消抖
#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
};
void delay(uint dd);
void main(void)
{
bit backup = 1;
bit keybuff = 1;
uchar cnt = 0;
P0 = 0x3F;
duan = 1;
duan = 0;
P0 = 0x00;
wei = 1;
wei = 0;
P3 = 0xFF;
while(1)
{
keybuff = key4;
if(keybuff!=backup)
{
delay(100);
if(keybuff==key4)
{
if(backup==0)
{
cnt++;
if(cnt>=16)
{
cnt = 1;
}
P0 = duantable[cnt];
duan = 1;
duan = 0;
}
backup = key4;
}
}
}
}
void delay(uint dd)
{
uint tt;
for(tt=0;tt<dd;tt++){}
for(tt=0;tt<dd;tt++){}
for(tt=0;tt<dd;tt++){}
for(tt=0;tt<dd;tt++){}
for(tt=0;tt<dd;tt++){}
}
2.独立按键_定时器消抖
#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)
{
cnt++;
if(cnt>=16)
{
cnt = 0;
}
P0 = duantable[cnt];
duan = 1;
duan = 0;
}
backup = keystay;
}
}
}
void timer0(void) interrupt 1
{
static uchar keybuff = 0xFF;
TH0 = 0xF8;
TL0 = 0xCD;
keybuff = (keybuff << 1) | key3;
if(keybuff==0x00)
{
keystay = 0;
}
else if(keybuff==0xFF)
{
keystay = 1;
}
else{}
}
3.矩阵键盘_定时器消抖
#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
};
uchar keystay[4][4] =
{
{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}
};
void main(void)
{
uchar i = 0,j = 0;
uchar backup[4][4] =
{
{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}
};
P3 = 0xFF;
P0 = 0xFF;
duan = 1;
duan = 0;
P0 = 0x00;
wei = 1;
wei = 0;
TH0 = 0xFC;
TL0 = 0x67;
EA = 1;
ET0 = 1;
TMOD &= ~(0xF<<0);
TMOD |= 0x1<<0;
TR0 = 1;
while(1)
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(keystay[i][j]!=backup[i][j])
{
if(backup[i][j]==0)
{
P0 = duantable[i*4+j];
duan = 1;
duan = 0;
}
backup[i][j] = keystay[i][j];
}
}
}
}
}
void timer0(void) interrupt 1
{
uchar i = 0;
static uchar keyout = 0;
static uchar keybuff[4][4] =
{
{0xF,0xF,0xF,0xF},{0xF,0xF,0xF,0xF},{0xF,0xF,0xF,0xF},{0xF,0xF,0xF,0xF}
};
TH0 = 0xFC;
TL0 = 0x67;
keybuff[keyout][0] = (keybuff[keyout][0] << 1) | keyin1;
keybuff[keyout][1] = (keybuff[keyout][1] << 1) | keyin2;
keybuff[keyout][2] = (keybuff[keyout][2] << 1) | keyin3;
keybuff[keyout][3] = (keybuff[keyout][3] << 1) | keyin4;
for(i=0;i<4;i++)
{
if(keybuff[keyout][i]==0x0)
{
keystay[keyout][i] = 0;
}
else if(keybuff[keyout][i]==0xF)
{
keystay[keyout][i] = 1;
}
else{}
}
keyout++;
keyout &= 0x3;
switch(keyout)
{
case 0 : keyout4 = 1;keyout1 = 0;break;
case 1 : keyout1 = 1;keyout2 = 0;break;
case 2 : keyout2 = 1;keyout3 = 0;break;
case 3 : keyout3 = 1;keyout4 = 0;break;
default : break;
}
}