美文网首页物联网loT从业者物联网相关技术研究
CC2640R2F学习笔记(26)——RTC实时时钟使用

CC2640R2F学习笔记(26)——RTC实时时钟使用

作者: Leung_ManWah | 来源:发表于2019-11-08 13:43 被阅读0次

    一、简介

    实时时钟的缩写是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.cboard_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

    相关文章

      网友评论

        本文标题:CC2640R2F学习笔记(26)——RTC实时时钟使用

        本文链接:https://www.haomeiwen.com/subject/cglibctx.html