#include
#define uchar unsigned char
#define uint unsigned int
const char tab[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x40};
char time[3]; //时分秒计数
char dis_buff[8]; //显示缓冲区,存放要显示的的6个字符的段码值 //和两个分隔符
char time_counter; //1s计数器
void delayus(unsigned int t)
{unsigned int i;
while(t--)
for(i=1330;i>0;i--);
}
void display(void) //扫描显示函数,执行时间16ms
{
static char i;
P3OUT=0xFF;
P1OUT=tab[dis_buff[i]]; //字段码送数码管
P3OUT=~(1<
if(++i==8)i=0;
delayus(2000);
}
void time_to_disbuffer(void)
{
dis_buff[0]=time[2]/10; //小时
dis_buff[1]=time[2]%10;
dis_buff[3]=time[1]/10; //分钟
dis_buff[4]=time[1]%10;
dis_buff[6]=time[0]/10; //秒
dis_buff[7]=time[0]%10;
}
void main(void)
{
P1DIR=0xFF; //p1初始化为输出端口
P3DIR=0xFF; //p2初始化为输出端口
time[2]=0;
time[1]=0;
time[0]=0; //时间初值00;00;00
time_to_disbuffer();
dis_buff[2]=dis_buff[5]=10;
while(1)
{
display(); //显示扫描
if(++time_counter>=40) //更新时间
{
time_counter=0;
if(++time[0]>=60)
{
time[0]=0;
if(++time[1]>=60)
{
time[1]=0;
if(++time[2]>=24)time[2]=0;
}
}
time_to_disbuffer(); //修改显示缓冲区
}
}
}