下面是我的程序,还有仿真图,不懂可以再问我,祝你成功
/*******************************************
ds1302计时+lcd1602显示
*******************************************/
#include<reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P2^0;
sbit rw=P2^1;
sbit e=P2^2;
sbit T_RST=P1^5;
sbit T_CLK=P1^6;
sbit T_IO=P1^7;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
/*******************************************
向1302写一个字节
*******************************************/
void input_BYTE(uchar dat)
{
uchar i;
ACC=dat;
for(i=8;i>0;i--)
{
T_IO=ACC0;
T_CLK=1;
T_CLK=0;
ACC=(ACC>>1);
}
}
/*******************************************
1302读出一个字节
*******************************************/
uchar output_BYTE()
{
uchar i;
for(i=8;i>0;i--)
{
ACC=(ACC>>1);
ACC7=T_IO;
T_CLK=1;
T_CLK=0;
}
return (ACC);
}
/*******************************************
写数据
*******************************************/
void write_1302(uchar add,uchar dat)
{
T_RST=0;
T_CLK=0;
T_RST=1;
input_BYTE(add);
input_BYTE(dat);
T_CLK=1;
T_RST=0;
}
/*******************************************
读数据
*******************************************/
uchar read_1302(uchar add)
{
uchar inf;
T_RST=0;
T_CLK=0;
T_RST=1;
input_BYTE(add);
inf=output_BYTE();
T_CLK=1;
T_RST=0;
return (inf);
}
void init_1302()
{
write_1302(0x8e,0x00);//关闭写保护;
/* write_1302(0x90,0xaa);//设置充电方式;
write_1302(0x80,0x00);//秒寄存器初始化;
write_1302(0x82,0x46);//分.......
write_1302(0x84,0x22);//时.......
write_1302(0x86,0x17);//日........
write_1302(0x88,0x03);//月.......
write_1302(0x8a,0x04);//星期...
write_1302(0x8c,0x11);//年...... */
write_1302(0x8e,0x80);//打开写保护;
}
uchar com,inf;
uchar code table[]="DS1302";
uchar code table1[]="0123456789";
uchar code table2[]="WEEK-";
void delay1(uchar t)
{
uchar x,y;
for(x=t;x>0;x--)
for(y=110;y>0;y--);
}
/*******************************************
给1602写指令
*******************************************/
void write_com(uchar com)
{
rs=0;
P0=com;
delay1(5);
e=1;
delay1(5);
e=0;
}
/*******************************************
给1602写数据
*******************************************/
void write_date(uchar date)
{
rs=1;
P0=date;
delay1(5);
e=1;
delay1(5);
e=0;
}
/*******************************************
1602初始化
*******************************************/
void unit()
{
e=0;
rw=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
/*******************************************
主函数
*******************************************/
void main()
{
uchar i;
uchar t_sec,sec1,sec2;
uchar t_min,min1,min2;
uchar t_hour,hour1,hour2;
uchar t_mon,mon1,mon2;
uchar t_day,day1,day2;
uchar t_year,year1,year2;
uchar week,week1;
uchar num;
unit();
init_1302();
while(1)
{
t_sec=read_1302(0x81);//du miao ;
sec1=t_sec&0x0f;
sec2=(t_sec>>4);
t_min=read_1302(0x83);//du fen ;
min1=t_min&0x0f;
min2=(t_min>>4);
t_hour=read_1302(0x85);//du shi ;
hour1=t_hour&0x0f;
hour2=(t_hour>>4);
t_day=read_1302(0x87);//du ri ;
day1=t_day&0x0f;
day2=(t_day>>4);
t_mon=read_1302(0x89);//du yue ;
mon1=t_mon&0x0f;
mon2=(t_mon>>4);
week=read_1302(0x8b);//du xing qi ;
week1=week&0x0f;
t_year=read_1302(0x8d);//du nian ;
year1=t_year&0x0f;
year2=(t_year>>4);
write_com(0x80);
for(i=0;i<6;i++)
{
write_date(table[i]);
}
write_com(0x80+0x40);
for(i=0;i<5;i++)
{
write_date(table2[i]);
}
write_com(0x80+0x07);
write_date(table1[year2]);
write_com(0x80+0x08);
write_date(table1[year1]);
write_com(0x80+0x09);
write_date('/');
write_com(0x80+0x0a);
write_date(table1[mon2]);
write_com(0x80+0x0b);
write_date(table1[mon1]);
write_com(0x80+0x0c);
write_date('/');
write_com(0x80+0x0d);
write_date(table1[day2]);
write_com(0x80+0x0e);
write_date(table1[day1]);
write_com(0x80+0x40+0x05);
write_date(table1[week1]);
write_com(0x80+0x40+0x07);
write_date(table1[hour2]);
write_com(0x80+0x40+0x08);
write_date(table1[hour1]);
write_com(0x80+0x40+0x09);
write_date(':');
write_com(0x80+0x40+0x0a);
write_date(table1[min2]);
write_com(0x80+0x40+0x0b);
write_date(table1[min1]);
write_com(0x80+0x40+0x0c);
write_date(':');
write_com(0x80+0x40+0x0d);
write_date(table1[sec2]);
write_com(0x80+0x40+0x0e);
write_date(table1[sec1]);
}
}