ds1302程序-stm_第1頁
ds1302程序-stm_第2頁
ds1302程序-stm_第3頁
ds1302程序-stm_第4頁
ds1302程序-stm_第5頁
已閱讀5頁,還剩12頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、. /* File name - ds1302.c* Author - By Sam Chan* Version - V1.0* Date - 08 - 23 - 2012* Brief - 時鐘芯片DS1302驅動* Copyright (C) 20* All rights reserved* File Update* Author - By Sam Chan* Date - 10 - 20 - 2013 * Revise - A、增加對STM32控制的移植支持* - B、增加對置的RAM操作相關函數* - C、增加檢測DS1302是否存在,是否第一次上電檢測函數* - D、增加對DS130

2、2置的鋰電池充電存放器參數設置函數和構造體*/#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; /定義時間數據指針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外設時鐘/* 初始化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寫入一個字節數據* Description - none* Input - dat:要寫入的數據* Output - none* Reaturn - none */void DS1302_Write_Byte(u8 dat)u8 i;DS1302_SDA_OUT(); /設置為輸出口for(i=0;i= 1; /數據右移一位DS1302_SCL = 1; /發送數據,上升沿有效/* Function Name - DS1302讀出一個字節數據* Description - none* Input - none* Output - none* Reatu

6、rn - 讀到的數據*/unsigned DS1302_Read_Byte()u8 ReData=0*00;u8 i;DS1302_SDA_IN(); /設置為輸入口for(i=0;i= 1; /數據右移一位,先讀取低位,數據補0DS1302_SCL = 1; /上升沿讀取數據return(ReData); /返回讀取到的數據/* Function Name - 向DS1302*地址寫入數據* Description - none* Input - add:要操作的地址* dat:要寫入的數據* Output - none* Reaturn - none*/void DS1302_Write_

7、Data(u8 add,u8 dat)DS1302_RST = 0; /復位腳拉低DS1302_SCL = 0; /時鐘線拉低DS1302_RST = 1; /復位腳拉高DS1302_Write_Byte(add); /寫入要操作地址DS1302_Write_Byte(dat); /寫入數據/delay_us(5);DS1302_RST = 0;DS1302_SCL = 0;/* Function Name - 從DS1302*地址讀取數據* Description - none* Input - add:要操作的地址* Output - none* Reaturn - 要讀取的存放器的數值*

8、/unsigned DS1302_Read_Data(u8 add)u8 Temp;DS1302_RST = 1;DS1302_Write_Byte(add); /寫入要操作地址Temp = DS1302_Read_Byte(); /開場讀取數據DS1302_RST = 0;return(Temp); /返回讀取到的數據/* Function Name - 主電源對備用電池充電設置* Description - 如果備用電池接的是可充電的鋰電池或者其他可充電電池的時候,* 可以翻開DS1302的充電電路,利用主供電對電池進展充電,免的換電池的麻煩* 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(); /取消寫保護DS1302_Write_Data(Trickle_Charger_Address,CHG_Value);DS1302_WP_Enable(); /翻開寫保護/* Function Name - DS1302置的RAM讀寫操作* Description - none* Input

10、 - *pBuff:讀寫數據存放區* WRadd:讀寫起始地址,圍在RAM_Address0 RAM_Address28之間,最后一位地址有其他用途* num:讀寫字節數據的數量,圍在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; /要寫入數據的RAM地址是最后一個,直接退出/因為最后一個字節是用來檢測D

11、S1302的if(RW = 0*00) /寫數據操作for(i = 0;i num;i+)DS1302_WP_Disable(); /取消寫保護DS1302_Write_Data(WRadd+(i1),pBuffi);DS1302_WP_Enable(); /翻開寫保護elsefor(i = 0;i num;i+)DS1302_WP_Disable(); /取消寫保護pBuffi = DS1302_Read_Data(WRadd+1+(i 測試設備好壞* Description - 在DS1302芯片的RAM的最后一個地址寫入一個數據并讀出來判斷* 與上次寫入的值相等,不是第一次上電,否則則初

12、始化時間* Input - none* Output - none* Reaturn - 0:設備正常并不是第一次上電* 1:設備錯誤或者已損壞*/u8 DS1302_Check(void)u8 test_value;DS1302_WP_Disable(); /取消寫保護test_value = DS1302_Read_Data(RAM_Address29 + 1); /讀出最后一個RAM地址里的數據DS1302_WP_Enable(); /翻開寫保護if(test_value = test_data) /判斷數據是否對return 0; /設備正常,不是第一次上電elsereturn 1;

13、/不是第一次上電或者設備異常/ DS1302_WP_Disable(); /取消寫保護/ DS1302_Write_Data(RAM_Address29,0*55); /向RAM最后一個地址里寫入數據/ DS1302_WP_Enable(); /翻開寫保護/ / _nop_();_nop_();/ / DS1302_WP_Disable(); /取消寫保護/ test_value = DS1302_Read_Data(RAM_Address29 + 1); /讀出最后一個RAM地址里的數據/ DS1302_WP_Enable(); /翻開寫保護/ / if(test_value = test_

14、data) return 0; /設備正常,不是第一次上電/ return 1; /設備不在線或者已損壞/* Function Name - DS1302初始化* Description - none* Input - *Time: 時間構造體指針* Output - none* Reaturn - none*/void DS1302_Init(Time_Typedef* Time) DS1302_RST = 0;DS1302_SCL = 0;DS1302_WP_Disable(); /取消寫保護DS1302_Write_Data(Address_year,(Time-year)/10)yea

15、r)%10); /寫年數據delay_us(3);DS1302_Write_Data(Address_week,(Time-week)/10)week)%10); /寫星期數據delay_us(3);DS1302_Write_Data(Address_month,(Time-month)/10)month)%10);/寫月數據delay_us(3);DS1302_Write_Data(Address_date,(Time-date)/10)date)%10); /寫日數據delay_us(3);DS1302_Write_Data(Address_hour,(Time-hour)/10)hour

