AVR DS1302源程序
//相關(guān)定義:
本文引用地址:http://www.ex-cimer.com/article/201611/315731.htm#define uint unsigned int
#define uchar unsigned char
#define DS1302_RST 0 //pc0
#define DS1302_SDA 1 //pc1
#define DS1302_SCLK 6 //pc6
//ds1302相關(guān)
//DS1302_RST=1
#define Set_DS1302_RST() PORTC|=1< //DS1302_RST=0 #define Clr_DS1302_RST() PORTC&=~(1< //DS1302_SDA=1 #define Set_DS1302_SDA() PORTC|=1< //DS1302_SDA=0 #define Clr_DS1302_SDA() PORTC&=~(1< //DS1302_SCLK=1 #define Set_DS1302_SCLK() PORTC|=1< //DS1302_SCLK=0 #define Clr_DS1302_SCLK() PORTC&=~(1< //DS1302deSDA置為輸出腳 #define Set_DS1302_DDRSDA() DDRC|=1< //DS1302deSDA置為輸入腳 #define Clr_DS1302_DDRSDA() DDRC&=~(1< #define DS1302_SEC_Reg 0x80 #define DS1302_MIN_Reg 0x82 #define DS1302_HR_Reg 0x84 #define DS1302_DATE_Reg 0x86 #define DS1302_MONTH_Reg 0x88 #define DS1302_DAY_Reg 0x8a #define DS1302_YEAR_Reg 0x8c #define DS1302_CONTROL_Reg 0x8e #define DS1302_CHARGER_Reg 0x90 #define DS1302_CLKBURST_Reg 0xbe //全局變量 //為了方便,我把個位和十位分開了 uchar year1=0x88; uchar year0=0x88; uchar month1=0x88; uchar month0=0x88; uchar date0=0x88; uchar date1=0x88; uchar day=0x88; uchar hour1=0x88; uchar hour0=0x88; uchar minute1=0x88; uchar minute0=0x88; uchar second1=0x88; uchar second0=0x88; //自程序開始 void DS1302_Write(uchar reg,uchar da { uchar i; Set_DS1302_DDRSDA(); 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_SDA(); else Clr_DS1302_SDA(); 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_SDA(); 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_DDRSDA(); } uchar DS1302_Read(uchar reg) { uchar da reg+=1;//讀標(biāo)志 Set_DS1302_DDRSDA(); 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_SDA(); else Clr_DS1302_SDA(); asm("nop"); asm("nop"); Set_DS1302_SCLK(); asm("nop"); asm("nop"); Clr_DS1302_SCLK(); asm("nop"); asm("nop"); reg>>=1; } Clr_DS1302_DDRSDA(); for(i=8;i>0;i--) { da if((PINC&(1< 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; } void DS1302_Init(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,0x04); //年 DS1302_Write(DS1302_MONTH_Reg,0x12); //月 DS1302_Write(DS1302_DATE_Reg,0x09); //日 DS1302_Write(DS1302_DAY_Reg,0x04); //周 DS1302_Write(DS1302_HR_Reg,0x10); //時 DS1302_Write(DS1302_MIN_Reg,0x25); //分 DS1302_Write(DS1302_SEC_Reg,0x00); //秒 DS1302_Write(DS1302_CONTROL_Reg,0x80);//打開寫保護(hù) } void ReadTime(void) { uchar da da year0=da year1=da da month0=da month1=(da da date0=da date1=(da da day=da da hour0=da hour1=(da da minute0=da minute1=(da da second0=da second1=(da }
評論