程序如下,请教程序哪里有问题,谢谢!
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit lcdrs=P2^6;
sbit lcdrw=P2^5;
sbit lcden=P2^7;
sbit ds1302_IO=P3^4;
sbit ds1302_CLK=P3^6;
sbit ds1302_RST=P3^5;
uchar num,i;
uchar code read_addr[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};
uchar code write_addr[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};
uchar time[7] = {0x57,0x59,0x23,0x06,0x08,0x04,0x20};
/****************************************************
LCD1206 程序
******************************************************/
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com) //写地址
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date) //读数据
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init() //LCD1206初始化
{
lcden=0;
lcdrw=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
/***********************************************
/********************
DS1302 写数据
*********************/
void ds1302_write(uchar addr,uchar date)
{
uchar n;
ds1302_CLK=0;
ds1302_RST=1;
for(n=0;n<8;n++)
{
ds1302_IO=addr&0x01;
addr>>=1;
ds1302_CLK=1;
ds1302_CLK=0;
}
for(n=0;n<8;n++)
{
ds1302_IO=date&0x01;
date>>=1;
ds1302_CLK=1;
ds1302_CLK=0;
}
ds1302_RST=0;
}
/**********************
读数据
*************************/
uchar ds1302_read(uchar addr)
{
uchar n,dat=0;
ds1302_RST=1;
for(n=0;n<8;n++)
{
ds1302_IO=addr&0x01;
addr>>=1;
ds1302_CLK=1;
ds1302_CLK=0;
}
for(n=0;n<8;n++)
{
dat=dat>>1;
if(ds1302_IO==1)
dat=dat|0x80;
ds1302_CLK=1;
ds1302_CLK=0;
}
ds1302_RST=0;
return dat;
}
void ds1302init() //ds1302初始化
{
uchar n;
ds1302_write(0x8e,0x00);
for(n=0;n<7;n++)
{
ds1302_write(write_addr[n],time[n]);
}
ds1302_write(0x8e,0x80);
}
void ds1302_readtime() //ds1302读数据
{
uchar n;
for(n=0;n<7;n++)
{
time[n]=ds1302_read(read_addr[n]);
}
}
void LcdDisplay()
{
write_com(0x80+0X40);
write_date('0'+time[2]/16); //时
write_date('0'+(time[2]&0x0f));
write_date('-');
write_date('0'+time[1]/16); //分
write_date('0'+(time[1]&0x0f));
write_date('-');
write_date('0'+time[0]/16); //秒
write_date('0'+(time[0]&0x0f));
write_com(0x80);
write_date('2');
write_date('0');
write_date('0'+time[6]/16); //年
write_date('0'+(time[6]&0x0f));
write_date('-');
write_date('0'+time[4]/16); //月
write_date('0'+(time[4]&0x0f));
write_date('-');
write_date('0'+time[3]/16); //日
write_date('0'+(time[3]&0x0f));
write_com(0x8D);
write_date('0'+(time[5]&0x07)); //星期
}
void main()
{
init();
ds1302init();
while(1)
{
ds1302_readtime();
LcdDisplay();
}
}
|