基于AT89C51单片机的0~59秒计时器C语言程序
已有 444 次阅读2017-11-5 18:36
#include<reg51.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
uchar code dispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar sec1,sec2,sec3,sec4,sec,arcsec,num;
void delay();//延时函数
void main()
{
sec=0;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
EA=1;
TR0=1;
while(1)
{
arcsec=abs(99-sec);
// sec1=sec/10;
// sec2=sec%10;
// sec3=arcsec/10;
// sec4=arcsec%10;
P2=0x01;
P0=dispcode[sec/10];
delay();
P2=0x02;
P0=dispcode[sec%10];
delay();
P2=0x10;
P0=dispcode[arcsec/10];
delay();
P2=0x20;
P0=dispcode[arcsec%10];
delay();
}
}
void int0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
num++;
if(num==20)
{
num=0;
sec++;
if(sec>99)sec=0;
}
}
void delay()
{
unsigned int i;
for(i=0;i<125;i++);
}