16、)%10); /寫小時數據delay_us(3);DS1302_Write_Data(Address_minute,(Time-minute)/10)minute)%10); /寫分鐘數據delay_us(3);DS1302_Write_Data(Address_second ,(Time-second)/10)second)%10); /寫秒數據并使時鐘運行delay_us(3);DS1302_WP_Enable(); /翻開寫保護DS1302_WP_Disable(); /取消寫保護DS1302_Write_Data(RAM_Address29,test_data); /向RAM最后一個地

17、址里寫入數據DS1302_WP_Enable(); /翻開寫保護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 純文本查看復制代碼001002003004005006007008009010011012013014015016017018019020021022023024025026027028029030031032033034035036037038039040041042043044045046047048049050051052053054055056057058059060061062063064065066067068069070071072073074

22、075076077078079080081082083084085086087088089090091092093094095096097098099100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174

23、175176177178179180181182183184185186187188189190191192193/* File name - ds1302.h* Author - By Sam Chan* Version - V1.0* Date - 08 - 23 - 2012* Brief - 時鐘芯片DS1302驅動* Copyright (C) 20* All rights reserved* File Update* Author - By Sam Chan* Date - 10 - 20 - 2013 * Revise - A、增加對STM32控制的移植支持* - B、增加對置的RAM操作相關函數* - C、增加檢測DS1302是否存在,是否第一次上電檢測函數* - D、增加對DS1302置的鋰電池充電存放器參數設置函數和構造體*/#ifndef _ds1302_h_ #define _ds1302_h_/*外部函數頭文件*/#include sys.h#include delay.h/*定義接口所接的IO不一樣請修改這里設置IO的方向請修改這里,對于不是具有準雙向IO的MCU*/#define DS1302_SDA_IN() GPIOC-CRL &= 0*

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論