用独立按键实现按一个按键时,数字从F到0递减的变化程序,即每按一次按键,数码管显示字符减一,减到0后重新显示。
#include <reg52.h>
sbit ADDR0= P2^2;
sbit ADDR1=P2^3;
sbit ADDR2=P2^4;
sbit key1=P3^0;
unsigned char code ledchar[] = {0x00,0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
bit keysta = 1;
void main()
{
bit backup = 1;
unsigned char cnt = 16;
EA = 1;
ADDR2 =0;
ADDR1= 0;
ADDR0 = 0;
TMOD = 0X01;
TH0 = 0XF8;
TL0 = 0XCD;
ET0 = 1;
TR0 = 1;
P0 = ~ledchar[cnt];
while(1)
{
if(keysta != backup)
{
if(backup == 0)
{
cnt--;
if(cnt < 1)
{
cnt = 16;
}
P0 = ~ledchar[cnt];
}
backup = keysta;
}
}
}
void InterruptTimer0() interrupt 1
{
static unsigned char keybuf = 0xff;
TH0 = 0XF8;
TL0 = 0XCD;
keybuf = (keybuf <<1)|key1;
if(keybuf == 0x00)
{
keysta = 0;
}
else if(keybuf == 0xff)
{
keysta = 1;
}
else
{}
}
|