ds1302的avr C程序
#defineds1302_rst PC0
#defineds1302_io PC1
#defineds1302_sclk PC2
#define set_ds1302_rst_ddr() DDRC|=1<
#defineds1302_sec_reg 0x80
#defineds1302_min_reg 0x82
#defineds1302_hr_reg 0x84
#defineds1302_date_reg 0x86
#defineds1302_month_reg 0x88
#defineds1302_day_reg 0x8a
#defineds1302_year_reg 0x8c
#defineds1302_control_reg 0x8e
#defineds1302_charger_reg 0x90
#defineds1302_clkburst_reg 0xbe
voidds1302_write(uchar reg,uchar da
{
uchar i=0;
set_ds1302_io_ddr();
asm("nop");
asm("nop");
clr_ds1302_rst();
asm("nop");
asm("nop");
clr_ds1302_sclk();
asm("nop");
asm("nop");
set_ds1302_rst();
asm("nop");
asm("nop");
for(i=8;i>0;i--)
{
if(reg&0x01) set_ds1302_io();
else clr_ds1302_io();
asm("nop");
asm("nop");
set_ds1302_sclk();
asm("nop");
asm("nop");
clr_ds1302_sclk();
asm("nop");
asm("nop");
reg>>=1;
}
for(i=8;i>0;i--)
{
if(da
else clr_ds1302_io();
asm("nop");
asm("nop");
set_ds1302_sclk();
asm("nop");
asm("nop");
clr_ds1302_sclk();
asm("nop");
asm("nop");
da
}
clr_ds1302_rst();
asm("nop");
asm("nop");
clr_ds1302_io_ddr();
asm("nop");
asm("nop");
}
uchards1302_read(uchar reg)
{
uchar da
uchar i=0;
reg+=1;//讀標(biāo)志
set_ds1302_io_ddr();
asm("nop");
asm("nop");
clr_ds1302_rst();
asm("nop");
asm("nop");
clr_ds1302_sclk();
asm("nop");
asm("nop");
set_ds1302_rst();
asm("nop");
asm("nop");
for(i=8;i>0;i--)
{
if(reg&0x01) set_ds1302_io();
else clr_ds1302_io();
asm("nop");
asm("nop");
set_ds1302_sclk();
asm("nop");
asm("nop");
clr_ds1302_sclk();
asm("nop");
asm("nop");
reg>>=1;
}
clr_ds1302_io_ddr();
asm("nop");
asm("nop");
for(i=8;i>0;i--)
{
da
if(in_ds1302_io()) da
asm("nop");
asm("nop");
set_ds1302_sclk();
asm("nop");
asm("nop");
clr_ds1302_sclk();
asm("nop");
asm("nop");
}
clr_ds1302_rst();
asm("nop");
asm("nop");
return(da
}
uchar check_ds1302(void)
{
ds1302_write(ds1302_control_reg,0x80);
if(ds1302_read(ds1302_control_reg)==0x80) return 1;
return 0;
}
voidds1302_read_time(void)
{
year=ds1302_read(ds1302_year_reg); //年
month=ds1302_read(ds1302_month_reg);//月
day=ds1302_read(ds1302_date_reg); //日
week=ds1302_read(ds1302_day_reg); //周
hour=ds1302_read(ds1302_hr_reg); //時(shí)
minute=ds1302_read(ds1302_min_reg); //分
second=ds1302_read(ds1302_sec_reg); //秒
}
voidds1302_write_time(void)
{
ds1302_write(ds1302_control_reg,0x00); //關(guān)閉寫保護(hù)
ds1302_write(ds1302_sec_reg,0x80); //暫停
ds1302_write(ds1302_charger_reg,0xa9); //涓流充電
ds1302_write(ds1302_year_reg,year); //年
ds1302_write(ds1302_month_reg,month); //月
ds1302_write(ds1302_date_reg,day); //日
ds1302_write(ds1302_day_reg,week); //周
ds1302_write(ds1302_hr_reg,hour); //時(shí)
ds1302_write(ds1302_min_reg,minute); //分
ds1302_write(ds1302_sec_reg,second); //秒
ds1302_write(ds1302_control_reg,0x80); //打開寫保護(hù)
}
評(píng)論