51单片机定时器数码管30秒倒计时(三个按键控制开始暂停复位)
- 格式:docx
- 大小:138.11 KB
- 文档页数:3
程序:
#include ""
unsigned char code led[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; unsigned char code addr[2]={0xef,0xdf};
unsigned char xx[2];
unsigned char time=30;
unsigned char flag=0;
void delay(void);
void sys(void)
{
TMOD=0x01;
TH0=(65536 - 50000) / 256;
TL0=(65536 - 50000) % 256;
ET0=1;
TR0=1;
EA=1;
EX0=1;
IT0=1;
}
void ftimer0(void) interrupt 1
{
static unsigned char cnt;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
cnt++;
if(cnt>20)
{
if(flag==1)
{
time--;
cnt=0;
if(time==0)
{
ET0=0;
TR0=0;
}
}
}
}
void int0(void) interrupt 0
{
if(P2_0==0)
{
flag=1;
ET0=1;
}
else if(P2_1==0)
{
ET0=0;
}
else if(P2_2==0)
{
time=30;
ET0=0;
TR0=1;
}
}
void main()
{
unsigned char a;
sys();
for(;;)
{
for(a=0;a<2;a++)
{
xx[0]=time/10;
xx[1]=time%10;
P0=led[xx[a]];
P1=addr[a];
delay();
P1=0xff;
}
}
}
void delay(void)
{
long t=100;
while (t--);
}
电路图及结果: