请教我的12864+ds1302程序在12864上显示000000是怎么回事,程序如下:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit SCK=P2^7;
sbit IO=P2^6;
sbit RST=P2^5;
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
sbit PSB=P2^3;
uint shi,fen,miao;
//uchar TimeTab[]="11:14:00";
uint code SMTab[] = {'0','1','2','3','4','5','6','7','8','9'};
uint time_date[3]={9,27,10};//小时,分钟,秒
uint write_add[3]={0x84,0x82,0x80};
uint Read_add[3]={0x85,0x83,0x81};
void delay()
{;;}
void delay1(uchar x)
{
uchar y,z;
for(y=x;y>0;y--)
for(z=110;z>0;z--);
}
void Write_comm(uchar comm)
{
RS=0;
RW=0;
delay1(1);
P0=comm;
E=1;
delay1(1);
E=0;
}
void Write_dat(uchar dat)
{
RS=1;
RW=0;
delay1(5);
P0=dat;
E=1;
delay1(5);
E=0;
}
void Init_12864()
{
E=0;
delay1(1);
PSB=1;
delay1(40);
Write_comm(0x30); //基本指令动作-功能设定
delay1(100);
Write_comm(0x30);
delay1(37);
Write_comm(0x08);//显示状态
delay1(100);
Write_comm(0x10); //设定游标的移位与显示的移位-游标或显示移位控制
delay1(100);
Write_comm(0x0c); //显示状态-整体显示
delay1(100);
Write_comm(0x01); //清屏显示
delay1(10);
Write_comm(0x06); //进入点设置-指定在资料的读取与写入时设定游标移动方向及指定显示的移位光标
delay1(100);
Write_comm(0x80);
}
void Write_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
SCK=0;
delay();
IO=dat&0x01;
dat=dat>>1;
delay();
SCK=1;
delay();
}
}
void Write_ds1302(uchar add,uchar dat)
{
RST=0;
delay();
SCK=0;
delay();
RST=1;
delay();
Write_byte(add);
Write_byte(dat);
RST=0;
delay();
SCK=1;
IO=1;
}
uchar Read_ds1302(uchar add)
{
uchar i,value;
RST=0;
delay();
SCK=0;
delay();
RST=1;
delay();
Write_byte(add);
delay();
for(i=0;i<8;i++)
{
SCK=0;
delay();
value=value>>1;
if(IO)
value=value|0x80;
delay();
SCK=1;
delay();
}
return value;
RST=0;
delay();
SCK=1;
IO=1;
}
void init_ds1302()
{
uchar flag,time,i;
flag=Read_ds1302(0x81);
if(flag&0x80)
{
for(i=0;i<3;i++)
{
time=time_date[i]/10;
time_date[i]=time_date[i]%10;
time_date[i]=time_date[i]+time*16;
}
Write_ds1302(0x8e,0x00);
for(i=0;i<3;i++)
{
Write_ds1302(write_add[i],time_date[i]);
}
Write_ds1302(0x8e,0x80);
}
}
void time_pros(uchar X,uchar Y)
{
uchar pos;
if(X==0)
X=0x80;
else if(X==1)
X=0x90;
else if(X==2)
X=0x88;
else if(X==3)
X=0x98;
pos=X+Y;
Write_comm(pos);
}
void main()
{
uint shi,fen,miao;
Init_12864();
init_ds1302();
while(1)
//uchar Read_add[3]={0x85,0x83,0x81};
{
shi=Read_ds1302(0x85);
time_pros(0,0);
Write_dat(SMTab[shi/16]);
Write_dat(SMTab[shi%16]);
fen=Read_ds1302(0x83);
time_pros(0,1);
Write_dat(SMTab[fen/16]);
Write_dat(SMTab[fen%16]);
miao=Read_ds1302(0x81);
time_pros(0,2);
Write_dat(SMTab[miao/16]);
Write_dat(SMTab[miao%16]);
}
} |