First, the contents of the experiment:
The RTC0 of the configuration NRF51822 has a tick frequency of 8hz,compare0 matching event triggering period of 3 seconds and enables the tick and COMPARE0 interrupts.
- TICK interrupt in the drive indicator D1 rollover State, that is, the LED D1 at a rate of 8Hz rollover state
- COMPARE0 Interrupt Highlight Indicator D2
Second, the internal RTC structure of nRF51822:
The NRF51822 has two RTC clocks: RTC0,RTC1. The two RTC are 24-bit, with a LFCLK low-frequency clock and a 12-bit divider that generates tick, compare, and overflow events. The RTC schematic diagram looks like this:
Third, the calculation of the counter increment frequency:
frtc[khz]=32.768/(prescaler+1)
As can be seen from the above, set the increment frequency is to set the corresponding prescaler, such as setting the increment frequency of 8Hz. Then, the Prescaler value is as follows:
Prescaler = Round (rounded) (32.768 khz/8 Hz) –1 = 4095
At this point, the increment period is: 125ms.
Four, the core source code analysis:
Main
1 intMainvoid)2 {3 leds_config ();4 lfclk_config ();5 rtc_config ();6 7 while(true)8 {9 __sev ();Ten __wfe (); One __wfe (); A } -}
RTC Initialization:
1 Static voidRtc_config (void)2 {3 uint32_t Err_code;4 5 //Initialize RTC Instance6Err_code =Nrf_drv_rtc_init (&RTC, NULL, rtc_handler);//Initialize RTC7 App_error_check (err_code);8 9 //Enable Tick Event & InterruptTen nrf_drv_rtc_tick_enable (&RTC, true);//Enable Tick event One A //Set Compare Channel to trigger interrupt after compare_countertime seconds -Err_code = Nrf_drv_rtc_cc_set (&RTC,0, Compare_countertime * rtc0_config_frequency,true); - App_error_check (err_code); the - //Power on RTC instance -Nrf_drv_rtc_enable (&RTC); -}
RTC Interrupt callback function:
1 /** @brief: Function for handling the RTC0 interrupts.2 * triggered on TICK and COMPARE0 match.3 */4 Static voidRtc_handler (nrf_drv_rtc_int_type_t int_type)5 {6 if(Int_type = =nrf_drv_rtc_int_compare0)7 {8 Nrf_gpio_pin_toggle (compare_event_output);9 }Ten Else if(Int_type = =Nrf_drv_rtc_int_tick) One { A Nrf_gpio_pin_toggle (tick_event_output); - } -}
@nRF51822 Basic Experiment Series:
[nRF51822] 7, basic experimental Code Analysis Daquan (top ten)
[nRF51822] 8, basic experimental code Analysis Daquan · Experimental 11-ppi
[nRF51822] 9, basic experimental code Analysis Daquan · Experimental 12-ADC
@beautifulzzzz -The Internet of Things & ubiquitous computing practitioners
e-mail: [Email protected]
I-blog:blog.beautifulzzzz.com
[nRF51822] 10, basic experimental code Analysis Daquan · Experimental 15-RTC