51單片機程序——按鍵控制數(shù)碼管變化
#define uint unsigned int
#define uchar unsigned char
#define dula P0
#define wela P1
sbit key=P3^2;
unsigned char code DuanMa[]={0xff,0xc0,0xf9,0xa4,0xb0,
uchar num;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void main()
{
while(1)
{
if(key==0) //延遲再判斷,確保按鍵確實被按下
{
delay(5);
if(key==0)
{
num++;
if(num==17)
num=0;
}
while(!key); //同理,判斷鍵盤確實已經松開
delay(5);
}
wela=0xff; //位選都打開,8位數(shù)碼管都顯示
dula=DuanMa[num]; //送段碼
}
}
評論