#include"DEFF.h"
#include"ds1302.h"
#include"LCD_1602.h"
uchar time[7]=
{
13,5,4,26,21,11,20
};//年,周,月,日,时,分,秒
uchar write_add[7]=
{
0x8c,0x8a,0x88,0x86,0x84,0x82,0x80
};//写寄存器地址 r/w=0
uchar read_add[7]=
{
0x8d,0x8b,0x89,0x87,0x85,0x83,0x81
};//读寄存器地址 r/w=1
uchar dis[]=
{0,0,0,0,0,0,0,0};
void write_ds1302_byte(uchar a)//写一字节
{ uchar i;
for(i=0;i<8;i++)
{
sck0;//写操作为上升沿发送,给‘0’使稳定
if((a&0x01)==1)
io1;
else
io0;
delay_us(20);
//先准备好数据
sck1;//拉高发送
delay_us(20);
a=a>>1;//右移
}
}
void write_ds1302(uchar add,uchar dat)//写(双字节)//地址、数据
{
/* rst0;
sck0;
rst_out;
sck_out;
io_out;*/
io_out;
rst0;
delay_us(20);
rst1;
delay_us(20);
write_ds1302_byte(add);
write_ds1302_byte(dat);
rst0;//停止
delay_us(20);
io_in;
/* io1;//释放
sck1;//释放*/
}
uchar read_ds1302(uchar add)
{ uchar a;uchar value;
/*rst_out;
sck_out;
io_in;*/
io_out;
rst0;
delay_us(20);
sck0;
delay_us(20);
rst1;
delay_us(20);
write_ds1302_byte(add);
for(a=0;a<8;a++)
{ value=value>>1;//只需移位七次
sck0;
if(PINA&BIT(4))
value=value|0x80;
sck1;
delay_us(20);
}
rst0;
delay_us(20);
sck0;
delay_us(20);
sck1;
delay_us(20);
io1;
delay_us(20);
return value;
}
void set_rtc()
{ uchar i,j;
for(i=0;i<7;i++)
{
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_add[i],time[i]);//写入时间,对时
}
write_ds1302(0x8e,0x80);//写保护
}
void time_chuli()//BCD码转换,并成为ASCLL码
{
/*dis[0]=time[4]/16+0x30;
dis[1]=time[4]%16+0x30;
dis[2]='-';
dis[3]=time[5]/16+0x30;
dis[4]=time[5]%16+0x30;
dis[5]='-';
dis[6]=time[6]/16+0x30;
dis[7]=time[6]%16+0x30;
*/
dis[0]=time[4]/16+0x30;
dis[1]=time[4]%16+0x30;
dis[2]='-';
dis[3]=time[5]/16+0x30;
dis[4]=time[5]%16+0x30;
dis[5]='-';
dis[6]=time[6]/16+0x30;
dis[7]=time[6]%16+0x30;
}
void read_rtc()
{
uchar i;
for(i=0;i<7;i++)
{
time[i]=read_ds1302(read_add[i]);
}
}
|