#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P2^0;
sbit lcden=P2^2;
sbit rw=P2^1;
sbit dsas=P2^5;
sbit dsrw=P2^6;
sbit dsds=P2^7;
sbit dscs=P2^4;
char shi,fen,miao;
uchar code table[]=" 2010-06-14 ";
uchar code table1[]=" : : ";
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com) //1602A写命令
{
rw=0;
rs=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date) //1602A写数据
{
rw=0;
rs=1;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_sfm(uchar add,uchar date) //写十分秒,把两位数分离成两个一位数
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi); //0X30是ASC码转换
write_date(0x30+ge);//ASC码转换
}
void write_ds(uchar add,uchar date)
{
dsas=1;
dsds=1;
dsrw=1;
dscs=0;
P1=add;
_nop_();
_nop_();
dsas=0;
_nop_();
_nop_();
dsrw=0;
P1=date;
_nop_();
_nop_();
dsrw=1;
dsas=1;
dscs=1;
// delay(1);
}
uchar read_ds(uchar add)
{
uchar ds_date;
dsas=1;
dsds=1;
dsrw=1;
dscs=0;
P1=add;
_nop_();
_nop_();
dsas=0;
_nop_();
_nop_();
dsds=0;
_nop_();
_nop_();
P1=0xff;
ds_date=P1;
_nop_();
_nop_();
dsds=1;
_nop_();
dsas=1;
dscs=1;
return ds_date;
// delay(1);
}
void start()
{
write_ds(0x0a,0x20); //开启晶振,让时钟运行
}
void set_time()
{
write_ds(0x0b,0x86); //SET 置1允许写,不能读
write_ds(4,0x0a); //设置小时为10点
write_ds(2,0x08); //设置分为08分
write_ds(0x0b,0x06); //SET置0可以读,不能写
}
void init()
{
uchar num;
lcden=0;
delay(1);
write_ds(0x0a,0x20);
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay(5);
}
}
void busy_ds()
{
uchar temp;
temp=read_ds(0x0a);
// temp=temp&0x80;
while((temp&0x80)==0x80);
}
void gettime()
{
busy_ds();
miao=read_ds(0);
busy_ds();
fen=read_ds(2);
busy_ds();
shi=read_ds(4);
}
void main()
{
start();
set_time();
init();
while(1)
{
gettime();
write_sfm(10,miao);
write_sfm(7,fen);
write_sfm(4,shi);
}
}
连接方法as接P2^5;rw接P2^6;ds接P2^7;cs接P2^4;A0-A7接P1口,MOT接地,
reset接VCC。我怎么也读取不到时钟信息,请高手指点一下 |