?? rtc_time.c
字號:
/*******************************************************************************
* 本文件實現基于RTC的日期功能,提供年月日的讀寫。(基于ANSI-C的time.h)
*
* 作者:jjldc (九九)
* QQ: 77058617
*
* RTC中保存的時間格式,是UNIX時間戳格式的。即一個32bit的time_t變量(實為u32)
*
* ANSI-C的標準庫中,提供了兩種表示時間的數據 型:
* time_t: UNIX時間戳(從1970-1-1起到某時間經過的秒數)
* typedef unsigned int time_t;
*
* struct tm: Calendar格式(年月日形式)
* tm結構如下:
* struct tm {
* int tm_sec; // 秒 seconds after the minute, 0 to 60
* (0 - 60 allows for the occasional leap second)
* int tm_min; // 分 minutes after the hour, 0 to 59
* int tm_hour; // 時 hours since midnight, 0 to 23
* int tm_mday; // 日 day of the month, 1 to 31
* int tm_mon; // 月 months since January, 0 to 11
* int tm_year; // 年 years since 1900
* int tm_wday; // 星期 days since Sunday, 0 to 6
* int tm_yday; // 從元旦起的天數 days since January 1, 0 to 365
* int tm_isdst; // 夏令時??Daylight Savings Time flag
* ...
* }
* 其中wday,yday可以自動產生,軟件直接讀取
* mon的取值為0-11
* ***注意***:
* tm_year:在time.h庫中定義為1900年起的年份,即2008年應表示為2008-1900=108
* 這種表示方法對用戶來說不是十分友好,與現實有較大差異。
* 所以在本文件中,屏蔽了這種差異。
* 即外部調用本文件的函數時,tm結構體類型的日期,tm_year即為2008
* 注意:若要調用系統庫time.c中的函數,需要自行將tm_year-=1900
*
* 成員函數說明:
* struct tm Time_ConvUnixToCalendar(time_t t);
* 輸入一個Unix時間戳(time_t),返回Calendar格式日期
* time_t Time_ConvCalendarToUnix(struct tm t);
* 輸入一個Calendar格式日期,返回Unix時間戳(time_t)
* time_t Time_GetUnixTime(void);
* 從RTC取當前時間的Unix時間戳值
* struct tm Time_GetCalendarTime(void);
* 從RTC取當前時間的日歷時間
* void Time_SetUnixTime(time_t);
* 輸入UNIX時間戳格式時間,設置為當前RTC時間
* void Time_SetCalendarTime(struct tm t);
* 輸入Calendar格式時間,設置為當前RTC時間
*
* 外部調用實例:
* 定義一個Calendar格式的日期變量:
* struct tm now;
* now.tm_year = 2008;
* now.tm_mon = 11; //12月
* now.tm_mday = 20;
* now.tm_hour = 20;
* now.tm_min = 12;
* now.tm_sec = 30;
*
* 獲取當前日期時間:
* tm_now = Time_GetCalendarTime();
* 然后可以直接讀tm_now.tm_wday獲取星期數
*
* 設置時間:
* Step1. tm_now.xxx = xxxxxxxxx;
* Step2. Time_SetCalendarTime(tm_now);
*
* 計算兩個時間的差
* struct tm t1,t2;
* t1_t = Time_ConvCalendarToUnix(t1);
* t2_t = Time_ConvCalendarToUnix(t2);
* dt = t1_t - t2_t;
* dt就是兩個時間差的秒數
* dt_tm = mktime(dt); //注意dt的年份匹配,ansi庫中函數為相對年份,注意超限
* 另可以參考相關資料,調用ansi-c庫的格式化輸出等功能,ctime,strftime等
*
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
#include "RTC_Time.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : Time_ConvUnixToCalendar(time_t t)
* Description : 轉換UNIX時間戳為日歷時間
* Input : u32 t 當前時間的UNIX時間戳
* Output : None
* Return : struct tm
*******************************************************************************/
struct tm Time_ConvUnixToCalendar(time_t t)
{
struct tm *t_tm;
t_tm = localtime(&t);
t_tm->tm_year += 1900; //localtime轉換結果的tm_year是相對值,需要轉成絕對值
return *t_tm;
}
/*******************************************************************************
* Function Name : Time_ConvCalendarToUnix(struct tm t)
* Description : 寫入RTC時鐘當前時間
* Input : struct tm t
* Output : None
* Return : time_t
*******************************************************************************/
time_t Time_ConvCalendarToUnix(struct tm t)
{
t.tm_year -= 1900; //外部tm結構體存儲的年份為2008格式
//而time.h中定義的年份格式為1900年開始的年份
//所以,在日期轉換時要考慮到這個因素。
return mktime(&t);
}
/*******************************************************************************
* Function Name : Time_GetUnixTime()
* Description : 從RTC取當前時間的Unix時間戳值
* Input : None
* Output : None
* Return : time_t t
*******************************************************************************/
time_t Time_GetUnixTime(void)
{
return (time_t)RTC_GetCounter();
}
/*******************************************************************************
* Function Name : Time_GetCalendarTime()
* Description : 從RTC取當前時間的日歷時間(struct tm)
* Input : None
* Output : None
* Return : time_t t
*******************************************************************************/
struct tm Time_GetCalendarTime(void)
{
struct tm t_tm;
t_t = (time_t)RTC_GetCounter();
t_tm = Time_ConvUnixToCalendar(t_t);
return t_tm;
}
/*******************************************************************************
* Function Name : Time_SetUnixTime()
* Description : 將給定的Unix時間戳寫入RTC
* Input : time_t t
* Output : None
* Return : None
*******************************************************************************/
void Time_SetUnixTime(time_t t)
{
RTC_WaitForLastTask();
RTC_SetCounter((u32)t);
RTC_WaitForLastTask();
return;
}
/*******************************************************************************
* Function Name : Time_SetCalendarTime()
* Description : 將給定的Calendar格式時間轉換成UNIX時間戳寫入RTC
* Input : struct tm t
* Output : None
* Return : None
*******************************************************************************/
void Time_SetCalendarTime(struct tm t)
{
Time_SetUnixTime(Time_ConvCalendarToUnix(t));
return;
}
////////////////////////////////////////////////////////////////////////////////
// RTC時鐘初始化!
////////////////////////////////////////////////////////////////////////////////
/*******************************************************************************
* Function Name : RTC_Configuration
* Description : 來重新配置RTC和BKP,僅在檢測到后備寄存器數據丟失時使用
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RTC_Configuration(void)
{
//啟用PWR和BKP的時鐘(from APB1)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
//后備域解鎖
PWR_BackupAccessCmd(ENABLE);
//備份寄存器模塊復位
BKP_DeInit();
//外部32.768K其喲偶那個
RCC_LSEConfig(RCC_LSE_ON);
//等待穩定
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
//RTC時鐘源配置成LSE(外部32.768K)
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
//RTC開啟
RCC_RTCCLKCmd(ENABLE);
//開啟后需要等待APB1時鐘與RTC時鐘同步,才能讀寫寄存器
RTC_WaitForSynchro();
//讀寫寄存器前,要確定上一個操作已經結束
RTC_WaitForLastTask();
//設置RTC分頻器,使RTC時鐘為1Hz
//RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1)
RTC_SetPrescaler(32767);
//等待寄存器寫入完成
RTC_WaitForLastTask();
//使能秒中斷
RTC_ITConfig(RTC_IT_SEC, ENABLE);
//等待寫入完成
RTC_WaitForLastTask();
return;
}
/*******************************************************************************
* Function Name : RTC_Config
* Description : 上電時調用本函數,自動檢查是否需要RTC初始化,
* 若需要重新初始化RTC,則調用RTC_Configuration()完成相應操作
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RTC_Config(void)
{
//我們在BKP的后備寄存器1中,存了一個特殊字符0xA5A5
//第一次上電或后備電源掉電后,該寄存器數據丟失,
//表明RTC數據丟失,需要重新配置
if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
{
//重新配置RTC
RTC_Configuration();
//配置完成后,向后備寄存器中寫特殊字符0xA5A5
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
}
else
{
//若后備寄存器沒有掉電,則無需重新配置RTC
//這里我們可以利用RCC_GetFlagStatus()函數查看本次復位類型
if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
{
//這是上電復位
}
else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
{
//這是外部RST管腳復位
}
//清除RCC中復位標志
RCC_ClearFlag();
//雖然RTC模塊不需要重新配置,且掉電后依靠后備電池依然運行
RCC_RTCCLKCmd(ENABLE);
//等待RTC時鐘與APB1時鐘同步
RTC_WaitForSynchro();
//使能秒中斷
RTC_ITConfig(RTC_IT_SEC, ENABLE);
//等待操作完成
RTC_WaitForLastTask();
}
RCC_ClearFlag();
/* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
#ifdef RTCClockOutput_Enable
/* Disable the Tamper Pin */
BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper
functionality must be disabled */
/* Enable RTC Clock Output on Tamper Pin */
BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
#endif
return;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -