单片机定时器实时时钟程序.docx

上传人:b****0 文档编号:12485305 上传时间:2023-04-19 格式:DOCX 页数:12 大小:15.37KB
下载 相关 举报
单片机定时器实时时钟程序.docx_第1页
第1页 / 共12页
单片机定时器实时时钟程序.docx_第2页
第2页 / 共12页
单片机定时器实时时钟程序.docx_第3页
第3页 / 共12页
单片机定时器实时时钟程序.docx_第4页
第4页 / 共12页
单片机定时器实时时钟程序.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

单片机定时器实时时钟程序.docx

《单片机定时器实时时钟程序.docx》由会员分享,可在线阅读,更多相关《单片机定时器实时时钟程序.docx(12页珍藏版)》请在冰豆网上搜索。

单片机定时器实时时钟程序.docx

单片机定时器实时时钟程序

单片机定时器实时时钟程序

#include

typedefunsignedintuint;

typedefunsignedcharuchar;

sbitRS=P1^0;

sbitRW=P1^1;

sbitEN=P2^5;

sbitwela=P2^7;

sbitdula=P2^6;

sbitBUSY=P0^7;

sbitFM=P2^3;

ucharcounter,tshi,tfen,tmiao;

ucharsshi,sge,fshi,fge,mshi,mge;

uintnian;

ucharyue,ri;

ucharnqian,nbai,nshi,nge,yshi,yge,rshi,rge;

ucharcodetime[]="Time:

";

ucharcodedate[]="Date:

";

//delay:

xms

voiddelay(uintxms)

