程序如下:
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar
time[7]={13,6,4,17,11,15,30};//初值年周月日时分秒
uchar write[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80}; //1302写的地址
uchar read[7]={0x8d,0x8b,0x89,0x87,0x85,0x81,0x81}; //1302读的地址
sbit io =P1^5;
sbit sck=P1^6;
sbit rst=P1^7;
sbit LED_RS=P2^5;
sbit LED_RW=P2^6;
sbit LED_EN=P2^7;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/*******************1602部分*****************************************/
void LED1602_com(uint com)
{
LED_RS=0;
LED_RW=0;
LED_EN=0;
P0=com;
delay(5);
LED_EN=1;
delay(5);
LED_EN=0;
}
void LED1602_date(uint date)
{
LED_RS=1;
LED_RW=0;
LED_EN=0;
P0=date;
delay(5);
LED_EN=1;
delay(5);
LED_EN=0;
}
void LED1602_start() //1602初始化
{
LED1602_com(0x38);
LED1602_com(0x0c);
LED1602_com(0x06);
LED1602_com(0x01);
}
/**************************1302部分******************************/
void write_ds1302s(uchar dat) //传数据(读写)
{
uchar i;
for(i=0;i<8;i++)
{
sck=0;
io=dat&0x01; //低字节开始传
dat=dat>>1;
sck=1;
}
}
void write_ds1302(uchar add,uchar dat)
{
rst=0;
delay(2);
sck=0;
rst=1;
write_ds1302s(add);
write_ds1302s(dat);
rst=0;
delay(2);
sck=1;
io=1;
}
uchar read_ds1302(uchar add)
{
uchar i,value;
rst=0;
delay(2);
sck=0;
rst=1;
write_ds1302s(add);
for(i=0;i<8;i++)
{
value=value>>1;
sck=0;
if(io)
value=value|0x80;
sck=1;
}
rst=0;
delay(2);
sck=0;
io=1;
sck=1;
return value;
}
void write_rtc()
{
uchar i,j;
for(i=0;i<7;i++) //转换BCD码
{
j=time[i]/10;
time[i]=time[i]%10;
time[i]=time[i]+j*16;
}
write_ds1302(0x8e,0x00); //去除写保护
for(i=0;i<7;i++)
{
write_ds1302(write[i],time[i]);
}
write_ds1302(0x8e,0x80); //加写保护
}
void read_rtc() //读出1302
{
uchar i;
for(i=0;i<7;i++)
{
time[i]=((read_ds1302(read[i])&0xf0)>>4))*10+(read[i]&0x0f);
}
}
void main()
{
uchar i;
LED1602_start();
write_rtc();
while(1)
{
read_rtc();
LED1602_com(0x80+0x00);
LED1602_date(time[4]/10+0x30);
LED1602_date(time[4]%10+0x30);
LED1602_date(':');
LED1602_date(time[5]/10+0x30);
LED1602_date(time[5]%10+0x30);
LED1602_date(':');
LED1602_date(time[6]/10+0x30);
LED1602_date(time[6]%10+0x30);
LED1602_date(' ');
LED1602_date(' ');
}
}
显示时分秒都显示I5,这什么问题阿?帮忙看下哪里错了。谢谢各位
一周热门 更多>