|
#include<reg51.h>
#define uchar unsigned char
int i=0,s=0,f=0,z=0;ri=21,k=0,n=0;//最多只能调到日月年我没加上去
sbit E=P2^7;
sbit RW=P2^5;
sbit RS=P2^6;
sbit k1=P2^0;
sbit k2=P2^1;
sbit k3=P2^2;
sbit k4=P2^3;
sbit k5=P2^4;
sbit beep=P1^5;
//uchar sz[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar sz[]="TIME ";
uchar table2[]="DATE 2019-4-";
void Lcd1602_Delay1ms(int c) //误差 0us
{
uchar a,b;
for (; c>0; c--)
{
for (b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
LCDWriteCom(uchar com)
{
E=0;
RW=0;
RS=0;
P0=com;
E=1;
Lcd1602_Delay1ms(1);
E=0;
Lcd1602_Delay1ms(5);
}
LCDWriteDat(uchar dat)
{
E=0;
RW=0;
RS=1;
P0=dat;
E=1;
Lcd1602_Delay1ms(1);
E=0;
Lcd1602_Delay1ms(5);
}
LCDInit()
{
LCDWriteCom(0x38);
LCDWriteCom(0x0c);
LCDWriteCom(0x06);
LCDWriteCom(0x01);
}
zd() interrupt 1
{
TH0=0x3c;
TL0=0xb0;
i++;
if(i==20)
{
i=0;
s++;
if(s==60) //秒
{
s=0;
f++;
if(f==60) //分
{
f=0;
z++;
}
if(z==24) //时
{
z=0;ri++;//日
}
}
}
}
main()
{
TMOD=0x01; //定时器T0中断
TH0=0x3c;
TL0=0xb0;
ET0=1;
EA=1;
TR0=1;
LCDInit();
LCDWriteCom(0xc0);
for(i=0;i<13;i++)
{
LCDWriteDat(table2[i]);
}
LCDWriteCom(0x80);
for(i=0;i<6;i++)
LCDWriteDat(sz[i]);
while(1)
{
if(k1==0)//按钮k1 小时+1
{
Lcd1602_Delay1ms(100);
if(k1==0)
{
z++;
if(z==24)
z=0;
}
}
if(k2==0)
{
Lcd1602_Delay1ms(100);//按钮k2 分+1
if(k2==0)
{
f++;
if(f==60)
{
f=0;z++;
}
}
}
if(k3==0)
{
Lcd1602_Delay1ms(100); //按钮k3 秒+1
if(k3==0)
{
s++;
if(s==60)
{
s=0;f++;
}
}
}
if(k4==0)
{
Lcd1602_Delay1ms(10);
if(k4==0)
{
k++;
if(k==24)
{
k=0;
}
LCDWriteCom(0x85);
LCDWriteDat(0x30+(k/10));
LCDWriteCom(0x86);
LCDWriteDat(0x30+(k%10));
Lcd1602_Delay1ms(500);
}
}
if(k5==0)
{
Lcd1602_Delay1ms(10);
if(k5==0)
{
n++;
if(n==60)
{
n=0;
}
LCDWriteCom(0x88);
LCDWriteDat(0x30+(n/10));
LCDWriteCom(0x89);
LCDWriteDat(0x30+(n%10));
Lcd1602_Delay1ms(500);
}
}
if((z==k)&&(f==n))
{
beep=~beep;
Lcd1602_Delay1ms(100);
}
LCDWriteCom(0x85);
LCDWriteDat(0x30+(z/10));
LCDWriteCom(0x86);
LCDWriteDat(0x30+(z%10));
LCDWriteCom(0x87);
LCDWriteDat(':');
LCDWriteCom(0x88);
LCDWriteDat(0x30+(f/10));
LCDWriteCom(0x89);
LCDWriteDat(0x30+(f%10));
LCDWriteCom(0x8a);
LCDWriteDat(':');
LCDWriteCom(0x8b);
LCDWriteDat(0x30+(s/10));
LCDWriteCom(0x8c);
LCDWriteDat(0x30+(s%10));
LCDWriteCom(0xcc);
LCDWriteDat(0x30+(ri/10));
LCDWriteDat(0x30+(ri%10));
}
}
|
|