按键程序#include<reg52.h>
sbit AA=P2^2; sbit BB=P2^3; sbit CC=P2^4;
sbit key_in_1=P1^0; sbit key_in_2=P1^1;
sbit key_in_3=P1^2; sbit key_in_4=P1^3;
sbit key_out_1=P1^4; sbit key_out_2=P1^5;
sbit key_out_3=P1^6; sbit key_out_4=P1^7;
unsigned char keysta[4][4]={{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
unsigned char code led[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void main()
{
unsigned char i,j;
unsigned char keybackup[4][4]={{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
EA=1;
AA=0;BB=0;CC=0;
TMOD=0X01;
TH0=0XFC;
TL0=0X67;
ET0=1;
TR0=1;
P0=led[0];
while(1)
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(keysta[i][j]!=keybackup[i][j])
{
if(keybackup[i][j]!=0)
{
P0=led[i*4+j];
}
}
keybackup[i][j]=keysta[i][j];
}
}
}
}
void Interrupt
timer0() interrupt 1
{
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}};
unsigned char i;
TH0=0XFC;
TL0=0X67;
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++)
{
if(keybuf[keyout][i]&0x0f==0X0F)
{
keysta[keyout][i]=1;
}
else if(keybuf[keyout][i]&0x0f==0X00)
{
keysta[keyout][i]=0;
}
}
keyout++;
keyout=keyout&0x30;
switch(keyout)
{
case 0:key_out_4=0;key_out_1=1;break;
case 1:key_out_3=0;key_out_4=1;break;
case 2:key_out_2=0;key_out_3=1;break;
case 3:key_out_1=0;key_out_2=1;break;
default:break;
}
}
一周热门 更多>