#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit key1=P1^0;
sbit key2=P1^1;
sbit keyup=P1^2;
sbit keydw=P1^3; sbit beep=P2^3; sbit lcdrs=P1^4;
sbit lcdrw=P1^5;
sbit lcden=P1^6;
uchar table2[]={"0123456789-"};
int
m1,m2,m3,m4,m5,m6,k,n,p,q,m8,w,a,ri,yue,nian; void delayms(int j)
{
int i;
for(j;j>0;j--)
for(i=125;i>0;i--);
} // LCD写地址
void writecom(uchar com)
{
lcden=0;
lcdrs=0;
lcdrw=0;
delayms(1);
lcden=1;
delayms(1);
P0=com;
delayms(1);
lcden=0;
} //LCD写数据 void writedata(uchar dat)
{
lcden=0;
lcdrs=1;
lcdrw=0;
delayms(1);
lcden=1;
delayms(1);
P0=dat;
delayms(1);
lcden=0;
} // LCD初始化 void LCD_Init()
{
delayms(15);
writecom(0x38);
delayms(5);
writecom(0x38);
delayms(5);
writecom(0x38);
writecom(0x38);
writecom(0x08);
writecom(0x01);
writecom(0x06);
writecom(0x0c);
} void beepinit(void)
{
beep=0;
delayms(100);
beep=1;
}
void beep2()
{
beep=0;
delayms(500);
beep=1;
}
void display()
{
writecom(0x80+0x02);
writedata(table2[nian/1000]);
writedata(table2[nian%1000/100]);
writedata(table2[nian%100/10]);
writedata(table2[nian%10]);
writedata(table2[10]);
writedata(table2[yue/10]);
writedata(table2[yue%10]);
writedata(table2[10]);
writedata(table2[ri/10]);
writedata(table2[ri%10]);
writecom(0x80+0x40);
writedata(table2[m1/10]);
writedata(table2[m1%10]);
writedata(table2[10]);
writedata(table2[m2/10]);
writedata(table2[m2%10]);
writedata(table2[10]);
writedata(table2[m3/10]);
writedata(table2[m3%10]);
writecom(0x80+0x48);
writedata(table2[m4/10]);
writedata(table2[m4%10]);
writedata(table2[10]);
writedata(table2[m5/10]);
writedata(table2[m5%10]);
writedata(table2[10]);
writedata(table2[m6/10]);
writedata(table2[m6%10]);
if(k>=20)
{
k=0;
m3++;
m8++;
} if(m3>=60)
{
m3=m3-60;
m2++;
} if(m2>=60)
{
m2=m2-60;
m1++;
} if(m1>=24)
{
m1=m1-24;
ri++;
} switch(yue)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(ri>=32)
{
yue++;
ri=ri-31;
}
break;
case 4:
case 6:
case 9:
case 11:
if(ri>=31)
{
yue++;
ri=ri-30;
}
break;
case 2:
if((nian%100==0&nian%400==0)||(nian%100!=0&nian%4==0))
{
if(ri>=29)
{
yue++;
ri=ri-28;
}
}
else
if(ri>=30)
{
yue++;
ri=ri-29;
}
break;
default:
yue=1;
break;
}
if(m6>=60)
{
m6=m6-60;
m5++;
}
if(m5>=60)
{
m5=m5-60;
m4++;
}
if(m4>=24)
{
m4=m4-24;
}
} //上调时钟
void keyupinit()
{
if(p==1) //时间上调
{
if(!keyup)
{
delayms(1);
if(!keyup)
{
switch(n)
{
case 1: m3=m3+1;
break;
case 2: m3=m3+10;
break;
case 3: m2=m2+1;
break;
case 4: m2=m2+10;
break;
case 5: m1=m1+1;
break;
case 6: m1=m1+10;
break;
case 7: ri=ri+1;
break;
case 8: ri=ri+10;
break;
case 9: yue=yue+1;
break;
case 10: yue=yue+10;
break;
case 11: nian=nian+1;
break;
case 12: nian=nian+10;
break;
case 13: nian=nian+100;
break;
case 14: nian=nian+1000;
break;
}
beepinit();
while(!keyup);
}
}
} if(p==2) // 闹钟上调
{
if(!keyup)
{ delayms(1);
if(!keyup)
{
switch(n)
{
case 1: m6=m6+1;
break;
case 2: m6=m6+10;
break;
case 3: m5=m5+1;
break;
case 4: m5=m5+10;
break;
case 5: m4=m4+1;
break;
case 6: m4=m4+10;
break;
}
beepinit();
while(!keyup);
}
}
}
} //下调时钟 void keydwinit()
{
if(p==1) // 时间下调
{
if(!keydw)
{
delayms(1);
if(!keydw)
{
switch(n)
{
case 1: m3=m3-1;
break;
case 2: m3=m3-10;
break;
case 3: m2=m2-1;
break;
case 4: m2=m2-10;
break;
case 5: m1=m1-1;
break;
case 6: m1=m1-10;
break;
case 7: ri=ri-1;
break;
case 8: ri=ri-10;
break;
case 9: yue=yue-1;
break;
case 10: yue=yue-10;
break;
case 11: nian=nian-1;
break;
case 12: nian=nian-10;
break;
case 13: nian=nian-100;
break;
case 14: nian=nian-1000;
break;
}
beepinit();
while(!keydw);
}
}
} if(p==2) // 闹钟下调
{
if(!keydw)
{
delayms(1);
if(!keydw)
{
switch(n)
{
case 1: m6=m6-1;
break;
case 2: m6=m6-10;
break;
case 3: m5=m5-1;
break;
case 4: m5=m5-10;
break;
case 5: m4=m4-1;
break;
case 6: m4=m4-10;
break;
}
beepinit();
while(!keydw);
}
}
}
}
//选择位数
//p:模式选择 void xuanze()
{
n=1;
p=1;
while(p==1) //时间模式
{
if(!key2)
{
delayms(5);
beepinit();
while(!key2);
n=n+1;
}
if(n==15)n=1; keyupinit();
keydwinit(); if(m1<0) m1=m1+24;
if(m2<0) m2=m2+60;
if(m3<0) m3=m3+60; display(); if(!key1)
{
delayms(2);
beepinit();
while(!key1);
p=2;
}
} delayms(1);
n=1;
while(p==2) //闹钟模式
{
if(!key2)
{
delayms(5);
beepinit();
while(!key2);
n=n+1;
} if(n==7)n=1;
keyupinit();
keydwinit();
if(m4<0) m4=m4+24;
if(m5<0) m5=m5+60;
if(m6<0) m6=m6+60;
display();
if(!key1)
{
delayms(2);
beepinit();
while(!key1);
p=3;
}
}
}
//选择模式
void key1init()
{
TR0=0;
xuanze();
delayms(5);
if(p==3)
{
TR0=1;
}
} void main()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1; //总开关
ET0=1; //定时器t0中断允许
TR0=1; //开启定时器
m1=m2=m3=q=m4=m5=m6=0;
nian=2014;
yue=01;
ri=01;
LCD_Init();
while(1)
display();
while(1)
{
if(!key1)
{
delayms(2);
beepinit();
while(!key1);
key1init();
delayms(3);
}
if((m4==0)&&(m5==0)&&(m6==0)) // 初始值不计闹钟
{
w=0;
}
else w=1;
if((m1==m4)&&(m2==m5)&&(m3==m6)&&w) //判断闹钟
{
q=1;
m8=0;
}
if(q==1)
{
if(m8<=30)
{
beep2();
}
else
q=0;
}
display();
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
k++;
}
END
;
|