ds1302时钟-AVR程序代码
2017-11-23 来源:eefocus
#include
#include 'DS1302.h'
#include
void ds1302_init(void)
{
PORTX=0x00;
DDRX=0x07;
}
void write_ds1302_byte(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
SCK=0;
IO_out=dat&0x01;
dat=dat>>1;
SCK=1;
}
}
void write_ds1302(unsigned char add,unsigned char dat)
{
RST=1;
delay_us(1);
write_ds1302_byte(add);
write_ds1302_byte(dat);
RST=0;
delay_us(2);
IO_out=0;
SCK=0;
}
unsigned char read_ds1302(unsigned char add)
{
unsigned char i,value=0;
RST=1;
write_ds1302_byte(add);
IO_setting=0;
for(i=0;i<8;i++)
{
value=value>>1;
SCK=0;
SCK=0; //必须给两次!!
if(IO_in) value=value|0x80;
SCK=1;
}
IO_setting=1;
SCK=0;
RST=0;
delay_us(2);
IO_out=0;
return value;
}
void set_rtc(unsigned char* time_data)
{
unsigned char i,j,write_add;
for(i=0;i<7;i++)
{
j=time_data[i]/10;
time_data[i]=time_data[i];
time_data[i]=time_data[i]+j*16;
}
write_ds1302(0x8e,0x00);//去除写保护
for(i=0,write_add=0x8c;i<7;i++,write_add-=2)
write_ds1302(write_add,time_data[i]);
write_ds1302(0x8e,0x80);
}
void read_rtc(unsigned char* time_data)
{
unsigned char i,j,read_add;
write_ds1302(0x8e,0x00);
for(i=0,read_add=0x8d;i<7;i++,read_add-=2)
time_data[i]=read_ds1302(read_add);
for(i=0;i<7;i++)
{
j=time_data[i]/16;
time_data[i]%=16;
time_data[i]+=j*10;
}
write_ds1302(0x8e,0x80);
}
下一篇:AD数模转换-CVAVR程序代码