定时器时钟
#include<reg52.h>
typedef unsigned char u8;
typedef unsigned int u16;
sbit k1=P3^1;
u8 code duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
u8 code wei[8]={0xe3,0xe7,0xeb,0xef,0xf3,0xf7,0xfb,0xff};
u8 duan2[8];
u8 i,x,y,z,p,a,b,c,d;
void delay(u16 j)
{
u8 m=100;
while(j--)
{while(m--);}
}
void main()
{
TMOD=0x01;//选择定时器0,方式1
TH0=0xfc;//赋给高8位初值
TL0=0Xa18;//赋给低8位初值64536
ET0=1;//中断源允许
TR0=1;//定时器启动
EA=1;
while(1)
{
for(i=0;i<8;i++)
{
P0=0;
P2=wei[i];
P0=duan2[i];
delay(1);
}
}
}
void time0() interrupt 1
{static u16 m=0;
TH0=0xfc;//赋给高8位初值
TL0=0X18;//赋给低8位初值64536
m++;
if(m==5)
{
duan2[0]=duan[x];
x++;
m=0;
}
if(x==10)
{ y++;
duan2[1]=duan[y];
x=0;
}
if(y==10)
{ z++;
duan2[2]=duan[z];
y=0;
}
if(z==10)
{ p++;
duan2[3]=duan[p];
z=0;
}
if(p==10)
{ a++;
duan2[3]=duan[a];
p=0;
}
if(a==10)
{ b++;
duan2[3]=duan[b];
a=0;
}
if(b==10)
{ c++;
duan2[3]=duan[c];
b=0;
}
if(c==10)
{ d++;
duan2[3]=duan[d];
c=0;
}
}
本文介绍了一个基于51单片机的定时器程序实现,通过定时器0以特定频率更新四位数码管显示的时间。程序使用了定时中断来递增时间,并通过循环更新数码管的显示内容。
1783

被折叠的 条评论
为什么被折叠?



