采用8位数码管实现倒计时设计。
仿真原理图:
程序设计:
代码语言:javascript复制#include<reg52.h>
typedef unsigned int u16; //对数据类型进行声明定义
typedef unsigned char u8;
unsigned char LedBuff[]={
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
unsigned char code LedChar[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e
};//共阳
/*unsigned char code LedChar[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
};//共阴*/
unsigned char cnt=0;
unsigned long sec=999999;
unsigned char i=0;
void delay(u16 i)
{
while(i--);
}
void main()
{
TMOD=0x01;
TH0=0xEC;
TL0=0x78;
TR0=1;
EA=1;
ET0=1;
while(1)
{
if(cnt>=150)//溢出次1秒
{
cnt=0;
sec--;
LedBuff[0]= LedChar[sec];
LedBuff[1]= LedChar[sec/10];
LedBuff[2]= LedChar[sec/100];
LedBuff[3]= LedChar[sec/1000];
LedBuff[4]= LedChar[sec/10000];
LedBuff[5]= LedChar[sec/100000];
}
}
}
void InterruptTimer0() interrupt 1
{
TH0=0XEC;
TL0=0X78;//计时器溢出一次0.001秒
cnt ;
P0=0xFF;
for(i=0;i<6;i )
{
P0=0xff;
switch(i) //位选,选择点亮的数码管,
{
case(0):
P1=0x20; break;//显示第0位
case(1):
P1=0x10; break;//显示第1位
case(2):
P1=0x08; break;//显示第2位
case(3):
P1=0x04; break;//显示第3位
case(4):
P1=0x02; break;//显示第4位
case(5):
P1=0x01; break;//显示第5位
default: break;
}
P0=LedBuff[i];//发送段码
delay(10); //间隔一段时间扫描
P0=0xFF;
}
}
仿真视频: http://mpvideo.qpic.cn/0bf2regdeaamf4abya4hpzpvzcodgkeqymqa.f10002.mp4?