单片机原理课程设计基于AT89C52的电子时钟设计说明 下载本文

delay1ms(uchar time) //延时1ms { uchar i,j;

for(i=0;i

//LCD驱动部分 enable() { rs=0; rw=0; e=0;

delay1ms(3); e=1; }

write2(uchar i) { P0=i; rs=1; rw=0; e=0;

delay1ms(2); e=1; }

write1(uchar data *address,m) { uchar i,j;

for(i=0;i

//LCD显示 lcdshow()

{ P0=0XC; //显示器开、光标关 enable();

P0=0x80; //第一行0~15 enable();

write1(time1,16);

P0=0xc1; //第二行1~14 enable();

write1(time2,15); }

//DS1302读写子程序 write(uchar address) { uchar i; clk=0;

_nop_(); rst=1; _nop_();

for(i=0;i<8;i++) { dat=address&1; _nop_(); clk=1;

address>>=1; clk=0; } }

uchar read() { uchar i,j=0; for(i=0;i<8;i++) { j>>=1; _nop_(); clk=0; _nop_(); if(dat)

j|=0x80; _nop_(); clk=1; }

return(j); }

//部分显示数据初始化 timestart() {

time1[6]=time1[9]='-';

time1[2]='2',time1[3]='0'; time2[2]=time2[5]=':'; write(0xc1);

alarm[0]=read(); rst=0;

write(0xc3);

alarm[1]=read(); rst=0;

write(0xc5); time1[0]=read(); rst=0; }

//读取时间 readtime()

{ uchar i,m,n;

write(0x8d); //读取年份

m=read(); rst=0;

time1[4]=m/16+0x30; time1[5]=m+0x30;

time1[15]=m+0x30;

for(i=7,n=0x89;i<11;i+=3,n-=2) //读取月份和日期 { write(n); m=read(); rst=0;

time1[i]=m/16+0x30; time1[i+1]=m+0x30; }

for(m=0,i=0,n=0x85;i<7;i+=3,n-=2,m++) //读取时,分,秒 { write(n);

time[m]=read(); //将实时的时分秒给time[] rst=0;

time2[i]=time[m]/16+0x30; time2[i+1]=time[m]+0x30; } }

baoshi() //整点报时 { beep=1;

if(time[1]==0) if(time[2]==0)

{beep=0;delay(10);beep=1;} }

//闹钟部分 showalarm() { uchar i;

for(i=1;i<10;i++)

{beep=0;delay(1000);beep=1;} }

//根据日期的变动自动调整星期

//设置时间 settime() {

uchar i=0x85,year,month,day,n;

time2[6]=time2[7]=0x30,time1[14]=time1[15]=0x20;

lcdshow(); while(1) {

P0=0x0e; //显示器开显示、开光标 enable();

P0=i; //定光标 enable(); P1=0xf0;

if(P1!=0Xf0) //有按钮按下 {

delay1ms(100); //延时0.1s去抖动 if(P1!=0Xf0) {

j=7;

if(P1==0X70) //K1,设置时钟 {

i+=3;

if(i==0x8e) i=0xc2;

else if(i>0xc5) i=0x85; }

else if(P1==0xb0) //K2,上调 {

year=(time1[4]&0xf)*10+(time1[5]&0xf); month=(time1[7]&0xf)*10+(time1[8]&0xf); day=(time1[10]&0xf)*10+(time1[11]&0xf); if(i==0x85) {

year++; if(year>99) year=0;

if((year%4)!=0)

if(month==2&&day==29) day=28; }

else if(i==0x88) {

month++; if(month>12) month=1;

if(day>Day[month-1]) {

day=Day[month-1];

if(month==2&&(year%4)==0)

}

day=29;

} }

else if(i==0x8b) {

day++;

if(day>Day[month-1]) {

if(month==2&&(year%4)==0) {

if(day>29) day=1; }

if(month!=2) day=1; } }

else if(i==0xc2) {

n=(time2[0]&0xf)*10+(time2[1]&0xf); n++; if(n>23) n=0;

time2[0]=n/10+0x30; time2[1]=n+0x30; } else {

n=(time2[3]&0xf)*10+(time2[4]&0xf); n++; if(n>59) n=0;

time2[3]=n/10+0x30; time2[4]=n+0x30; }

time1[4]=year/10+0x30; time1[5]=year+0x30; time1[7]=month/10+0x30; time1[8]=month+0x30; time1[10]=day/10+0x30; time1[11]=day+0x30; lcdshow();

else if(P1==0xd0) //K3,下调