本帖最后由 liaobiaoxing 于 2009-8-29 21:53 编辑
自己看了半天,终于找到错误了。显示正确了,谢谢回复贴子的人。
#include<reg52.h>
sbit E=P2^7;
sbit RS=P2^5;
sbit RW=P2^6;
unsigned char show[8]={'0','0','-','0','0','-','0','0'};
unsigned int count=0,sec=0,min=0,hour=0;
void delay(unsigned char x)
{ unsigned int a,b;
for(a=0;a<=x;a++)
for(b=0;b<=10;b++);
}
void time0_init()
{ EA=1;
ET0=1;
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
}
void time0_int() interrupt 1
{ TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==10)
{ count=0;
sec++;
if(sec==60)
{ sec=0;
min++;
if(min==60)
{ min=0;
hour++;
if(hour==24)
{ hour=0;
}
}
}
}
show[0]=0x30+hour/10;
show[1]=0x30+hour%10;
show[2]='-';
show[3]=0x30+min/10;
show[4]=0x30+min%10;
show[5]='-';
show[6]=0x30+sec/10;
show[7]=0x30+sec%10;
}
unsigned char convert(unsigned char input) //由于板件的数据位跟P0口的数据位接反了,这是转换程序。
{ unsigned char i,output=0,temp;
for(i=0;i<=7;i++)
{ temp=(input>>i)&0x01;
output|=(temp<<(7-i));
}
return output;
}
void write_com(unsigned char com)
{ RS=0;
RW=0;
P0=convert(com);
E=1;
delay(1);
E=0;
delay(1);
}
void write_data(unsigned char indata)
{ RS=1;
RW=0;
P0=convert(indata);
E=1;
delay(1);
E=0;
delay(1);
}
void c1602_init()
{ write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void c1602_char(unsigned char x,unsigned char y,unsigned char charvaule)
{ if(y==1)
write_com(0x80+x);
else
write_com(0xc0+x);
write_data(charvaule);
}
void c1602_str(unsigned char space,unsigned char hang,unsigned char *str)
{ while(*str!='\0')
c1602_char(space++,hang,*str++);
}
void main()
{ c1602_init();
time0_init();
while(1)
{
c1602_str(3,1,show);
}
}
|