在做时间调整时遇到一个问题,就是按下调整按键时,光标在正确的位置闪烁,但按下加以后,光标向后一了一位。具体程序如下:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code array1[]=" 2010-11-06 Sat ";
uchar code array2[]=" 00:00:00";
uchar num=0,s1num=0;
char sec,minu,hour;
sbit lcdrs=P1^0;
sbit lcdrw=P1^1;
sbit lcden=P1^2;
sbit L=P0^7;
sbit s1=P0^0; //时间调整键
sbit s2=P0^1; //时间调整加一键
sbit s3=P0^2; //时间调整减一键
void delay(uchar z) //延时大约z毫秒函数
{
uchar i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void write_com(uchar com) //液晶写指令函数
{
lcdrs=0;
lcdrw=0;
P2=com;
lcden=0;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_dat(uchar dat) //液晶写数据函数
{
lcdrs=1;
lcdrw=0;
P2=dat;
lcden=0;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init_lcd() //初始化液晶函数
{
uchar m,n;
lcden=0;
lcdrw=0;
write_com(0x38);
write_com(0x01);
write_com(0x0c);
write_com(0x06);
write_com(0x80);
for(m=0;m<15;m++)
{
write_dat(array1[m]);
delay(5);
}
write_com(0x80+0x40);
for(n=0;n<12;n++)
{
write_dat(array2[n]);
delay(5);
}
}
void init_timer1() //初始化定时器1
{
TMOD|=0x10;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
EA=1;
ET1=1;
TR1=1;
}
void write_smh(uchar add,char date) //刷新液晶显示函数
{
uint shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_dat(0x30+shi);
write_dat(0x30+ge);
}
void key_scan() //按键扫描函数
{
L=0;
if(s1==0)
{
delay(5);
if(s1==0)
{
s1num++;
while(!s1);
if(s1num==1)
{
TR1=0;
write_com(0x80+0x40+11);
write_com(0x0f);
}
if(s1num==2)
{
write_com(0x80+0x40+8);
}
if(s1num==3)
{
write_com(0x80+0x40+5);
}
if(s1num==4)
{
s1num=0;
write_com(0x0c);
TR1=1;
}
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
write_com(0x80+0x40+10);
sec++;
if(sec==60) sec=0;
write_smh(10,sec);
}
if(s1num==2)
{
write_com(0x80+0x40+7);
minu++;
if(minu==60) minu=0;
write_smh(7,minu);
}
if(s1num==3)
{
write_com(0x80+0x40+4);
hour++;
if(hour==24) hour=0;
write_smh(4,hour);
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
sec--;
if(sec==-1)
sec=59;
write_smh(10,sec);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
minu--;
if(minu==-1)
minu=59;
write_smh(7,minu);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
hour--;
if(hour==-1)
hour=23;
write_smh(4,hour);
write_com(0x80+0x40+4);
}
}
}
}
}
void main() //主函数
{
init_lcd();
init_timer1();
while(1)
{
key_scan();
}
}
void timer1() interrupt 3 //定时器1中断服务函数
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
num++;
if(num==18)
{
num=0;
sec++;
if(sec==60)
{
sec=0;
minu++;
if(minu==60)
{
minu=0;
hour++;
if(hour==24)
{
hour=0;
}
write_smh(4,hour);
}
write_smh(7,minu);
}
write_smh(10,sec);
}
} |