本文為大家分享一個可通過按鍵來調節時間的1602液晶顯示的數字時鐘程序。
詳細程序
/***************************************
程序功能:
LCD液晶顯示屏顯示時鐘并且可以修改時間
***************************************/
#include《reg52.h》
#define uint unsigned int
#define uchar unsigned char
sbit K1=P1^0;//設置三個獨立鍵盤
sbit K2=P1^3;
sbit K3=P1^5;
sbit lcdwrite=P2^5;
sbit lcddatecommand=P2^6;
sbit lcde=P2^7;
uchar code firstline[]=“2013-06-03”;
uchar code secondline[]=“00-00-00”;
uchar i;
uchar hour,minutes,seconds;
uchar count,num;
uchar cnt;
void delay(uint z)
{
uint x,y;
for(x=z;x》0;x--)
{
for(y=0;y《=112;y++)
{
}
}
}
void write_command(uchar command)
{
lcddatecommand=0;
lcdwrite=0;
P0=command;
delay(1);
lcde=1;
delay(1);
lcde=0;
}
void write_date(uchar date)
{
lcddatecommand=1;
lcdwrite=0;
P0=date;
delay(1);
lcde=1;
delay(1);
lcde=0;
}
void LCDinit()
{
lcde=0;
write_command(0x38);//設置16*2顯示,5*7點陣,8位數據接口
write_command(0x0f);//設置開顯示,不顯示光標
write_command(0x06);// 寫一個字符后地址指針加1
write_command(0x80);
for(i=0;i《10;i++)
{
write_date(firstline[i]);
delay(1);
}
write_command(0x80+0x40);
for(i=0;i《8;i++)
{
write_date(secondline[i]);
delay(1);
}
}
void Time0init()
{
TMOD=0x01;//開啟定時器1
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void Clockinit()
{
hour=0;
minutes=0;
seconds=0;
count=0;
num=0;
}
void display(uchar hour,uchar minutes,uchar seconds)//接收并發送數據到液晶屏幕
{
uchar hourge,hourshi,minutesshi,minutesge,secondsshi,secondsge;
hourshi=hour/10;
hourge=hour%10;
write_command(0x80+0x40);
write_date(hourshi+48);
delay(1);
write_date(hourge+48);
delay(1);
minutesshi=minutes/10;
minutesge=minutes%10;
write_command(0x80+0x43);
write_date(minutesshi+48);
delay(1);
write_date(minutesge+48);
delay(1);
secondsshi=seconds/10;
secondsge=seconds%10;
write_command(0x80+0x46);
write_date(secondsshi+48);
delay(1);
write_date(secondsge+48);
delay(1);
}
void revise()
{
if(K1==0)
{
delay(5);
if(K1==0)
{
cnt++;//計數第幾次按下
TR0=0;
}
while(!K1);
delay(5);
while(!K1);
}
if(cnt==1)//第一次按下,調整秒鐘,調整時秒鐘有光標閃爍
{
write_command(0x80+0x46);
write_command(0x0f);
if(K2==0)
{
delay(5);
if(K2==0)
{
seconds++;
if(seconds==60)
{
seconds=0;
}
}
while(!K2);
delay(5);
while(!K2);
}
if(K3==0)
{
delay(5);
if(K3==0)
{
seconds--;
if(seconds==-1)//here is a bug
{
seconds=59;
}
}
while(!K3);
delay(5);
while(!K3);
}
}
if(cnt==2)//第二次按下,調整分鐘,調整時秒鐘有光標閃爍
{
write_command(0x80+0x43);
write_command(0x0f);
if(K2==0)
{
delay(5);
if(K2==0)
{
minutes++;
if(minutes==60)
{
minutes=0;
}
}
while(!K2);
delay(5);
while(!K2);
}
if(K3==0)
{
delay(5);
if(K3==0)
{
minutes--;
if(minutes==-1)//here is a bug
{
minutes=59;
}
}
while(!K3);
delay(5);
while(!K3);
}
}
if(cnt==3)//第三次按下,調整時鐘,調整時秒鐘有光標閃爍
{
write_command(0x80+0x40);
write_command(0x0f);
if(K2==0)
{
delay(5);
if(K2==0)
{
hour++;
if(hour==24)
{
hour=0;
}
}
while(!K2);
delay(5);
while(!K2);
}
if(K3==0)
{
delay(5);
if(K3==0)
{
hour--;
if(hour==-1)//here is a bug
{
hour=23;
}
}
while(!K3);
delay(5);
while(!K3);
}
}
if(cnt==4)
{
TR0=1;
cnt=0;
}
}
void main()
{
LCDinit();
Time0init();
Clockinit();
while(1)
{
revise();
display(hour,minutes,seconds);
}
}
void Time0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
count=0;
seconds++;
if(seconds==60)
{
seconds=0;
minutes++;
if(minutes==60)
{
minutes=0;
hour++;
if(hour==24)
{
hour=0;
}
}
}
}
}
評論
查看更多