#include <reg52.h>
#include <intrins.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
sbit SCK=P3^5;//时钟
sbit SDA=P3^4;//数据
sbit RST=P1^7;//复位
sbit RS=P1^0;
sbit RW=P1^1;
sbit EN=P1^5;
sbit busy=P0^7;
uint16 num;
#define DS1302_W_ADDR 0xBE
#define DS1302_R_ADDR 0xBF
uint8
time[]={50,59,23,15,8,7,10};
void DS1302WriteByte(uint8 dat)
{
uint8 i;
SCK=0;
for(i=0;i<8;i++)
{
SDA=dat & 0x01;
SCK=1;
dat>>=1;
SCK=0;
}
}
uint8 DS1302ReadByte()
{
uint8 i,dat;
for(i=0;i<8;i++)
{
dat>>=1;
if(SDA==1)
{
dat |= 0x80;
}
SCK=1;
SCK=0;
}
return dat;
}
void reset_ds1302(void)
{
RST = 0;
SCK = 0;
RST = 1;
}
void clear_ds1302_WP(void)
{
reset_ds1302();
RST = 1;
DS1302WriteByte(0x8E);
DS1302WriteByte(0);
SDA = 0;
RST = 0;
}
/**
* 设置写保护
*/
void set_ds1302_WP(void)
{
reset_ds1302();
RST = 1;
DS1302WriteByte(0x8E);
DS1302WriteByte(0x80);
SDA = 0;
RST = 0;
}
void set_time(uint8 *timedata)
{
uint8 i,tmp;
for(i=0;i<7;i++)
{
tmp=timedata[i]/10;
timedata[i]=timedata[i]%10;
timedata[i]=timedata[i]+tmp*16;
}
clear_ds1302_WP();
reset_ds1302();
RST=1;
DS1302WriteByte(DS1302_W_ADDR);
for(i=0;i<7;i++)
{
DS1302WriteByte(timedata[i]);
}
DS1302WriteByte(0);
SDA=0;
RST=0;
set_ds1302_WP();
}
void read_time(uint8 *timedata)
{
uint8 i;
clear_ds1302_WP();
reset_ds1302();
DS1302WriteByte(DS1302_R_ADDR);
for(i=0;i<7;i++)
{
timedata[i]=DS1302ReadByte();
}
SDA=0;
RST=0;
set_ds1302_WP();
}
void wait()
{
do
{
P0=0xFF;
EN=0;
RS=0;
RW=1;
EN=1;
}while(busy==1);
EN=0;
}
void w_dat(uint8 dat)
{
wait();
RS=1;
RW=0;
P0=dat;
EN=1;
EN=0;
}
void w_cmd(uint8 com)
{
wait();
RS=0;
RW=0;
P0=com;
EN=1;
EN=0;
}
void init()
{
EN=0;
w_cmd(0x38);
w_cmd(0x0C);
w_cmd(0x06);
w_cmd(0x01);
}
void main()
{
init();
set_time(&time);
while(1)
{
read_time(&time);
w_cmd(0x80);
w_dat(time[6]);
w_dat('-');
w_dat(time[4]);
w_dat('-');
w_dat(time[3]);
w_dat('-');
w_dat(time[2]);
w_dat(':');
w_dat(time[1]);
w_dat(':');
w_dat(time[0]);
}
}
请问这个程序有什么问题吗,为什么在液晶上显示乱码了
一周热门 更多>