DS1302的AVR程序(2000年~2099年的星期自動(dòng)計(jì)算功能)
雖然是在MEGA128上調(diào)的但適用于所有AVR芯片,改下管腳設(shè)置就行了。
程序不是我原創(chuàng),我只是把我找到的DS1302程序和星期計(jì)算程序做了下修改使他們能適應(yīng)我的系統(tǒng)能一起工作而已,在此向?qū)戇@些程序的原作者表示敬意。
structtimechar
{
ucharnowyear;
ucharnowmonth;
ucharnowday;
ucharnowhour;
ucharnowminute;
ucharnowsecond;
ucharnowweek;
};
structtimecharnowtime;///這里存時(shí)間
#defineds1302_rst7/*!PORTB7DS1302實(shí)時(shí)時(shí)鐘復(fù)位線引腳*/
#defineds1302_io6/*!PORTB6DS1302實(shí)時(shí)時(shí)鐘數(shù)據(jù)線引腳*/
#defineds1302_sclk5/*!PORTB5DS1302實(shí)時(shí)時(shí)鐘時(shí)鐘線引腳*/
#defineset_ds1302_rst_ddr()DDRB|=1<
#defineset_ds1302_io_ddr()DDRB|=1<
#defineset_ds1302_sclk_ddr()DDRB|=1<
#defineds1302_sec_reg0x80
#defineds1302_min_reg0x82
#defineds1302_hr_reg0x84
#defineds1302_date_reg0x86
#defineds1302_month_reg0x88
#defineds1302_day_reg0x8a
#defineds1302_year_reg0x8c
#defineds1302_control_reg0x8e
#defineds1302_charger_reg0x90
#defineds1302_clkburst_reg0xbe
ucharBcdToBin(ucharval)
{
val=(val>>4)*10+(val&0x0f);///將BCD碼轉(zhuǎn)換為10進(jìn)制數(shù)
returnval;///返回10進(jìn)制數(shù)
}
ucharBinToBcd(ucharval)
{
return((val/10)*16+val%10);
}
voidds1302_write(ucharreg,uchardata)
{
uchari=0;
set_ds1302_io_ddr();
nop();
nop();
clr_ds1302_rst();
nop();
nop();
clr_ds1302_sclk();
nop();
nop();
set_ds1302_rst();
nop();
nop();
for(i=8;i>0;i--)
{
if(reg&0x01)
{
set_ds1302_io();
}
else
{
clr_ds1302_io();
}
nop();
nop();
set_ds1302_sclk();
nop();
nop();
clr_ds1302_sclk();
nop();
nop();
reg>>=1;
}
for(i=8;i>0;i--)
{
if(data&0x01)
{
set_ds1302_io();
}
else
{
clr_ds1302_io();
}
nop();
nop();
set_ds1302_sclk();
nop();
nop();
clr_ds1302_sclk();
nop();
nop();
data>>=1;
}
clr_ds1302_rst();
nop();
nop();
clr_ds1302_io_ddr();
nop();
nop();
}
uchards1302_read(ucharreg)
{
uchardata=0;
uchari=0;
reg+=1;///讀標(biāo)志
set_ds1302_io_ddr();
nop();
nop();
clr_ds1302_rst();
nop();
nop();
clr_ds1302_sclk();
nop();
nop();
set_ds1302_rst();
nop();
nop();
for(i=8;i>0;i--)
{
if(reg&0x01)
{
set_ds1302_io();
}
else
{
clr_ds1302_io();
}
nop();
nop();
set_ds1302_sclk();
nop();
nop();
clr_ds1302_sclk();
nop();
nop();
reg>>=1;
}
clr_ds1302_io_ddr();
nop();
nop();
for(i=8;i>0;i--)
{
data>>=1;
if(in_ds1302_io())
{
data|=0x80;
}
nop();
nop();
set_ds1302_sclk();
nop();
nop();
clr_ds1302_sclk();
nop();
nop();
}
clr_ds1302_rst();
nop();
nop();
return(data);
}
ucharcheck_ds1302(void)
{
ds1302_write(ds1302_control_reg,0x80);
if(ds1302_read(ds1302_control_reg)==0x80)
{
return1;
}
return0;
}
voidds1302_read_time(void)
{
uchartmphour;
nowtime.nowyear=BcdToBin(ds1302_read(ds1302_year_reg));///年
nowtime.nowmonth=BcdToBin(ds1302_read(ds1302_month_reg));///月
nowtime.nowday=BcdToBin(ds1302_read(ds1302_date_reg));///日
nowtime.nowweek=BcdToBin(ds1302_read(ds1302_day_reg));///周
tmphour=ds1302_read(ds1302_hr_reg);///時(shí)
nowtime.nowhour=(tmphour&0b00100000)/32*20+(tmphour&0x10)/16*10+(tmphour&0x0F);
nowtime.nowminute=BcdToBin(ds1302_read(ds1302_min_reg));///分
nowtime.nowsecond=BcdToBin(ds1302_read(ds1302_sec_reg));///秒
}
constunsignedcharWeekTab[]={
//閏年月星期表
(3<<5)+31,///1月
(6<<5)+29,///2月
(0<<5)+31,///3月
(3<<5)+30,///4月
(5<<5)+31,//5月
(1<<5)+30,//6月
(3<<5)+31,//7月
(6<<5)+31,//8月
(1<<5)+30,//9月
(4<<5)+31,//10月
(0<<5)+30,//11月
(2<<5)+31//12月
};
/*!------------------------------------------------------------------------------
2000年~2099年星期算法
-------------------------------------------------------------------------------*/
unsignedcharWeekDay20(unsignedchary,unsignedcharm,unsignedchard)
{
unsignedcharweek,day;
day=WeekTab[m-1];///月表
week=day>>5;///月星期數(shù)
day&=0x1f;//月天數(shù)
if((m<3)&&(y&0x03))
{///平年
if(m==2)day--;///平年月天數(shù)
week++;///平年月表+1
}
y=y+(y>>2);//年+年/4
week=(week+y+d+2)%7;///(星期=年+年/4+月表+2日)%7
returnweek;///返回星期
}
voidds1302_write_time(void)
{
ucharbcdyear;
ucharbcdmonth;
ucharbcdday;
ucharbcdhour;
ucharbcdminute;
ucharbcdsecond;
ucharbcdweek;
bcdyear=BinToBcd(nowtime.nowyear);
bcdmonth=BinToBcd(nowtime.nowmonth);
bcdday=BinToBcd(nowtime.nowday);
bcdhour=BinToBcd(nowtime.nowhour);
bcdminute=BinToBcd(nowtime.nowminute);
bcdsecond=BinToBcd(nowtime.nowsecond);
nowtime.nowweek=WeekDay20(nowtime.nowyear,nowtime.nowmonth,nowtime.nowday);
bcdweek=BinToBcd(nowtime.nowweek);
ds1302_write(ds1302_control_reg,0x00);///關(guān)閉寫保護(hù)
ds1302_write(ds1302_sec_reg,0x80);///暫停
ds1302_write(ds1302_day_reg,bcdweek);///周
ds1302_write(ds1302_charger_reg,0xa9);///涓流充電
ds1302_write(ds1302_year_reg,bcdyear);///年
ds1302_write(ds1302_month_reg,bcdmonth);///月
ds1302_write(ds1302_date_reg,bcdday);///日
ds1302_write(ds1302_hr_reg,bcdhour);///時(shí)
ds1302_write(ds1302_min_reg,bcdminute);///分
ds1302_write(ds1302_sec_reg,bcdsecond);///秒
ds1302_write(ds1302_control_reg,0x80);///打開(kāi)寫保護(hù)
}