单片机数字钟程序
#include at89x52.h>
#include intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define A 1
uint year;
uchar count;
uchar flag=0;
uchar month,date,hour,min,sec;
uchar code monthdata[12]={31,28,31,30,31,30,31,31,30,31,30,31};
//uchar code disdata[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
uchar code disdata[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//uchar reg;
void sysini(void);
void timer2(void);
void delayX1ms(uint);
void delay(void);
void display(void);
void display2(uchar);
void change(void);
uchar weekday(void);
uchar daysofmonth(void) reentrant;
bit isleap(void) reentrant;
;c语言编写的单片机数字钟程序 http://www.51hei.com 出品
;主程序开始。
void main(void)
{sysini();
while(1)
{if(P3_2) display();//P3.0--mode,P3.1--set
else change();
}
display();
}
void sysini(void)
{
year=2004;month=1;date=1;
hour=0;min=0;sec=0;
count=0;
T2CON=0x00;T2MOD=0x00;
TH2=0x3c;TL2=0xb0;
RCAP2H=0x3c;RCAP2L=0xb0; //50ms
TR2=1;ET2=1;
SCON=0x00;
EA=1;
}
void timer2(void) interrupt 5
{TF2=0;
if(++count>=20)
{count=0;
if(++sec>=60)
{sec=0;
if(++min>=60)
{min=0;
if(++hour>=24)
{hour=0;
if(++date>daysofmonth())
{date=1;
if(++month>12)
{month=1;
year++;
}
}
}
}
}
}
}
void delayX1ms(uint count)
{uint i,j;
for(i=0;icount;i++)
for(j=0;j120;j++)//120
;
}
void delay(void)
{uchar a=10;
while(a)
a--;
}
void display(void)
{
SBUF=disdata[date%10];delay();P1=0xfe;delayX1ms(1);P1=0xff;
if(date/10) SBUF=disdata[date/10]; else SBUF=0x00;delay();P1=0xfd;delayX1ms(1);P1=0xff;
SBUF=disdata[month%10];delay();P1=0xfb;delayX1ms(1);P1=0xff;
if(month/10) SBUF=disdata[month/10]; else SBUF=0x00;delay();P1=0xf7;delayX1ms(1);P1=0xff;
SBUF=disdata[year%10];delay();P1=0xef;delayX1ms(1);P1=0xff;
SBUF=disdata[year/10%10];delay();P1=0xdf;delayX1ms(1);P1=0xff;
SBUF=disdata[year/100%10];delay();P1=0xbf;delayX1ms(1);P1=0xff;
SBUF=disdata[year/1000];delay();P1=0x7f;delayX1ms(1);P1=0xff;
SBUF=disdata[sec%10];delay();P2=0xfe;delayX1ms(1);P2=0xff;
SBUF=disdata[sec/10];delay();P2=0xfd;delayX1ms(1);P2=0xff;
SBUF=disdata[min%10];delay();P2=0xfb;delayX1ms(1);P2=0xff;
SBUF=disdata[min/10];delay();P2=0xf7;delayX1ms(1);P2=0xff;
SBUF=disdata[hour%10];delay();P2=0xef;delayX1ms(1);P2=0xff;
if(hour/10) SBUF=disdata[hour/10]; else SBUF=0x00;delay();P2=0xdf;delayX1ms(1);P2=0xff;
SBUF=disdata[weekday()];delay();P2=0xbf;delayX1ms(1);P2=0xff;
}
void display2(uchar a)
{
if(a=='d') SBUF=0x00;else SBUF=disdata[date%10];delay();P1=0xfe;delayX1ms(1);P1=0xff;
if(a=='d') SBUF=0x00;else{if(date/10) SBUF=disdata[date/10];else SBUF=0x00;}delay();P1=0xfd;delayX1ms(1);P1=0xff;
if(a=='m') SBUF=0x00;else SBUF=disdata[month%10];delay();P1=0xfb;delayX1ms(1);P1=0xff;
if(a=='m') SBUF=0x00;else{if(month/10) SBUF=disdata[month/10]; else SBUF=0x00;}delay();P1=0xf7;delayX1ms(1);P1=0xff;
if(a=='y') SBUF=0x00;else SBUF=disdata[year%10];delay();P1=0xef;delayX1ms(1);P1=0xff;
if(a=='y') SBUF=0x00;else SBUF=disdata[year/10%10];delay();P1=0xdf;delayX1ms(1);P1=0xff;
if(a=='y') SBUF=0x00;else SBUF=disdata[year/100%10];delay();P1=0xbf;delayX1ms(1);P1=0xff;
if(a=='y') SBUF=0x00;else SBUF=disdata[year/1000];delay();P1=0x7f;delayX1ms(1);P1=0xff;
if(a=='S') SBUF=0x00;else SBUF=disdata[sec%10];delay();P2=0xfe;delayX1ms(1);P2=0xff;
if(a=='S') SBUF=0x00;else SBUF=disdata[sec/10];delay();P2=0xfd;delayX1ms(1);P2=0xff;
if(a=='M') SBUF=0x00;else SBUF=disdata[min%10];delay();P2=0xfb;delayX1ms(1);P2=0xff;
if(a=='M') SBUF=0x00;else SBUF=disdata[min/10];delay();P2=0xf7;delayX1ms(1);P2=0xff;
if(a=='H') SBUF=0x00;else SBUF=disdata[hour%10];delay();P2=0xef;delayX1ms(1);P2=0xff;
if(a=='H') SBUF=0x00;else {if(hour/10) SBUF=disdata[hour/10]; else SBUF=0x00;}delay();P2=0xdf;delayX1ms(1);P2=0xff;
SBUF=disdata[weekday()];delay();P2=0xbf;delayX1ms(1);P2=0xff;
}
加入微信
获取电子行业最新资讯
搜索微信公众号:EEPW
或用微信扫描左侧二维码