资源简介

基于51单片机1602显示 实时时钟 利用单片机内部定时器做时钟,带有按键控制,具有停止时间,加减时间显示的功能。闰年补偿

资源截图

代码片段和文件信息

#include
#define uchar  unsigned char
#define uint unsigned int


sbit lcdrs=P2^5;
sbit lcdrw=P2^6;
sbit lcden=P2^7;
sbit s1=P3^2;
sbit s2=P3^3;
sbit s3=P3^4;



uchar code table[]={“2012 00 00“};
uchar code table1[]={“00:00:00“};

uchar numflags1num;
int miaofenshiriyuenian;




void delay(uint z)
{
uint xy;
for(x=z;x>0;x--)
{
for(y=147;y>0;y--);
}
}


void write_control(uchar con)//写控制字

P0=con;
lcdrs=0;//rs=0控制
lcdrw=0;
lcden=0;
delay(5);
lcden=1;
delay(5);

}

void write_data(uchar dt)//写数据
{
P0=dt;
lcdrs=1;//rs=1数据
lcdrw=0;
lcden=0;
delay(5);
lcden=1;
delay(5);

}



void init()//初始化  屏幕显示  定时器初值
{
nian=2012;
write_control(0x01);//控制初始  1602
write_control(0x38);
write_control(0x0c);
write_control(0x06);
write_control(0xd0);

write_control(0x80);//初始化显示内容 2012 3 28
for(num=0;num<10;num++)
write_data(table[num]);

write_control(0x80+0x40);// 00:00:00
for(num=0;num<8;num++)
write_data(table1[num]);

TMOD=0x01;
TH0=(65536-5000)/256;//5ms
TL0=(65536-5000)%256;
EA=1;
ET0=1;
TR0=1;


}


void write_sfm(uchar adduchar date)//写入 时分秒 的数值
{
uchar shige;
shi=date/10;
ge=date%10;
write_control(0x80+0x40+add);
write_data(0x30+shi);//求 个位  十位的ASCIIA码  进而显示
write_data(0x30+ge);
}


void write_yr(uchar add1uchar date1)//写入 月日 的数值
{
uchar shige;
shi=date1/10;
ge=date1%10;
write_control(0x80+add1);
write_data(0x30+shi);//求 个位  十位的ASCIIA码  进而显示
write_data(0x30+ge);
}


void write_nian(uchar add2uint date2)//写入 年 的数值
{
uchar qianbaishige;
qian=date2/1000;
bai=date2/100%10;
shi=date2/10%10;
ge=date2%10;
write_control(0x80+add2);//求 千百十个位  的ASCIIA码  进而显示
write_data(0x30+qian);
write_data(0x30+bai);
write_data(0x30+shi);
write_data(0x30+ge);
}






void timer0()  interrupt 1//定时器中断程序  计时显示
{
TH0=(65536-5000)/256;//5ms   5.42534722222222222
TL0=(65536-5000)%256;
flag++;
if(flag==184)//1秒
{
flag=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
ri++;
if((ri>31)&&((yue==1)||(yue==3)||(yue==5)||(yue==7)||(yue==8)||(yue==10)||(yue==12)))
{
ri=1;
yue++;
if(yue==12)
{
yue=1;
nian++;
write_nian(0nian);
}
write_yr(5yue);
}
if((ri==31)&&((yue==4)||(yue==6)||(yue==9)||(yue==11)))
{
ri=1;
yue++;
if(yue==12)
{
yue=1;
nian++;
write_nian(0nian);
}
write_yr(5yue);

}
if((ri==30)&&(yue==2))
{

if(nian%4==0)
{
if(nian%100==0)
{
if(nian%400==0)
{
ri=1;
yue++;
if(yue==12)
{
yue=1;
nian++;
write_nian(0nian);
}
writ

评论

共有 条评论