




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、. /* File name - ds1302.c* Author - By Sam Chan* Version - V1.0* Date - 08 - 23 - 2012* Brief - 時(shí)鐘芯片DS1302驅(qū)動(dòng)* Copyright (C) 20* All rights reserved* File Update* Author - By Sam Chan* Date - 10 - 20 - 2013 * Revise - A、增加對(duì)STM32控制的移植支持* - B、增加對(duì)置的RAM操作相關(guān)函數(shù)* - C、增加檢測(cè)DS1302是否存在,是否第一次上電檢測(cè)函數(shù)* - D、增加對(duì)DS130
2、2置的鋰電池充電存放器參數(shù)設(shè)置函數(shù)和構(gòu)造體*/#include ds1302.h/*定義變量*/#define Date TimeValue.date#define Min TimeValue.minute#define Sec TimeValue.second#define Hour TimeValue.hour#define Week TimeValue.week#define Month TimeValue.month#define Year TimeValue.yearTime_Typedef TimeValue; /定義時(shí)間數(shù)據(jù)指針void Time_Init()TimeValue.
3、date=24;TimeValue.hour=6;TimeValue.minute=12;TimeValue.month=5;TimeValue.second=0;TimeValue.week=2;TimeValue.year=16;Charge_Typedef ChargeValue; /定義充電存放器/* Function Name - GPIO初始化* Description - none* Input - none* Output - none* Reaturn - none */void DS1302_GPIOInit(void)GPIO_InitTypeDef GPIO_InitS
4、tructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); /開啟GPIOC外設(shè)時(shí)鐘/* 初始化GPIOC */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /推挽輸出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;GPIO_Init(GPIOC, &GPIO_InitStructure);/* F
5、unction Name - DS1302寫入一個(gè)字節(jié)數(shù)據(jù)* Description - none* Input - dat:要寫入的數(shù)據(jù)* Output - none* Reaturn - none */void DS1302_Write_Byte(u8 dat)u8 i;DS1302_SDA_OUT(); /設(shè)置為輸出口for(i=0;i= 1; /數(shù)據(jù)右移一位DS1302_SCL = 1; /發(fā)送數(shù)據(jù),上升沿有效/* Function Name - DS1302讀出一個(gè)字節(jié)數(shù)據(jù)* Description - none* Input - none* Output - none* Reatu
6、rn - 讀到的數(shù)據(jù)*/unsigned DS1302_Read_Byte()u8 ReData=0*00;u8 i;DS1302_SDA_IN(); /設(shè)置為輸入口for(i=0;i= 1; /數(shù)據(jù)右移一位,先讀取低位,數(shù)據(jù)補(bǔ)0DS1302_SCL = 1; /上升沿讀取數(shù)據(jù)return(ReData); /返回讀取到的數(shù)據(jù)/* Function Name - 向DS1302*地址寫入數(shù)據(jù)* Description - none* Input - add:要操作的地址* dat:要寫入的數(shù)據(jù)* Output - none* Reaturn - none*/void DS1302_Write_
7、Data(u8 add,u8 dat)DS1302_RST = 0; /復(fù)位腳拉低DS1302_SCL = 0; /時(shí)鐘線拉低DS1302_RST = 1; /復(fù)位腳拉高DS1302_Write_Byte(add); /寫入要操作地址DS1302_Write_Byte(dat); /寫入數(shù)據(jù)/delay_us(5);DS1302_RST = 0;DS1302_SCL = 0;/* Function Name - 從DS1302*地址讀取數(shù)據(jù)* Description - none* Input - add:要操作的地址* Output - none* Reaturn - 要讀取的存放器的數(shù)值*
8、/unsigned DS1302_Read_Data(u8 add)u8 Temp;DS1302_RST = 1;DS1302_Write_Byte(add); /寫入要操作地址Temp = DS1302_Read_Byte(); /開場(chǎng)讀取數(shù)據(jù)DS1302_RST = 0;return(Temp); /返回讀取到的數(shù)據(jù)/* Function Name - 主電源對(duì)備用電池充電設(shè)置* Description - 如果備用電池接的是可充電的鋰電池或者其他可充電電池的時(shí)候,* 可以翻開DS1302的充電電路,利用主供電對(duì)電池進(jìn)展充電,免的換電池的麻煩* Input - *CHG_dat:存放器控制
9、指針* Output - none* Reaturn - none*/void DS1302_Charge_Manage(Charge_Typedef* CHG_dat)u8 CHG_Value;CHG_Value = (CHG_dat-TCS* DS* RS*; DS1302_WP_Disable(); /取消寫保護(hù)DS1302_Write_Data(Trickle_Charger_Address,CHG_Value);DS1302_WP_Enable(); /翻開寫保護(hù)/* Function Name - DS1302置的RAM讀寫操作* Description - none* Input
10、 - *pBuff:讀寫數(shù)據(jù)存放區(qū)* WRadd:讀寫起始地址,圍在RAM_Address0 RAM_Address28之間,最后一位地址有其他用途* num:讀寫字節(jié)數(shù)據(jù)的數(shù)量,圍在1 28之間* RW:讀寫判斷位。0*00為寫操作,0*01為讀操作* Output - none* Reaturn - none*/void DS1302_RAM_WriteRead_Data(u8* pBuff,u8 WRadd,u8 num,u8 RW) u8 i; if(WRadd = RAM_Address29) return; /要寫入數(shù)據(jù)的RAM地址是最后一個(gè),直接退出/因?yàn)樽詈笠粋€(gè)字節(jié)是用來檢測(cè)D
11、S1302的if(RW = 0*00) /寫數(shù)據(jù)操作for(i = 0;i num;i+)DS1302_WP_Disable(); /取消寫保護(hù)DS1302_Write_Data(WRadd+(i1),pBuffi);DS1302_WP_Enable(); /翻開寫保護(hù)elsefor(i = 0;i num;i+)DS1302_WP_Disable(); /取消寫保護(hù)pBuffi = DS1302_Read_Data(WRadd+1+(i 測(cè)試設(shè)備好壞* Description - 在DS1302芯片的RAM的最后一個(gè)地址寫入一個(gè)數(shù)據(jù)并讀出來判斷* 與上次寫入的值相等,不是第一次上電,否則則初
12、始化時(shí)間* Input - none* Output - none* Reaturn - 0:設(shè)備正常并不是第一次上電* 1:設(shè)備錯(cuò)誤或者已損壞*/u8 DS1302_Check(void)u8 test_value;DS1302_WP_Disable(); /取消寫保護(hù)test_value = DS1302_Read_Data(RAM_Address29 + 1); /讀出最后一個(gè)RAM地址里的數(shù)據(jù)DS1302_WP_Enable(); /翻開寫保護(hù)if(test_value = test_data) /判斷數(shù)據(jù)是否對(duì)return 0; /設(shè)備正常,不是第一次上電elsereturn 1;
13、/不是第一次上電或者設(shè)備異常/ DS1302_WP_Disable(); /取消寫保護(hù)/ DS1302_Write_Data(RAM_Address29,0*55); /向RAM最后一個(gè)地址里寫入數(shù)據(jù)/ DS1302_WP_Enable(); /翻開寫保護(hù)/ / _nop_();_nop_();/ / DS1302_WP_Disable(); /取消寫保護(hù)/ test_value = DS1302_Read_Data(RAM_Address29 + 1); /讀出最后一個(gè)RAM地址里的數(shù)據(jù)/ DS1302_WP_Enable(); /翻開寫保護(hù)/ / if(test_value = test_
14、data) return 0; /設(shè)備正常,不是第一次上電/ return 1; /設(shè)備不在線或者已損壞/* Function Name - DS1302初始化* Description - none* Input - *Time: 時(shí)間構(gòu)造體指針* Output - none* Reaturn - none*/void DS1302_Init(Time_Typedef* Time) DS1302_RST = 0;DS1302_SCL = 0;DS1302_WP_Disable(); /取消寫保護(hù)DS1302_Write_Data(Address_year,(Time-year)/10)yea
15、r)%10); /寫年數(shù)據(jù)delay_us(3);DS1302_Write_Data(Address_week,(Time-week)/10)week)%10); /寫星期數(shù)據(jù)delay_us(3);DS1302_Write_Data(Address_month,(Time-month)/10)month)%10);/寫月數(shù)據(jù)delay_us(3);DS1302_Write_Data(Address_date,(Time-date)/10)date)%10); /寫日數(shù)據(jù)delay_us(3);DS1302_Write_Data(Address_hour,(Time-hour)/10)hour
16、)%10); /寫小時(shí)數(shù)據(jù)delay_us(3);DS1302_Write_Data(Address_minute,(Time-minute)/10)minute)%10); /寫分鐘數(shù)據(jù)delay_us(3);DS1302_Write_Data(Address_second ,(Time-second)/10)second)%10); /寫秒數(shù)據(jù)并使時(shí)鐘運(yùn)行delay_us(3);DS1302_WP_Enable(); /翻開寫保護(hù)DS1302_WP_Disable(); /取消寫保護(hù)DS1302_Write_Data(RAM_Address29,test_data); /向RAM最后一個(gè)地
17、址里寫入數(shù)據(jù)DS1302_WP_Enable(); /翻開寫保護(hù)mw_shl_code=c,true#include led.h#include delay.h#include sys.h#include ds1302.h#include usart.h#define Date TimeValue.date#define Min TimeValue.minute#define Sec TimeValue.second#define Hour TimeValue.hour#define Week TimeValue.week#define Month TimeValue.month#define
18、 Year TimeValue.yearint main()Time_Typedef TimeValue;delay_init();LED_Init();uart_init(115200);DS1302_GPIOInit();DS1302_Init(&TimeValue);while(1)Sec=(DS1302_Read_Data(Address_second|0*01)&0*0f+(DS1302_Read_Data(Address_second|0*01)4)*10);delay_us(3);Min=(DS1302_Read_Data(Address_second|0*01)&0*0f+(D
19、S1302_Read_Data(Address_second|0*01)4)*10);delay_us(3);Hour=(DS1302_Read_Data(Address_second|0*01)&0*0f+(DS1302_Read_Data(Address_second|0*01)4)*10);delay_us(3);Date=(DS1302_Read_Data(Address_second|0*01)&0*0f+(DS1302_Read_Data(Address_second|0*01)4)*10);delay_us(3);Month=(DS1302_Read_Data(Address_s
20、econd|0*01)&0*0f+(DS1302_Read_Data(Address_second|0*01)4)*10);delay_us(3);Week=(DS1302_Read_Data(Address_second|0*01)&0*0f+(DS1302_Read_Data(Address_second|0*01)4)*10);delay_us(3);Year=(DS1302_Read_Data(Address_second|0*01)&0*0f+(DS1302_Read_Data(Address_second|0*01)4)*10);delay_us(3);printf(%d年%d月%
21、d日%d:%d:%d 星期%drn,Year,Month,Date,Hour,Min,Sec,Week);delay_ms(300);C 純文本查看復(fù)制代碼001002003004005006007008009010011012013014015016017018019020021022023024025026027028029030031032033034035036037038039040041042043044045046047048049050051052053054055056057058059060061062063064065066067068069070071072073074
22、075076077078079080081082083084085086087088089090091092093094095096097098099100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
23、175176177178179180181182183184185186187188189190191192193/* File name - ds1302.h* Author - By Sam Chan* Version - V1.0* Date - 08 - 23 - 2012* Brief - 時(shí)鐘芯片DS1302驅(qū)動(dòng)* Copyright (C) 20* All rights reserved* File Update* Author - By Sam Chan* Date - 10 - 20 - 2013 * Revise - A、增加對(duì)STM32控制的移植支持* - B、增加對(duì)置的RAM操作相關(guān)函數(shù)* - C、增加檢測(cè)DS1302是否存在,是否第一次上電檢測(cè)函數(shù)* - D、增加對(duì)DS1302置的鋰電池充電存放器參數(shù)設(shè)置函數(shù)和構(gòu)造體*/#ifndef _ds1302_h_ #define _ds1302_h_/*外部函數(shù)頭文件*/#include sys.h#include delay.h/*定義接口所接的IO不一樣請(qǐng)修改這里設(shè)置IO的方向請(qǐng)修改這里,對(duì)于不是具有準(zhǔn)雙向IO的MCU*/#define DS1302_SDA_IN() GPIOC-CRL &= 0*
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年河北省石家莊市裕華區(qū)中考模擬歷史試卷(含答案)
- 鹽城耐磨地坪合同協(xié)議
- 疫情店面房租賃合同協(xié)議
- 玉米半托管種植合同協(xié)議
- 珠寶店員工配股合同協(xié)議
- 珠寶高薪誠聘合同協(xié)議
- 留學(xué)機(jī)構(gòu)勞動(dòng)合同協(xié)議
- 電動(dòng)車供貨協(xié)議合同協(xié)議
- 電腦及配件協(xié)議供貨合同
- 瑜伽館全職老師合同協(xié)議
- GB/T 12785-2002潛水電泵試驗(yàn)方法
- 機(jī)械制圖國家標(biāo)準(zhǔn)
- 汽車吊起重吊裝方案-
- 文藝心理學(xué)課件
- 陰囊疾病超聲診斷課件
- 信息資產(chǎn)及分級(jí)管理程序
- 信用修復(fù)授權(quán)委托書
- 危大工程驗(yàn)收記錄表(腳手架工程)
- GA∕T 1729-2020 保安防衛(wèi)棍-行業(yè)標(biāo)準(zhǔn)
- 綜合部崗位廉潔風(fēng)險(xiǎn)點(diǎn)及防范措施匯總表
- 燈具成品檢驗(yàn)報(bào)告
評(píng)論
0/150
提交評(píng)論