1602显示问号,求好心人解答,谢谢
#include<reg51.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
///////时钟芯片端口定义////////////////////////
sbit sck=P1^0;
sbit io=P1^1;
sbit rst=P1^2;
//////液晶控制端口定义//////////////////
sbit rs=P2^7;
sbit en=P2^6;
//////按键端口定义//////////////////////////////
sbit key1=P1^3;
sbit key2=P1^4;
sbit key3=P1^5;
sbit key4=P1^6;
/////////自动或者按照作息时间切换////////////////////////
sbit qh=P1^7;// 自动或者按照作息时间切换
/////自动控制时信号输入/////////////////////////////////////////////////
sbit gg=P3^5;//光敏电阻信号输入
sbit yy=P3^6;//下雨传感器信号输入
uchar table[]={"0123456789"};//液晶显示字符
uchar writetime[7];
uchar writeadd[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80}; //写入数据的地址
uchar readadd[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};//时钟芯片读出数据的地址
//////步进电机接口//////////////
uchar fan[4]={0xf8,0xf4,0xf2,0xf1};//反转
uchar zheng[4]={0xf1,0xf2,0xf4,0xf8};//正转
uchar nian,yue,ri,shi,fen,miao;//变量定义
uchar shi1,fen1,shi2,fen2;//时分1开启和2关闭比较变量
bit chuang_flag;//窗户开关标志位,0为窗户开,1为窗户关
void delay(uint ms) //延时函数
{
uchar y;
for(ms;ms>0;ms--)
for(y=110;y>0;y--);
}
void ds1302_write_byte(uchar date) //时钟芯片写字节
{
uchar i;
for(i=0;i<8;i++)
{
sck=0;
io=date&0x01;
date=date>>1;
sck=1;
}
}
void ds1302_write(uchar add,uchar date)//时钟芯片相应地址写入数据
{
rst=0;_nop_();
sck=0;_nop_();
rst=1;_nop_();
ds1302_write_byte(add);
ds1302_write_byte(date);
rst=0;_nop_();
sck=0;_nop_();
io=1;sck=1;
}
uchar ds1302_read(uchar add) //时钟芯片相应地址读出数据
{
uchar i,date;
rst=0;_nop_();
sck=0;_nop_();
rst=1;_nop_();
ds1302_write_byte(add);
for(i=0;i<8;i++)
{
date=date>>1;
sck=0;
if(io)
date=date|0x80;
sck=1;
}
rst=0;_nop_();
sck=0;_nop_();
io=1;sck=1;
return date;
}
void yj1602_write_com(uchar com)//液晶写入地址
{
en=0;delay(1);
en=1;delay(1);
rs=0;delay(1);
P0=com;
en=0;delay(1);
}
void yj1602_write_date(uchar date)//液晶写入数据
{
en=0;delay(1);
en=1;delay(1);
rs=1;delay(1);
P0=date;
en=0;delay(1);
}
void yj1602_init()//液晶初始化
{
yj1602_write_com(0x38);
yj1602_write_com(0x0c);
yj1602_write_com(0x06);
yj1602_write_com(0x01);
}
void write_1602(uchar add,uchar date)//液晶显示数字字符
{
uchar shi,ge;
shi=date/16;
ge=date%16;
yj1602_write_com(0x80+add);
yj1602_write_date(0x30+shi); //0x30码制转换
yj1602_write_date(0x30+ge);
}
void display() //显示函数 显示年月日时分秒
{
yj1602_write_com(0x80);
yj1602_write_date('D');
yj1602_write_date('a');
yj1602_write_date('t');
yj1602_write_date('a');
yj1602_write_date(':');
writetime[0]=ds1302_read(readadd[0]);
write_1602(5,writetime[0]);
yj1602_write_com(0x80+7);
yj1602_write_date('-');
writetime[2]=ds1302_read(readadd[2]);
write_1602(8,writetime[2]);
yj1602_write_com(0x80+10);
yj1602_write_date('-');
writetime[3]=ds1302_read(readadd[3]);
write_1602(11,writetime[3]);
yj1602_write_com(0x80+14);
if(chuang_flag==0)yj1602_write_date('K');
if(chuang_flag==1)yj1602_write_date('G');
yj1602_write_com(0x80+0x40);
yj1602_write_date('T');
yj1602_write_date('i');
yj1602_write_date('m');
yj1602_write_date('e');
yj1602_write_date(':');
writetime[4]=ds1302_read(readadd[4]);
write_1602(0x40+5,writetime[4]);
yj1602_write_com(0x80+0x40+7);
yj1602_write_date(':');
writetime[5]=ds1302_read(readadd[5]);
write_1602(0x40+8,writetime[5]);
yj1602_write_com(0x80+0x40+10);
yj1602_write_date(':');
writetime[6]=ds1302_read(readadd[6]);
write_1602(0x40+11,writetime[6]);
yj1602_write_com(0x80+0x40+11);
}
void display1() //显示调整开启和关闭时间函数
{
yj1602_write_com(0x80);
yj1602_write_date('K');
yj1602_write_date('a');
yj1602_write_date('i');
yj1602_write_date(':');
yj1602_write_com(0x87);
yj1602_write_date(table[shi1/10]);
yj1602_write_date(table[shi1%10]);
yj1602_write_date('-');
yj1602_write_date(table[fen1/10]);
yj1602_write_date(table[fen1%10]);
yj1602_write_com(0x80+0x40);
yj1602_write_date('G');
yj1602_write_date('a');
yj1602_write_date('n');
yj1602_write_date(':');
yj1602_write_com(0x87+0x40);
yj1602_write_date(table[shi2/10]);
yj1602_write_date(table[shi2%10]);
yj1602_write_date(':');
yj1602_write_date(table[fen2/10]);
yj1602_write_date(table[fen2%10]);
}
void keyscan() //按键扫描
{
uchar date_flag;
uchar set_flag;
uchar j,k;
if(key1==0)
{
yj1602_init();
delay(10);
if(key1==0)
{
display();
ds1302_write(0x8e,0x00);
date_flag=1;
yj1602_write_com(0x80+5);
yj1602_write_date('*');//依次调整年月日时分,秒不可调整
yj1602_write_date('*');
nian=ds1302_read(readadd[0]);
j=nian/16;
k=nian%16;
nian=j*10+k;
}
while(!key1);
}
////////////////////////调整年///////////////////////////////////////
while(date_flag==1)
{
display();
if(key1==0)
{
delay(10);
if(key1==0)
{
date_flag=2;
yj1602_write_com(0x80+8);
yj1602_write_date('*');//依次调整年月日时分,秒不可调整
yj1602_write_date('*');
yue=ds1302_read(readadd[2]);
j=yue/16;
k=yue%16;
yue=j*10+k;
}
while(!key1);
}
if(key2==0)
{
delay(10);
if(key2==0)
{
nian++;
yj1602_write_com(0x80+5);
yj1602_write_date('*');
yj1602_write_date('*');
if(nian==100)//年
nian=0;
j=nian/10;
k=nian%10;
writetime[0]=j*16+k;
ds1302_write(writeadd[0],writetime[0]);
}
while(!key2);
}
if(key3==0)
{
delay(10);
if(key3==0)
{
if(nian==0)
nian=100;
yj1602_write_com(0x80+5);
yj1602_write_date('*');
yj1602_write_date('*');
nian--;
j=nian/10;
k=nian%10;
writetime[0]=j*16+k;
ds1302_write(writeadd[0],writetime[0]);
}
while(!key3);
}
}
///////////// 调整月///////////////////////////////////////////////
while(date_flag==2)
{
display();
if(key1==0)
{
delay(10);
if(key1==0)
{
date_flag=3;
yj1602_write_com(0x80+11);
yj1602_write_date('*');//依次调整年月日时分,秒不可调整
yj1602_write_date('*');
ri=ds1302_read(readadd[3]);
j=ri/16;
k=ri%16;
ri=j*10+k;
}
while(!key1);
}
if(key2==0)
{
delay(10);
if(key2==0)
{
yj1602_write_com(0x80+8);
yj1602_write_date('*');
yj1602_write_date('*');
yue++;
if(yue>=13)
yue=1;
j=yue/10;
k=yue%10;
writetime[2]=j*16+k;
ds1302_write(writeadd[2],writetime[2]);
}
while(!key2);
}
if(key3==0)
{
delay(10);
if(key3==0)
{
if(yue<=1)
yue=13;
yj1602_write_com(0x80+8);
yj1602_write_date('*');
yj1602_write_date('*');
yue--;
j=yue/10;
k=yue%10;
writetime[2]=j*16+k;
ds1302_write(writeadd[2],writetime[2]);
}
while(!key3);
}
}
///////////// 调整日///////////////////////////////////////////////
while(date_flag==3)
{
display();
if(key1==0)
{
delay(10);
if(key1==0)
{
date_flag=4;
yj1602_write_com(0x80+0x40+5);
yj1602_write_date('*');//依次调整年月日时分,秒不可调整
yj1602_write_date('*');
shi=ds1302_read(readadd[4]);
j=shi/16;
k=shi%16;
shi=j*10+k;
}
while(!key1);
}
if(key2==0)
{
delay(10);
if(key2==0)
{
yj1602_write_com(0x80+11);
yj1602_write_date('*');
yj1602_write_date('*');
ri++;
if((yue==1)||(yue==3)||(yue==5)||(yue==7)||(yue==8)||(yue==10)||(yue==12))
{
if(ri==32)
ri=1;
}
if(yue==2)
{
if(nian%4==0)
{
if(ri==30)
ri=1;
}
else
{
if(ri==29)
ri=1;
}
}
if((yue==4)||(yue==6)||(yue==9)||(yue==11))
{
if(ri==31)
ri=1;
}
j=ri/10;
k=ri%10;
writetime[3]=j*16+k;
ds1302_write(writeadd[3],writetime[3]);
}
while(!key2);
}
if(key3==0)
{
delay(10);
if(key3==0)
{
if((yue==1)||(yue==3)||(yue==5)||(yue==7)||(yue==8)||(yue==10)||(yue==12))
{
if(ri==1)
ri=32;
}
if(yue==2)
{
if(nian%4==0)
{
if(ri==1)
ri=30;
}
else
{
if(ri==1)
ri=29;
}
}
if((yue==4)||(yue==6)||(yue==9)||(yue==11))
{
if(ri==1)
ri=31;
}
yj1602_write_com(0x80+11);
yj1602_write_date('*');
yj1602_write_date('*');
ri--;
j=ri/10;
k=ri%10;
writetime[3]=j*16+k;
ds1302_write(writeadd[3],writetime[3]);
}
while(!key3);
}
}
///////////// 调整时///////////////////////////////////////////////
while(date_flag==4)
{
display();
if(key1==0)
{
delay(10);
if(key1==0)
{
date_flag=5;
yj1602_write_com(0x80+0x40+8);
yj1602_write_date('*');//依次调整年月日时分,秒不可调整
yj1602_write_date('*');
fen=ds1302_read(readadd[5]);
j=fen/16;
k=fen%16;
fen=j*10+k;
}
while(!key1);
}
if(key2==0)
{
delay(10);
if(key2==0)
{
yj1602_write_com(0x80+0x40+5);
yj1602_write_date('*');
yj1602_write_date('*');
shi++;
if(shi==24)shi=0;
j=shi/10;
k=shi%10;
writetime[4]=j*16+k;
ds1302_write(writeadd[4],writetime[4]);
}
while(!key2);
}
if(key3==0)
{
delay(10);
if(key3==0)
{
if(shi==0)shi=24;
yj1602_write_com(0x80+0x40+5);
yj1602_write_date('*');
yj1602_write_date('*');
shi--;
j=shi/10;
k=shi%10;
writetime[4]=j*16+k;
ds1302_write(writeadd[4],writetime[4]);
}
while(!key3);
}
}
///////////// 调整分///////////////////////////////////////////////
while(date_flag==5)
{
display();
if(key1==0)
{
delay(10);
if(key1==0)
{
date_flag=0;
yj1602_write_com(0x80+5);
yj1602_write_date('*');
yj1602_write_date('*');
yj1602_write_com(0x80+8);
yj1602_write_date('*');
yj1602_write_date('*');
yj1602_write_com(0x80+11);
yj1602_write_date('*');
yj1602_write_date('*');
yj1602_write_com(0x80+0x40+5);
yj1602_write_date('*');
yj1602_write_date('*');
yj1602_write_com(0x80+0x40+8);
yj1602_write_date('*');
yj1602_write_date('*');
yj1602_write_com(0x80+0x40+11);
yj1602_write_date('*');
yj1602_write_date('*');
writetime[6]=0;
ds1302_write(writeadd[6],writetime[6]);
ds1302_write(0x8e,0x80);
}
while(!key1);
}
if(key2==0)
{
delay(10);
if(key2==0)
{
yj1602_write_com(0x80+0x40+8);
yj1602_write_date('*');
yj1602_write_date('*');
fen++;
if(fen==60)fen=0;
j=fen/10;
k=fen%10;
writetime[5]=j*16+k;
ds1302_write(writeadd[5],writetime[5]);
}
while(!key2);
}
if(key3==0)
{
delay(10);
if(key3==0)
{
if(fen==0)fen=60;
yj1602_write_com(0x80+0x40+8);
yj1602_write_date('*');
yj1602_write_date('*');
fen--;
j=fen/10;
k=fen%10;
writetime[5]=j*16+k;
ds1302_write(writeadd[5],writetime[5]);
}
while(!key3);
}
}
if(key4==0)
{
delay(10);
if(key4==0)
{
yj1602_init();
set_flag=1;
yj1602_write_com(0x87);
yj1602_write_date('*');
yj1602_write_date('*');
}
while(!key4);
}
while(set_flag==1)
{
display1();
if(key2==0)
{
delay(10);
if(key2==0)
{
yj1602_write_com(0x87);
yj1602_write_date('*');
yj1602_write_date('*');
shi1++;
if(shi1==24)shi1=0;
}
while(!key2);
}
if(key3==0)
{
delay(10);
if(key3==0)
{
yj1602_write_com(0x87);
yj1602_write_date('*');
yj1602_write_date('*');
if(shi1==0)shi1=24;
shi1--;
}
while(!key3);
}
if(key4==0)
{
delay(10);
if(key4==0)
{
yj1602_write_com(0x8a);
yj1602_write_date('*');
yj1602_write_date('*');
set_flag=2;
}
while(!key4);
}
}
while(set_flag==2)
{
display1();
if(key2==0)
{
delay(10);
if(key2==0)
{
yj1602_write_com(0x8a);
yj1602_write_date('*');
yj1602_write_date('*');
&nb