新1602下的闹钟程序.docx
《新1602下的闹钟程序.docx》由会员分享,可在线阅读,更多相关《新1602下的闹钟程序.docx(18页珍藏版)》请在冰豆网上搜索。
![新1602下的闹钟程序.docx](https://file1.bdocx.com/fileroot1/2023-1/24/34de712c-0838-4d55-a190-82e7bc765a04/34de712c-0838-4d55-a190-82e7bc765a041.gif)
新1602下的闹钟程序
1602下的闹钟程序
#include"reg52.h"
#defineucharunsignedchar
#defineuintunsignedint
#definen115
#definen214
#definey0x01
#definee5
sbitwela=P2^7;
sbitdula=P2^6;
sbitrs=P3^5;
sbitlcden=P3^4;
sbitrd=P3^7;
sbits0=P3^0;//themodesetnum:
one--thetimetwo--thealarm
sbits1=P3^1;//themodewhenthesetnum=one
sbits2=P3^2;//up
sbits3=P3^3;//down
sbitbeep=P2^3;//thebeep
uchari;
ucharcodetable[]="0000-00-00MON";
ucharcodetable1[]="00:
00:
0011Co";
ucharcodenmon[]={0,31,28,31,30,31,30,31,31,30,31,30,31};//thecommonyear
ucharcodelmon[]={0,31,29,31,30,31,30,31,31,30,31,30,31};//theleapyear
uchartt,sec,min,hour,day,week,mon;
uchart0,sec1,min1,hour1;
ucharnum,setnum,bnum;//forthemode
intyear;
voiddelay(ucharz)
{
uchari,j;
for(i=125;i>0;i--)
for(j=z;j>0;j--);
}
voidwrite_cmd(ucharcmd)//writecommond
{
rs=0;
P0=cmd;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
voidwrite_data(uchardat)//writedata
{
rs=1;
P0=dat;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
voidlcd_init()//theinit
{
wela=0;
dula=0;
lcden=0;
write_cmd(0x38);
write_cmd(0x0f);
write_cmd(0x06);
write_cmd(0x01);
write_cmd(0x80);
for(i=0;i{
write_data(table[i]);//one0xwrite_data(0x38)one0xthesame
delay
(2);
}
write_cmd(0x80+0x40);
for(i=0;i{
write_data(table1[i]);
delay
(2);
}
}
voidtimer_init()
{
TMOD=0x11;
TH0=(65536-50000)/256;
TL0=(65536-50000)%6;
TH1=(65536-50000)/256;
TL1=(65536-50000)%6;
EA=1;
ET0=1;
ET1=1;
TR0=1;
}
voidlcd_yeardisplay()//displaytheyear
{
inta1,a2,a3,a4;
write_cmd(0x80);
a1=year/1000;
a2=year/100;
a3=year/10;
a4=year;
write_data(0x30+a1);
write_data(0x30+a2);
write_data(0x30+a3);
write_data(0x30+a4);
}
voidlcd_mddisplay(ucharadd,uchardate)//displaythemonthandday
{
uchard1,d2;
write_cmd(0x80+add);
d1=date/10;
d2=date;
write_data(0x30+d1);
write_data(0x30+d2);
}
voidlcd_hmsdisplay(ucharadd,uchardate)
{
uchart1,t2;
write_cmd(0x80+0x40+add);
t1=date/10;
t2=date;
write_data(0x30+t1);
write_data(0x30+t2);
}
voidtime_display()
{
if(setnum==0||setnum==1)
{
lcd_yeardisplay();
lcd_mddisplay(0x05,mon);
lcd_mddisplay(0x08,day);
lcd_hmsdisplay(0x00,hour);
lcd_hmsdisplay(0x03,min);
lcd_hmsdisplay(0x06,sec);
}
}
voidalarm_display()
{
if(setnum==2)
{
lcd_hmsdisplay(0x00,hour1);
lcd_hmsdisplay(0x03,min1);
lcd_hmsdisplay(0x06,sec1);
}
}
voidset_1display()
{
if(num==6)//theyear
{
write_cmd(0x80);
write_cmd(0x0f);
}
if(num==5)//themonth
{
write_cmd(0x80+0x05);
write_cmd(0x0f);
}
if(num==4)//theday
{
write_cmd(0x80+0x08);
}
if(num==3)//thehour
{
write_cmd(0x80+0x40);
}
if(num==2)//theminute
{
write_cmd(0x80+0x40);
}
if(num==1)//thesecond
{
write_cmd(0x80+0x40+0x03);
}
if(num==1)
{
write_cmd(0x80+0x40+0x06);
write_cmd(0x0f);
}
write_cmd(0x0f);
}
voidtimekeeper()
{
if(tt==20)
{
tt=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
day++;
week++;
if(week==8)
{
week=0;
}
if((year%4==0&&year0!
=0)||year@0==0)//theleapyear
{
if(day==lmon[mon]+1)
{
day=1;
mon++;
if(mon==13)
{
mon=1;
year++;
}
}
}
else
{
if(day==nmon[mon]+1)
{
day=1;
mon++;
if(mon==13)
{
mon=1;
year++;
}
}
}
}
}
}
}
}
voidtime_keyscan()
{
if(setnum==1)
{
if(s1==0)
{
delay(5);
if(s1==0)
{
while(!
s1);
//year=1111;//forthedebug
//mon=11;
//day=11;
num++;
TR0=0;
if(num==7)
{
num=0;
TR0=1;
write_cmd(0x0c);
}
set_1display();
}
}
if(num!
=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
if(num==1)//thesecond
{
sec++;
if(sec==60)
{
sec=0;
}
}
if(num==2)//theminute
{
min++;
if(min==60)
{
min=0;
}
}
if(num==3)//thehour
{
hour++;
if(hour==24)
{
hour=0;
}
}
if(num==4)//theday
{
day++;
if((year%4==0&&year0!
=0)||year@0==0)
{
if(day==lmon[mon]+1)
{
day=1;
}
}
else
{
if(day==nmon[mon]+1)
{
day=1;
}
}
}
if(num==5)//themonth
{
mon++;
if(mon==13)
{
mon=1;
}
}
if(num==6)//theyear
{
year++;
if(year==10000)
{
year=0;
}
}
write_cmd(0x0f);
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
if(num==1)//thesecond
{
sec--;
if(sec==-1)
{
sec=59;
}
}
if(num==2)//theminute
{
min--;
if(min==-1)
{
min=59;
}
}
if(num==3)//thehour
{
hour--;
if(hour==-1)
{
hour=23;
}
}
if(num==4)//theday
{
day--;
if((year%4==0&&year0!
=0)||year@0==0)
{
if(day==0)
{
day=lmon[mon];
}
}
else
{
if(day==0)
{
day=nmon[mon];
}
}
}
if(num==5)//themonth
{
mon--;
if(mon==0)
{
mon=12;
}
}
if(num==6)//theyear
{
year--;
if(year==-1)
{
year=9999;
}
}
}
}
}
}
}
voidalarm_keyscan()
{
if(setnum==2)
{
if(s1==0)
{
delay(5);
if(s1==0)
{
while(!
s1);
year=0;//forthedebug,ifsetnum==2ands1==0thiswordwillworkorwillnot
mon=0;
day=0;
num++;
TR0=0;
if(num==4)
{
num=0;
TR0=1;
write_cmd(0x0c);
}
set_1display();
}
}
if(num!
=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
if(num==1)//thesecond
{
sec1++;
if(sec1==60)
{
sec1=0;
}
}
if(num==2)//theminute
{
min1++;
if(min1==60)
{
min1=0;
}
}
if(num==3)//thehour
{
hour1++;
if(hour1==24)
{
hour1=0;
}
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
if(num==1)//thesecond
{
sec1--;
if(sec1==-1)
{
sec1=60;
}
}
if(num==2)//theminute
{
min1--;
if(min1==-1)
{
min1=59;
}
}
if(num==3)//thehour
{
hour1--;
if(hour1==-1)
{
hour1=23;
}
}
}
}
}
}
}
voidset()
{
if(s0==0)
{
delay(5);
if(s0==0)
{
setnum++;
if(setnum==3)
{
setnum=0;
}
}
}
}
voidmain()
{
timer_init();
lcd_init();
year=0000;
mon=2;
day=28;
hour=23;
min=59;
sec=58;
hour1=0;
min1=0;
sec1=3;
while
(1)
{
set();
time_keyscan();
alarm_keyscan();
//time_alarm();
time_display();
alarm_display();
}
}
voidtimer0()interrupt1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%6;
tt++;
timekeeper();
}
voidtimer1()interrupt3
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%6;
t0++;
if(t0==40)
{
beep=1;
}
}