Hi! I'm configuring RTC on nrf51822. I'm setting up the LFCLK and RTC module by configuring their registers. I have set up a simple IRQ handler, but it seems that it never happens to execute. What can I be missing?
This is my source code:
#define LFCLK_FREQUENCY (32768UL) /**< LFCLK HZ. */
#define RTC_FREQUENCY (8UL) /**< RTC_FREQUENCY HZ. */
#define COMPARE_COUNTERTIME (1UL) /**< Get Compare event COMPARE_TIME seconds after the counter starts from 0. */
#define COUNTER_PRESCALER ((LFCLK_FREQUENCY/RTC_FREQUENCY) - 1) /* f = LFCLK/(prescaler + 1) */
#define LED_1 18
#define LED_2 19
void lfclk_config(void)
{
NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
NRF_CLOCK->TASKS_LFCLKSTART = 1;
while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
{
//Do nothing.
}
}
void rtc_config(void)
{
NVIC_EnableIRQ(RTC0_IRQn); // RTC
NRF_RTC0->PRESCALER = COUNTER_PRESCALER; // prescaler to a TICK of RTC_FREQUENCY.
NRF_RTC0->CC[0] = COMPARE_COUNTERTIME * RTC_FREQUENCY;; // Compare0 after approx COMPARE_COUNTERTIME seconds.
// Enable COMPARE0 event and COMPARE0 interrupt:
NRF_RTC0->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
NRF_RTC0->INTENSET = RTC_INTENSET_COMPARE0_Msk;
}
void RTC0_IRQHandler()
{
if ((NRF_RTC0->EVENTS_COMPARE[0] != 0) && ((NRF_RTC0->INTENSET & RTC_INTENSET_COMPARE0_Msk) != 0))
{
NRF_RTC0->EVENTS_COMPARE[0] = 0;
digitalWrite(LED_1, HIGH);
NRF_RTC0->TASKS_CLEAR = 1;
}
}
void setup() {
pinMode(LED_1, OUTPUT);
lfclk_config();
rtc_config();
}
void loop() {
__WFE();
__SEV();
__WFE();
}