刚开始学单片机 自己练习写了几个程序 这个抢答器程序,抢答按钮按下后,在液晶1602上显示的计数时间后面怎么总是多了几位数字 不知道是哪里蹦出来的?求大神们帮忙看看。谢谢了。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]=" 0";
uchar code table1[]=" 00:00:00";
sbit rs=P3^7;
sbit wr=P3^6;
sbit en=P3^5;
sbit K1=P2^7;
sbit K2=P2^6;
//sbit K3=P2^5;
//sbit K4=P2^4;
//sbit K5=P2^3;
//sbit K6=P2^2;
//sbit K7=P2^1;
//sbit K8=P2^0;
uchar t,miao,time,flag=0,aa=0,bb=0;
void delay(int z)
{
int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
rs=0;
P0=com;
delay(5);
en=1;
delay(5);
en=0;
}
void write_date(uchar date)
{
rs=1;
P0=date;
delay(5);
en=1;
delay(5);
en=0;
}
void init()
{
uchar i;
wr=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(i=0;i<10;i++)
{
write_date(table[i]);
delay(5);
}
write_com(0x80+0x40);
for(i=0;i<12;i++)
{
write_date(table1[i]);
delay(5);
}
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
TR0=1;
EA=1;
}
void write_sj(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void write_qd(uchar add,uchar date)
{
write_com(0x80+add);
write_date(0x30+date);
}
void main()
{
uchar qt=0;
init();
while(!(aa%2))
{
if(K1==0)
{
delay(5);
if(K1==0)
{
while(!K1);
aa++;
EA=1;
}
}
write_sj(12,time);
write_com(0x80+0x40+12);
}
while(1)
{
if(bb)
{
if(K1==0)
{
delay(5);
if(K1==0)
{
while(!K1);
bb=0;
time=0;
EA=1;
flag=0;
qt=0;
// bz();
}
}
}
if(!flag)
{
if(K2==0)
{
delay(5);
if(K2==0)
{
while(!K2);
EA=0;
qt=1;
flag=1;
bb=1;
}
}
}
write_qd(10,qt);
write_com(0x80+10);
}
}
void time0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t++;
if(t==20)
{
t=0;
miao++;
if(miao==60)
{
miao=0;
}
write_sj(10,miao);
}
} |