1. 程式人生 > >NRF52832 RTC for TICK

NRF52832 RTC for TICK

1.在sdk_config.h中加入巨集

// <e> NRF_CLOCK_ENABLED - nrf_drv_clock - CLOCK peripheral driver - legacy layer //========================================================== #ifndef NRF_CLOCK_ENABLED #define NRF_CLOCK_ENABLED 1 #endif // <o> CLOCK_CONFIG_LF_SRC  - LF Clock Source   // <0=> RC  // <1=> XTAL  // <2=> Synth  // <131073=> External Low Swing  // <196609=> External Full Swing 

#ifndef CLOCK_CONFIG_LF_SRC #define CLOCK_CONFIG_LF_SRC 1 #endif

// <o> CLOCK_CONFIG_IRQ_PRIORITY  - Interrupt priority  

// <i> Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice // <0=> 0 (highest)  // <1=> 1  // <2=> 2  // <3=> 3  // <4=> 4  // <5=> 5  // <6=> 6  // <7=> 7 

#ifndef CLOCK_CONFIG_IRQ_PRIORITY #define CLOCK_CONFIG_IRQ_PRIORITY 6 #endif

// <e> RTC_ENABLED - nrf_drv_rtc - RTC peripheral driver - legacy layer //========================================================== #ifndef RTC_ENABLED #define RTC_ENABLED 1 #endif // <o> RTC_DEFAULT_CONFIG_FREQUENCY - Frequency  <16-32768> 

#ifndef RTC_DEFAULT_CONFIG_FREQUENCY #define RTC_DEFAULT_CONFIG_FREQUENCY 32768 #endif

// <q> RTC_DEFAULT_CONFIG_RELIABLE  - Ensures safe compare event triggering  

#ifndef RTC_DEFAULT_CONFIG_RELIABLE #define RTC_DEFAULT_CONFIG_RELIABLE 0 #endif

// <o> RTC_DEFAULT_CONFIG_IRQ_PRIORITY  - Interrupt priority  

// <i> Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice // <0=> 0 (highest)  // <1=> 1  // <2=> 2  // <3=> 3  // <4=> 4  // <5=> 5  // <6=> 6  // <7=> 7 

#ifndef RTC_DEFAULT_CONFIG_IRQ_PRIORITY #define RTC_DEFAULT_CONFIG_IRQ_PRIORITY 6 #endif

// <q> RTC0_ENABLED  - Enable RTC0 instance  

#ifndef RTC0_ENABLED #define RTC0_ENABLED 1 #endif

// <q> RTC1_ENABLED  - Enable RTC1 instance  

#ifndef RTC1_ENABLED #define RTC1_ENABLED 0 #endif

// <q> RTC2_ENABLED  - Enable RTC2 instance  

#ifndef RTC2_ENABLED #define RTC2_ENABLED 0 #endif

// <e> NRFX_RTC_ENABLED - nrfx_rtc - RTC peripheral driver //========================================================== #ifndef NRFX_RTC_ENABLED #define NRFX_RTC_ENABLED 1 #endif // <q> NRFX_RTC0_ENABLED  - Enable RTC0 instance  

#ifndef NRFX_RTC0_ENABLED #define NRFX_RTC0_ENABLED 0 #endif

// <q> NRFX_RTC1_ENABLED  - Enable RTC1 instance  

#ifndef NRFX_RTC1_ENABLED #define NRFX_RTC1_ENABLED 0 #endif

// <q> NRFX_RTC2_ENABLED  - Enable RTC2 instance  

#ifndef NRFX_RTC2_ENABLED #define NRFX_RTC2_ENABLED 0 #endif

// <o> NRFX_RTC_MAXIMUM_LATENCY_US - Maximum possible time[us] in highest priority interrupt  #ifndef NRFX_RTC_MAXIMUM_LATENCY_US #define NRFX_RTC_MAXIMUM_LATENCY_US 2000 #endif

// <o> NRFX_RTC_DEFAULT_CONFIG_FREQUENCY - Frequency  <16-32768> 

#ifndef NRFX_RTC_DEFAULT_CONFIG_FREQUENCY #define NRFX_RTC_DEFAULT_CONFIG_FREQUENCY 32768 #endif

// <q> NRFX_RTC_DEFAULT_CONFIG_RELIABLE  - Ensures safe compare event triggering  

#ifndef NRFX_RTC_DEFAULT_CONFIG_RELIABLE #define NRFX_RTC_DEFAULT_CONFIG_RELIABLE 0 #endif

// <o> NRFX_RTC_DEFAULT_CONFIG_IRQ_PRIORITY  - Interrupt priority   // <0=> 0 (highest)  // <1=> 1  // <2=> 2  // <3=> 3  // <4=> 4  // <5=> 5  // <6=> 6  // <7=> 7 

#ifndef NRFX_RTC_DEFAULT_CONFIG_IRQ_PRIORITY #define NRFX_RTC_DEFAULT_CONFIG_IRQ_PRIORITY 6 #endif

// <e> NRFX_RTC_CONFIG_LOG_ENABLED - Enables logging in the module. //========================================================== #ifndef NRFX_RTC_CONFIG_LOG_ENABLED #define NRFX_RTC_CONFIG_LOG_ENABLED 0 #endif // <o> NRFX_RTC_CONFIG_LOG_LEVEL  - Default Severity level   // <0=> Off  // <1=> Error  // <2=> Warning  // <3=> Info  // <4=> Debug 

#ifndef NRFX_RTC_CONFIG_LOG_LEVEL #define NRFX_RTC_CONFIG_LOG_LEVEL 3 #endif

// <o> NRFX_RTC_CONFIG_INFO_COLOR  - ANSI escape code prefix.   // <0=> Default  // <1=> Black  // <2=> Red  // <3=> Green  // <4=> Yellow  // <5=> Blue  // <6=> Magenta  // <7=> Cyan  // <8=> White 

#ifndef NRFX_RTC_CONFIG_INFO_COLOR #define NRFX_RTC_CONFIG_INFO_COLOR 0 #endif

// <o> NRFX_RTC_CONFIG_DEBUG_COLOR  - ANSI escape code prefix.   // <0=> Default  // <1=> Black  // <2=> Red  // <3=> Green  // <4=> Yellow  // <5=> Blue  // <6=> Magenta  // <7=> Cyan  // <8=> White 

#ifndef NRFX_RTC_CONFIG_DEBUG_COLOR #define NRFX_RTC_CONFIG_DEBUG_COLOR 0 #endif

2.匯入檔案nrfx_rtc.c,nrfx_clock.c,nrf_drv_clock.c到工程

3.引入標頭檔案

#include "nrf_drv_rtc.h" #include "nrf_drv_clock.h"

4.定義變數 const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(0); /**< Declaring an instance of nrf_drv_rtc for RTC0. */ 5.RTC事件處理函式

/** @brief: Function for handling the RTC0 interrupts.  * Triggered on TICK and COMPARE0 match.  */ static void rtc_handler(nrf_drv_rtc_int_type_t int_type) {        if (int_type == 0)     {             }     else if (int_type == NRF_DRV_RTC_INT_TICK)     {        nrf_gpio_pin_toggle(31);     } }

6.初時化函式

/** @brief Function starting the internal LFCLK XTAL oscillator.  */ static void lfclk_config(void) {     ret_code_t err_code = nrf_drv_clock_init();     APP_ERROR_CHECK(err_code);

    nrf_drv_clock_lfclk_request(NULL); }

/** @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;     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);

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

7.在主函式中處理

 nrf_gpio_cfg_output(31);

    lfclk_config();

    rtc_config();