nRF RTC相关

首先在 sdk_config.h 中 勾选 nRF_Drivers --> NRFX_RTC_ENABLED 、 nRF_Drivers --> RTC_ENABLED 以及 nRF_Drivers --> RTC_ENABLED --> RTC2_ENABLED

 

(注:在没有协议栈的操作下选择RTC0,在有协议栈并且有定时器模块的情况下选择RTC2,因为协议栈的时钟源使用的是RTC0,定时器模块使用了RTC1)

 

配置RTC,(RTC频率 = 32.768kHZ/(prescaler+1)),使用 nrf_drv_rtc_tick_enable 使能 TICK 事件,自行选择是否启动比较事件和溢出事件(一组RTC有三个比较通道,使用 nrf_drv_rtc_cc_set 使能;溢出事件使用 nrf_drv_rtc_overflow_enable 使能)如下配置为每 125ms 进一次 NRF_DRV_RTC_INT_TICK 事件 (注:1HZ = 一秒一次)

/** @brief: Function for handling the RTC2 interrupts.
 * Triggered on TICK match.
 */
static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
{
    static uint16_t ms_tick_count = 0;
    static uint8_t sec_tick_count = 0;
    static uint8_t min_tick_count = 0;
    static uint8_t hour_tick_count = 0;

    switch(int_type)
    {
        #ifdef RTC2_COMPARE_ENABLED
        case NRF_DRV_RTC_INT_COMPARE0:
        {
            NRF_LOG_INFO("NRF_DRV_RTC_INT_COMPARE0");
        } break;

        case NRF_DRV_RTC_INT_COMPARE1:
        {
            NRF_LOG_INFO("NRF_DRV_RTC_INT_COMPARE1");
        } break;

        case NRF_DRV_RTC_INT_COMPARE2:
        {
            NRF_LOG_INFO("NRF_DRV_RTC_INT_COMPARE2");
        } break;
        #endif

        case NRF_DRV_RTC_INT_TICK:
        {
            ms_tick_count += 125;

            if(ms_tick_count>=1000)
            {
                ms_tick_count = 0;
                sec_tick_count++;
                if(sec_tick_count>=60)
                {
                    sec_tick_count = 0;
                    min_tick_count++;

                    if(min_tick_count>=60)
                    {
                        hour_tick_count++;

                        if(hour_tick_count>=24)
                        {
                            hour_tick_count = 0;
                        }
                    }
                }
            }

            NRF_LOG_INFO("This time is:%2d hour,%2d min,%2d s,%2d ms.",hour_tick_count,min_tick_count,sec_tick_count,ms_tick_count);
        } break;

        #ifdef RTC2_OVERFLOW_ENABLED
        case NRF_DRV_RTC_INT_OVERFLOW:
        {
            NRF_LOG_INFO("NRF_DRV_RTC_INT_OVERFLOW");
        } break;
        #endif

        default:
            break;
    }
}


/** @brief Function initialization and configuration of RTC driver instance.
 */
static void rtc_config(void)
{
    uint32_t err_code;

    // Initialize RTC instance
    nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
    config.prescaler = 4095; // f(RTC) = 32.768kHZ/(prescaler+1) = 8HZ = 125ms

    err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
    APP_ERROR_CHECK(err_code);

    // Enable tick event & interrupt
    nrf_drv_rtc_tick_enable(&rtc,true);

    #ifdef RTC2_COMPARE_ENABLED
    // Set compare channel to trigger interrupt after COMPARE0_COUNTERTIME seconds
    err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE0_COUNTERTIME * 8,true);
    APP_ERROR_CHECK(err_code);
    #endif

    //Power on RTC instance
    nrf_drv_rtc_enable(&rtc);
}

具体实例参考 1_disease_control 项目

你可能感兴趣的:(nRF RTC相关)