{

uchari,j;

for(i=0;i

for(j=0;j<125;j++);

}

//wait:

voidwait()

{

P0=0xff;

RS=0;

RW=1;

EN=0;

EN=1;

while(BUSY==1);

EN=0;

}

//write:

cmd

voidwrite_cmd(ucharcmd)

{

wait();

RS=0;

RW=0;

EN=0;

P0=cmd;

EN=1;

EN=0;

}

//write:

data

voidwrite_data(uchardat)

{

wait();

RS=1;

RW=0;

EN=0;

P0=dat;

EN=1;

EN=0;

}

//LCD1602:

init

voidinit()

{

nian=2016;

yue=6;

ri=13;

wela=0;

dula=0;

P1=0xff;

delay(10);

write_cmd(0x38);

delay(5);

write_cmd(0x38);

delay(5);

write_cmd(0x38);

wait();

write_cmd(0x38);

wait();

write_cmd(0x0c);

wait();

write_cmd(0x06);

wait();

write_cmd(0x01);

}

//timer1:

init

voidtimer1_init()

{

TMOD=0x10;

TH1=0xB8;

TL1=0x00;

TR1=1;

EA=1;

ET1=1;

}

//write:

byteaddress

voidwrite_pos(ucharadd)

{

write_cmd(add|0x80);

}

//write:

string

voidsend_string(uchar*str)

{

while(*str!

='')

{

write_data(*str++);

}

}

//dis:

date

voiddates()

{

nqian=nian/1000%10;

nbai=nian/100%10;

nshi=nian/10%10;

nge=nian%10;

yshi=yue/10%10;

yge=yue%10;

rshi=ri/10%10;

rge=ri%10;

write_pos(0x00);

send_string(time);

write_pos(0x09);

send_string(date);

write_pos(0x46);

write_data(nqian+0x30);

write_pos(0x47);

write_data(nbai+0x30);

write_pos(0x48);

write_data(nshi+0x30);

write_pos(0x49);

write_data(nge+0x30);

write_pos(0x4A);

write_data('-');

write_pos(0x4B);

write_data(yshi+0x30);

write_pos(0x4C);

write_data(yge+0x30);

write_pos(0x4D);

write_data('-');

write_pos(0x4E);

write_data(rshi+0x30);

write_pos(0x4F);

write_data(rge+0x30);

}

//dis:

time

voidtimes()

{

sshi=tshi/10%10;

sge=tshi%10;

fshi=tfen/10%10;

fge=tfen%10;

mshi=tmiao/10%10;

mge=tmiao%10;

write_pos(0x40);

write_data(sshi+0x30);

write_pos(0x41);

write_data(sge+0x30);

write_pos(0x43);

write_data(fshi+0x30);

write_pos(0x44);

write_data(fge+0x30);

write_pos(0x06);

write_data(mshi+0x30);

write_pos(0x07);

write_data(mge+0x30);

}

//keyscan:

voidmarixKeyscan()

{

uchartemp;

P3=0xfe;

temp=P3;

temp&=0xf0;

if(temp!

=0xf0)

{

delay(5);

temp=P3;

temp&=0xf0;

if(temp!

=0xf0)

{

FM=0;

temp=P3;

switch(temp)

{

case0xee:

tshi++;if(tshi>=24)tshi=0;break;

case0xde:

tfen++;if(tfen>=60)tfen=0;break;

case0xbe:

nian++;if(nian>=10000)nian=0;break;

case0x7e:

yue++;if(yue>=13)yue=1;break;

}

while(temp!

=0xf0)

{

temp=P3;

temp&=0xf0;

}

delay

(2);

while(temp!

=0xf0)

{

temp=P3;

temp&=0xf0;

}

FM=1;

}

}

P3=0xfd;

temp=P3;

temp&=0xf0;

if(temp!

=0xf0)

{

delay(5);

temp=P3;

temp&=0xf0;

if(temp!

=0xf0)

{

FM=0;

temp=P3;

switch(temp)

{

case0xed:

ri++;if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)

{

if(ri>=32)

{

ri=1;

}

}

if(yue==4||yue==6||yue==9||yue==11)

{

if(ri>=31)

{

ri=1;

}

}

if(yue==2)

{

if(((nian%4==0)&&(nian%100)!

=0)||(nian%400==0))

{

if(ri>=30)

{

ri=1;

}

}

else

{

if(ri>=29)

{

ri=1;

}

}

}

break;

case0xdd:

tshi--;if(tshi<=0){tshi=23;}break;

case0xbd:

tfen--;if(tfen<=0){tfen=59;}break;

case0x7d:

nian--;if(nian<=0){nian=10000;}break;

}

while(temp!

=0xf0)

{

temp=P3;

temp&=0xf0;

}

delay

(2);

while(temp!

=0xf0)

{

temp=P3;

temp&=0xf0;

}

FM=1;

}

}

P3=0xfb;

temp=P3;

temp&=0xf0;

if(temp!

=0xf0)

{

delay(5);

temp=P3;

temp&=0xf0;

if(temp!

=0xf0)

{

FM=0;

temp=P3;

switch(temp)

{

case0xeb:

ri--;

if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)

{

if(ri<=0)

{

ri=31;

}

}

if(yue==4||yue==6||yue==9||yue==11)

{

if(ri<=0)

{

ri=30;

}

}

if(yue==2)

{

if(((nian%4==0)&&(nian%100)!

=0)||(nian%400==0))

{

if(ri<=0)

{

ri=29;

}

}

else

{

if(ri<=0)

{

ri=28;

}

}

break;

case0xdb:

tshi=0;

tfen=0;

tmiao=0;

nian=0;

yue=1;

ri=1;

break;

}

while(temp!

=0xf0)

{

temp=P3;

temp&=0xf0;

}

delay

(2);

while(temp!

=0xf0)

{

temp=P3;

temp&=0xf0;

}

}

FM=1;

}

}

}

//main:

voidmain(void)

{

init();

timer1_init();

write_pos(0x42);

write_data(':

');

write_pos(0x06);

write_data('-');

while

(1)

{

times();

dates();

marixKeyscan();

if(counter==50)

{

counter=0;

tmiao++;

if(tmiao>=60)

{

tmiao=0;

tfen++;

if(tfen>=60)

{

tfen=0;

tshi++;

if(tshi>=24)

{

tshi=0;

ri++;

switch(yue)

{

case1:

case3:

case5:

case7:

case8:

case10:

case12:

if(ri>=32)

{

ri=1;

yue++;

}

break;

case4:

case6:

case9:

case11:

if(ri>=31)

{

ri=1;

yue++;

}

break;

case2:

if(((nian%4)==0&&(nian%100)!

=0)||(nian%400==0))

{

if(ri>=30)

{

ri=1;

yue++;

}

}

else

{

if(ri>=29)

{

ri=1;

yue++;

}

}

break;

default:

break;

}

if(yue>=13)

{

yue=1;

nian++;

if(nian>=10000)

{

nian=0;

}

}

}

}

}

}

}

}

//timr1:

int

voidtimer1_int()interrupt3

{

TH1=0xB8;

TL1=0x00;

counter++;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1