小弟学单片机的时间不长,自己写了个51单片机控制1602液晶显示器显示电子表的程序,但是调试程序的时候用按键调时间的时候老是在秒的后面还显示出两个数字,不知道这是为什么,秒后面并没有让他显示,还是显示了数字。希望高手不吝赐教;源程序如下:
#include<reg52.h>
sbit RS=P1^0;
sbit RW=P1^1;
sbit EN=P1^2;
sbit K1=P3^2;
sbit K2=P3^3;
sbit K3=P3^4;
unsigned int second,minute,hour;
unsigned int cnt;
unsigned int ms;
unsigned char x=0x88;
unsigned char y=0x85;
unsigned char z=0x82;
//*********延时******************
void yanshi(ms)
{
unsigned int i;
while(ms--)
for(i=120;i>0;i--);
}
//*********写命令*********
write_cmd(unsigned char cmd)
{
RS=0;
RW=0;
P0=cmd;
yanshi(5);
EN=1;
yanshi(5);
EN=0;
}
//*****************写数据***********
write_data(unsigned char date)
{
RS=1;
RW=0;
P0=date;
yanshi(5);
EN=1;
yanshi(5);
EN=0;
}
//*******初始化*************
void init()
{
EN=0;
write_cmd(0x38);
write_cmd(0x0e);
write_cmd(0x06);
write_cmd(0x01);
write_cmd(0x80);
write_cmd(0x84);
write_data(':');
write_cmd(0x87);
write_data(':');
}
//*********************写时间*******
write_SFM(unsigned char date)
{
unsigned char shi,fen;
shi=date/10;
fen=date%10;
write_data(shi+0x30);
write_data(fen+0x30);
}
//*********主函数入口**********
main()
{
TMOD=0x02;
TH0=0xa4;
TL0=0xa4;
ET0=1;
EA=1;
TR0=1;
init();
while(1)
{
if(K1==0)
{
yanshi(50);
if(K1==0)
{
second++;
if(second==60)
{
second=0;
}
write_cmd(x);
write_SFM(second);
while(K1==0);
}
}
if(K2==0)
{
yanshi(50);
if(K2==0)
{
minute++;
if(minute==60)
{
minute=0;
}
write_cmd(0x85);
write_SFM(minute);
while(K2==0);
}
}
if(K3==0)
{
yanshi(50);
if(K3==0)
{
hour++;
if(hour==24)
{
hour=0;
}
write_cmd(z);
write_SFM(hour);
while(K3==0);
}
}
}
}
void t0() interrupt 1
{
TH0=0xa4;
TL0=0xa4;
cnt++;
if(cnt==10000)
{
cnt=0;
second++;
write_cmd(x);
write_SFM(second);
if(second==60)
{
second=0;
minute++;
write_cmd(y);
write_SFM(minute);
if(minute==60)
{
minute=0;
hour++;
write_cmd(z);
write_SFM(hour);
if(hour==24)
{
hour=0;
}
}
}
}
}
欢迎光临 (http://www.51hei.com/bbs/) | Powered by Discuz! X3.1 |