单片机60秒计时器的程序,用AT89C51实现

如题所述

//1:用AT89C51单片机的定时/计数器T0产生一秒的
//定时时间,作为秒计数时间,当一秒产生时,秒
//计数加1,秒计数到60时,自动从0开始。单片机
//晶振频率为12MHZ
#include<reg51.h>
#define
uchar
unsigned
char
#define
uint
unsigned
int
uchar
code
table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
0x6f};
void
display(uchar
fen,uchar
miao);
uchar
a,fen,miao,shu;
void
delay(uint
z);
void
init();
void
main()
{
init();
while(1)
{
display(fen,miao);
}
}
void
timer0()
interrupt
1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
a++;
if(a==20)
{
a=0;
shu++;
fen=shu/10;
miao=shu%10;
if(shu==60)
shu=0;
}
}
void
init()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void
display(uchar
fen,uchar
miao)
{
P1=0xFe;
P2=table[fen];
delay(5);//延时5毫秒
P1=0xFf;
P2=table[miao];
delay(5);//延时5毫秒
}
void
delay(uint
z)
{
uint
x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
温馨提示:答案为网友推荐,仅供参考
大家正在搜