DS1302纠错。。。
时间:10-02
整理:3721RD
点击:
找了很久却不知道错哪,求大侠相助!
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit s_clka=P2^4;
sbit r_clka=P2^3;
sbit mosio=P2^2;
sbit sck=P3^6;
sbit io=P3^4;
sbit rst=P3^5;
uchar code dula[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
uchar code wela[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
uchar time_data[7]={10,6,4,17,58,8,38};
uchar write_add[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80};
uchar read_add[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};
uchar disp[8];
unsigned char flag;
unsigned int count;
void smg(uchar dat1,uchar dat2);
void send(uchar dat);
void out();
void write_ds1302_byte(uchar dat);
void write_ds1302(uchar add,uchar dat);
uchar read_ds1302(uchar add);
void set_rtc();
void read_rtc();
void time_pros();
void display();
void main()
{
set_rtc();
while(1)
{
read_rtc();
time_pros();
display();
}
}
void set_rtc()
{
uchar i,j;
for(i=0;i<7;i++)
{
j=time_data[i]/10;
time_data[i]=time_data[i]%10;
time_data[i]=time_data[i]+j*16;
}
write_ds1302(0x8e,0x00); //去处写保护
for(i=0;i<7;i++)
{
write_ds1302(write_add[i],time_data[i]);
}
write_ds1302(0x8e,0x80);
}
void read_rtc()
{
uchar i;
for(i=0;i<7;i++)
{
time_data[i]=read_ds1302(read_add[i]);
}
}
void time_pros()
{
disp[0]=time_data[6]%16;
disp[1]=time_data[6]/16;
disp[2]=10;
disp[3]=time_data[5]%16;
disp[4]=time_data[5]/16;
disp[5]=10;
disp[6]=time_data[4]%16;
disp[7]=time_data[4]/16;
}
void write_ds1302_byte(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;
_nop_();
sck=0;
_nop_();
rst=1;
_nop_();
write_ds1302_byte(add);
write_ds1302_byte(dat);
rst=0;
_nop_();
io=1;
sck=1;
}
uchar read_ds1302(uchar add)
{
uchar i,value;
rst=0;
_nop_();_nop_();
sck=0;
_nop_();_nop_();
rst=1;
_nop_();_nop_();
write_ds1302_byte(add);
for(i=0;i<8;i++)
{
if(io)
value=value|0x80;
sck=0;
value=value>>1;
_nop_();
_nop_();
_nop_();
sck=1;
}
rst=0;
_nop_();_nop_();
sck=0;
rst=0;
_nop_();
_nop_();
_nop_();
_nop_();
sck=1;
_nop_();
_nop_();
io=1;
_nop_();_nop_();
return value;
}
void display()
{
uchar i;
for(i=0;i<8;i++)
{
smg(wela[i],dula[disp[i]]);
}
}
void smg(uchar dat1,uchar dat2)
{
send(dat1);
send(dat2);
out();
}
void send(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
if((dat<<i)&0x80)
mosio=1;
else mosio=0;
s_clka=0;
s_clka=1;
}
}
void out()
{
r_clka=0;
r_clka=1;
}
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit s_clka=P2^4;
sbit r_clka=P2^3;
sbit mosio=P2^2;
sbit sck=P3^6;
sbit io=P3^4;
sbit rst=P3^5;
uchar code dula[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
uchar code wela[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
uchar time_data[7]={10,6,4,17,58,8,38};
uchar write_add[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80};
uchar read_add[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};
uchar disp[8];
unsigned char flag;
unsigned int count;
void smg(uchar dat1,uchar dat2);
void send(uchar dat);
void out();
void write_ds1302_byte(uchar dat);
void write_ds1302(uchar add,uchar dat);
uchar read_ds1302(uchar add);
void set_rtc();
void read_rtc();
void time_pros();
void display();
void main()
{
set_rtc();
while(1)
{
read_rtc();
time_pros();
display();
}
}
void set_rtc()
{
uchar i,j;
for(i=0;i<7;i++)
{
j=time_data[i]/10;
time_data[i]=time_data[i]%10;
time_data[i]=time_data[i]+j*16;
}
write_ds1302(0x8e,0x00); //去处写保护
for(i=0;i<7;i++)
{
write_ds1302(write_add[i],time_data[i]);
}
write_ds1302(0x8e,0x80);
}
void read_rtc()
{
uchar i;
for(i=0;i<7;i++)
{
time_data[i]=read_ds1302(read_add[i]);
}
}
void time_pros()
{
disp[0]=time_data[6]%16;
disp[1]=time_data[6]/16;
disp[2]=10;
disp[3]=time_data[5]%16;
disp[4]=time_data[5]/16;
disp[5]=10;
disp[6]=time_data[4]%16;
disp[7]=time_data[4]/16;
}
void write_ds1302_byte(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;
_nop_();
sck=0;
_nop_();
rst=1;
_nop_();
write_ds1302_byte(add);
write_ds1302_byte(dat);
rst=0;
_nop_();
io=1;
sck=1;
}
uchar read_ds1302(uchar add)
{
uchar i,value;
rst=0;
_nop_();_nop_();
sck=0;
_nop_();_nop_();
rst=1;
_nop_();_nop_();
write_ds1302_byte(add);
for(i=0;i<8;i++)
{
if(io)
value=value|0x80;
sck=0;
value=value>>1;
_nop_();
_nop_();
_nop_();
sck=1;
}
rst=0;
_nop_();_nop_();
sck=0;
rst=0;
_nop_();
_nop_();
_nop_();
_nop_();
sck=1;
_nop_();
_nop_();
io=1;
_nop_();_nop_();
return value;
}
void display()
{
uchar i;
for(i=0;i<8;i++)
{
smg(wela[i],dula[disp[i]]);
}
}
void smg(uchar dat1,uchar dat2)
{
send(dat1);
send(dat2);
out();
}
void send(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
if((dat<<i)&0x80)
mosio=1;
else mosio=0;
s_clka=0;
s_clka=1;
}
}
void out()
{
r_clka=0;
r_clka=1;
}
你自己对比一下http://bbs.elecfans.com/jishu_295866_1_1.html
我承认我是菜鸟,但也不用嘲笑我吧?
非常感谢!