一、簡介
實時時鐘的縮寫是RTC(Real_Time Clock)。RTC 是集成電路,通常稱爲時鐘芯片。
實時時鐘芯片是日常生活中應用最爲廣泛的消費類電子產品之一。它爲人們提供精確的實時時間,或者爲電子系統提供精確的時間基準,目前實時時鐘芯片大多采用精度較高的晶體振盪器作爲時鐘源。有些時鐘芯片爲了在主電源掉電時,還可以工作,需要外加電池供電。
二、修改配置文件
如在 ble5_simple_peripheral_cc2640r2lp_app/TOOLS/src/app_ble.cfg
添加以下兩行:
var Seconds = xdc.useModule('ti.sysbios.hal.Seconds');
var SecondsProxy = xdc.useModule('ti.sysbios.hal.SecondsClock');
三、移植文件
鏈接:https://pan.baidu.com/s/1egd_3o6sImBc6uFaYcAvNw 提取碼:p4qy
將 board_rtc.c 、 board_rtc.h 兩個文件拖拽至CCS工程的Application文件夾下
3.1 board_rtc.c
/*********************************************************************
* INCLUDES
*/
#include "_hal_types.h"
#include <time.h>
#include <ti/sysbios/hal/Seconds.h>
#include "board_rtc.h"
/*********************************************************************
* PUBLIC FUNCTIONS
*/
/**
@brief 設置RTC時間
@param timeNow -[in] 當前時間
@return 無
*/
void RTC_SetTime(uint32 timeNow)
{
Seconds_set(timeNow);
}
/**
@brief 獲取RTC時間
@param 無
@return 當前時間
*/
uint32 RTC_GetTime(void)
{
return Seconds_get();
}
3.2 board_rtc.h
#ifndef _BOARD_RTC_H_
#define _BOARD_RTC_H_
/*********************************************************************
* INCLUDES
*/
#include "_hal_types.h"
/*********************************************************************
* API FUNCTIONS
*/
void RTC_SetTime(uint32 timeNow);
uint32 RTC_GetTime(void);
#endif /* _BOARD_RTC_H_ */
四、API調用
需包含頭文件 board_rtc.h
RTC_SetTime
功能 | 設置RTC時間 |
---|---|
函數定義 | void RTC_SetTime(uint32 timeNow) |
參數 | timeNow:當前時間(Unix時間戳) |
返回 | 無 |
RTC_GetTime
功能 | 獲取RTC時間 |
---|---|
函數定義 | uint32 RTC_GetTime(void) |
參數 | 無 |
返回 | 當前時間(Unix時間戳) |
五、使用例子
1)添加頭文件(例 simple_peripheral.c 中)
#include "board_rtc.h"
2)添加設置時間函數(simple_peripheral.c 的 SimplePeripheral_processGapMessage 函數 GAP_DEVICE_INIT_DONE_EVENT
設備初始化完成事件中)
static void SimplePeripheral_processGapMessage(gapEventHdr_t *pMsg)
{
switch(pMsg->opcode)
{
/*================================== 設備初始化完成事件 ==================================*/
case GAP_DEVICE_INIT_DONE_EVENT:
{
uint32 timeNow = 1412800000; /* Wed, 08 Oct 2014 20:26:40 GMT */
RTC_SetTime(timeNow);
}
}
3)獲取當前時間
/* Use overridden time() function to get the current time.
* Use standard C RTS library functions with return from time().
* Assumes Seconds_set() has been called as above */
time_t t1 = time(NULL);
struct tm *ltm = localtime(&t1);
char *curTime = asctime(ltm);
System_printf("Time(GMT): %s\n", curTime);
4)tm時間結構體
struct tm
{
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours after the midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since Jan 1st - [0,365] */
int tm_isdst; /* Daylight Savings Time flag */
};
• 由 Leung 寫於 2019 年 11 月 8 日
• 參考:TI-RTOS Kernel (SYS/BIOS) User’s Guide - 5.4Seconds Module