|
今天我编写完了我的程序 按键消抖独立控制:
#include <reg52.h>
code unsigned char zixing_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
code unsigned char wei_table[]={~0x01, ~0x02, ~0x04, ~0x08, ~0x10, ~0x20, ~0x40, ~0x80};
unsigned char disbuf[8]=0;
unsigned char count,counter,flag10msok,flag100msok;
sbit P1_0 = P1^0;//定义按键与引脚的链接
sbit P1_1 = P1^1;
sbit P1_2 = P1^2;
sbit P1_3 = P1^3;
sbit P1_4 = P1^4;
sbit P1_5 = P1^5;
sbit P1_6 = P1^6;
sbit P1_7 = P1^7;
unsigned char postion;
void display(void)//扫描数码管
{
P0 = zixing_table[disbuf[postion]];
P2 = wei_table[postion];
if(++postion>=8)
postion=0;
}
void timer0_isr(void) interrupt 1//中断服务程序
{
TL0=0xCD;
TH0=0xF8;
display();
if(++count>=5)
{
count=0;
flag10msok=1;
}
if(++counter>=50)
{
counter=0;
flag100msok=1;
}
}
unsigned char getkey0(void)//按键程序
{
if(!P1_0)
{
if(flag10msok==1) //按键消抖
{
if(!P1_0)
{
while(!P1_0)
;
return 1;
}
else
{
return 0;
}
}
}
else
return 0;
}
unsigned char getkey1(void)
{
if(!P1_1)
{
if(flag10msok==1)
{
if(!P1_1)
{
while(!P1_1)
;
return 1;
}
else
{
return 0;
}
}
}
else
return 0;
}
unsigned char getkey2(void)
{
if(!P1_2)
{
if(flag10msok==1)
{
if(!P1_2)
{
while(!P1_2)
;
return 1;
}
else
{
return 0;
}
}
}
else
return 0;
}
unsigned char getkey3(void)
{
if(!P1_3)
{
if(flag10msok==1)
{
if(!P1_3)
{
while(!P1_3)
;
return 1;
}
else
{
return 0;
}
}
}
else
return 0;
}
unsigned char getkey4(void)
{
if(!P1_4)
{
if(flag10msok==4)
{
if(!P1_4)
{
while(!P1_4)
;
return 1;
}
else
{
return 0;
}
}
}
else
return 0;
}
unsigned char getkey5(void)
{
if(!P1_5)
{
if(flag10msok==1)
{
if(!P1_5)
{
while(!P1_5)
;
return 1;
}
else
{
return 0;
}
}
}
else
return 0;
}
unsigned char getkey6(void)
{
if(!P1_6)
{
if(flag10msok==1)
{
if(!P1_6)
{
while(!P1_6)
;
return 1;
}
else
{
return 0;
}
}
}
else
return 0;
}
unsigned char getkey1(void)
{
if(!P1_7)
{
if(flag10msok==1)
{
if(!P1_7)
{
while(!P1_7)
;
return 1;
}
else
{
return 0;
}
}
}
else
return 0;
}
void init(void)//初始化
{
TMOD=0x01;
TL0=0xCD;
TH0=0xF8;
TR0=1;
IE = 0x82;
}
int main(void)//主程序
{
init();
while(1)
{
if(flag100msok==1)
{
if( getkey0() == 1 )
{
if(++disbuf[0]>=10)
disbuf[0]=0;
flag10msok=0;
}
if( getkey1() == 1 )
{
if(++disbuf[1]>=10)
disbuf[1]=0;
flag10msok=0;
}
if( getkey2() == 1 )
{
if(++disbuf[2]>=10)
disbuf[2]=0;
flag10msok=0;
}
if( getkey3() == 1 )
{
if(++disbuf[3]>=10)
disbuf[3]=0;
flag10msok=0;
}
if( getkey4() == 1 )
{
if(++disbuf[4]>=10)
disbuf[4]=0;
flag10msok=0;
}
if( getkey5() == 1 )
{
if(++disbuf[5]>=10)
disbuf[5]=0;
flag10msok=0;
}
if( getkey6() == 1 )
{
if(++disbuf[6]>=10)
disbuf[6]=0;
flag10msok=0;
}
if( getkey7() == 1 )
{
if(++disbuf[7]>=10)
disbuf[7]=0;
flag10msok=0;
}
}
}
}