#define rtc_ok1//操作成功
#define rtc_fail0//操作失敗
#define second_invalid40//秒值無效
#define minute_invalid41//分鐘值無效
#define hour_invalid42//小時值無效
#define week_invalid43//星期值無效
#define day_invalid44//日期值無效
#define month_invalid45//月份值無效
#define year_invalid46//年份值無效
#define century_invalid47//世紀值無效
//時鐘制式引數定義(暫沒開啟,全用24小時制)
#define mode_12_hour0x80//12小時制,即有am/pm
#define mode_24_hour0x00//24小時制
//充電引數定義
#define charge_enable0xa0//開啟充電,此時,若限流電阻選擇select_none,還是不能充電
#define charge_disable0x00//關閉充電
//充電二極體引數
#define select_one_diode0x04//選擇乙個二極體
#define select_two_diode0x08//選擇二個二極體
//充電限流電阻引數
#define select_none0x00//不用連,即斷開充電電路
#define select_r1_2k0x01//選擇電阻1,2k
#define select_r2_4k0x02//選擇電阻2,4k
#define select_r3_8k0x03//選擇電阻3,8k
#define ram_maxsize31//時鐘內部使用者可自定義的ram最大空間
typedef struct _datetime
unsigned char century;
unsigned char year;
unsigned char month;
unsigned char day;
unsigned char hour;
unsigned char min;
unsigned char sec;
unsigned char week;
}sttdatetime;
sbitrst=p1^5;
sbitscl=p1^6;
sbitsda=p1^7;
#define sec_addr0x80//1000 0000
#define min_addr0x82//1000 0010
#define hour_addr0x84//1000 0100
#define day_addr0x86//1000 0110
#define mom_addr0x88//1000 1000
#define week_addr0x8a//1000 1010
#define year_addr0x8c//1000 1100
#define century_addr0xc0//1100 0000用其rtc記憶體一位元組作為年世紀位元組
#define control_addr0x8e//1000 1110
#define trch_addr0x90//1001 0000
#define ram_base_addr0xc0
#define read1
#define write0
void ds1302_sendbyte(unsigned char data)
unsigned char i;
for(i=0;i<8;i++)
scl = 0;
sda = (bit)(data & 0x01);
scl = 1;
data >>= 1;
return;
unsigned char ds1302_readbyte(void)
unsigned char i,data;
sda = 1;
for(i=0;i<8;i++)
data >>= 1;
scl = 0;
if(sda==1) data |= 0x80;
scl = 1;
return(data);
voidds1302writebyte(unsigned char addr, unsigned char data)
scl = 0;
rst = 1;
ds1302_sendbyte(addr);
ds1302_sendbyte(data);
rst = 0;
return ;
unsigned char ds1302readbyte(unsigned char addr)
unsigned char data;
scl = 0;
rst = 1;
ds1302_sendbyte(addr | read);
data = ds1302_readbyte();
rst = 0;
return data;
//初始化時鐘
void initds1302(unsigned char chargemode)
unsigned char i;
scl = 0;
rst = 0;
ds1302writebyte(control_addr,0x00);//wp=0;
i = ds1302readbyte(sec_addr);
if((i & 0x80) == 0x80)
{//若時鐘丟失,即鋰電池沒電時,自動復位為2005-1-1 00:00:00 星期六
ds1302writebyte(century_addr,0x20);
ds1302writebyte(year_addr,0x06);
ds1302writebyte(mom_addr,0x01);
ds1302writebyte(day_addr,0x01);
ds1302writebyte(hour_addr,0x00);
ds1302writebyte(min_addr,0x00);
ds1302writebyte(sec_addr,0x00);
ds1302writebyte(week_addr,0x00);
ds1302writebyte(trch_addr,chargemode);//設定充電模式
ds1302writebyte(control_addr,0x80);//wp=1;
//讀時鐘
void readclock(sttdatetime *pstdatetime)
//pstdatetime->century = ds1302readbyte(century_addr);
pstdatetime->year= ds1302readbyte(year_addr);
if(pstdatetime->year < 0x80)
pstdatetime->century = 0x20;
else
pstdatetime->century = 0x19;
pstdatetime->month= ds1302readbyte(mom_addr);
pstdatetime->day= ds1302readbyte(day_addr);
pstdatetime->hour= ds1302readbyte(hour_addr);
pstdatetime->min= ds1302readbyte(min_addr);
pstdatetime->sec= ds1302readbyte(sec_addr);
pstdatetime->week= ds1302readbyte(week_addr);
//寫時鐘
void writeclock(sttdatetime *pstdatetime)
ds1302writebyte(control_addr,0x00);//wp=0;
ds1302writebyte(century_addr,pstdatetime->century);
ds1302writebyte(year_addr,pstdatetime->year);
ds1302writebyte(mom_addr,pstdatetime->month);
ds1302writebyte(day_addr,pstdatetime->day);
ds1302writebyte(hour_addr,pstdatetime->hour);
ds1302writebyte(min_addr,pstdatetime->min);
ds1302writebyte(sec_addr,pstdatetime->sec);
ds1302writebyte(week_addr,pstdatetime->week);
ds1302writebyte(control_addr,0x80);//wp=1;
C語言程式》DS1302
include include include ds1302.h ds1302 io口定義 define clk pc0 define data pc1 define rst pc6 define ds1302 data ddr out ddrc 1 define ds1302 data set p...
藍橋杯ds1302程式小問題
寫乙個修改ds1302時間的程式時,對定義的陣列進行加減時發現,修改的資料無法寫入ds1302.程式片段 shijian dsinit 錯誤原因 1.對所存的時間資料的陣列定義為code。code 指定資料是儲存在 區,資料是在程式設計的時候跟 一起寫入 儲存器,執行過程中不能改變 2.初始化時間所...
DS1302實時時鐘晶元
ds1302 是 dallas 達拉斯 公司推出的一款涓流充電時鐘晶元,2001 年 dallas 被 maxim 美信 收購,因此我們看到的 ds1302 的資料手冊既有 dallas 的標誌,又有 maxim 的標誌,大家了解即可。ds1302 實時時鐘晶元廣泛應用於 傳真 可攜式儀器等產品領域...