思想:用一个定时器,实现10ms定时,每次定时时间到count加一,count=100时即为一秒,然后每一秒时间到就让初值减一(初值为99秒).以下为实现99秒倒计时的程序,用数码管显示出来.希望对你有帮助.
#include
unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//共阴数码管 0-9
unsigned char Dis_Shiwei;//定义十位
unsigned char Dis_Gewei; //定义个位
void delay(unsigned int cnt)
{
while(--cnt);
}
main()
{
TMOD |=0x01;//定时器设置 10ms in 12M crystal
TH0=0xd8;
TL0=0xf0;
IE= 0x82; //打开中断
TR0=1;
while(1)
{
P0=Dis_Shiwei;//显示十位
P2=0;
delay(300);//短暂延时
P0=Dis_Gewei; //显示个位
P2=1;
delay(300);
}
}
/********************************/
void tim(void) interrupt 1 using 1
{
static unsigned char second=99,count; //初值99
TH0=0xd8;//重新赋值
TL0=0xf0;
count++;
if (count==100)
{
count=0;
second--;//秒减1
if(second==0)
{second=99;} //减到0时,重新赋值99
Dis_Shiwei=tab[second/10];//十位显示值处理
Dis_Gewei=tab[second%10]; //个位显示处理
}
}
看我i文档里有个 ,行不行,我在给你找找看