nRF52832 Timer interrupt not working

Hi,

I cannot get the interrupt on Timer1 to work. I have checked that the counter is matching the compare register by not enabling the interrupt. When I enable the interrupt the LED is never set.

I'm using the nRF52832 with SD132 and nRF5 SDK v11.0.0

Below is my code

void TIMER1_IRQHandler(void)
{
nrf_gpio_pin_set(RED_LED_PIN);
}

int main()
{

// Reset 16 MHz crystal
NRF_CLOCK->TASKS_HFCLKSTART = 1;
while(NRF_CLOCK->EVENTS_HFCLKSTARTED != 1)
{}
NRF_CLOCK->TASKS_LFCLKSTART = 1;
while(NRF_CLOCK->EVENTS_LFCLKSTARTED != 1)
{}
__enable_irq();

//Turn off LED
nrf_gpio_pin_clear(RED_LED_PIN);
//Config Pin
nrf_gpio_cfg(RED_LED_PIN, NRF_GPIO_PIN_DIR_OUTPUT,
NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_S0S1,
NRF_GPIO_PIN_NOSENSE);

//Enable Timer

sd_nvic_DisableIRQ(TIMER1_IRQn);
nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_STOP);
nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_CLEAR);

nrf_timer_mode_set(NRF_TIMER1, TIMER_MODE_MODE_Timer);
nrf_timer_bit_width_set(NRF_TIMER1, NRF_TIMER_BIT_WIDTH_32);
nrf_timer_frequency_set(NRF_TIMER1, NRF_TIMER_FREQ_31250Hz);
nrf_timer_cc_write(NRF_TIMER1, NRF_TIMER_CC_CHANNEL0, 65534);
nrf_timer_int_enable(NRF_TIMER1, NRF_TIMER_INT_COMPARE0_MASK);

nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_START);

sd_nvic_SetPriority(TIMER1_IRQn, 7);
sd_nvic_EnableIRQ(TIMER1_IRQn);

//Toggle LED to show alive

for(int i=0;i<2;) {
nrf_gpio_pin_toggle(RED_LED_PIN);
nrf_delay_ms(100);
i++;
}

while(1)
{
if(NRF_TIMER1->EVENTS_COMPARE[0] == 1)
{
nrf_gpio_pin_set(RED_LED_PIN);
}


}
}

  • Try this:

    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf_drv_timer.h"
    #include "nrf52.h"
    #include "nrf_gpio.h"
    
    #define RED_LED_PIN 17 // LED1 on nRF52-DK
    
    void TIMER1_IRQHandler(void)
    {
        if(NRF_TIMER1->EVENTS_COMPARE[0] == 1)
        {
            NRF_TIMER1->EVENTS_COMPARE[0] = 0;
            nrf_gpio_pin_toggle(RED_LED_PIN);    
        }
    }
    
    int main()
    {
        // Output LED pin
        nrf_gpio_pin_clear(RED_LED_PIN);
        nrf_gpio_cfg_output(RED_LED_PIN);
    
        //Enable ~1sec timer 
        NVIC_DisableIRQ(TIMER1_IRQn); // use "sd_nvic" prefix with softdevice
        
        nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_STOP);
        nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_CLEAR);
        
        nrf_timer_mode_set(NRF_TIMER1, NRF_TIMER_MODE_TIMER); 
        nrf_timer_bit_width_set(NRF_TIMER1, NRF_TIMER_BIT_WIDTH_32);
        nrf_timer_frequency_set(NRF_TIMER1, NRF_TIMER_FREQ_31250Hz);
        nrf_timer_cc_write(NRF_TIMER1, NRF_TIMER_CC_CHANNEL0, 31250);
        nrf_timer_int_enable(NRF_TIMER1, NRF_TIMER_INT_COMPARE0_MASK);
        nrf_timer_shorts_enable(NRF_TIMER1, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK); // self running
        
        NVIC_SetPriority(TIMER1_IRQn, 7); // use "sd_nvic" prefix with softdevice
        NVIC_EnableIRQ(TIMER1_IRQn); // use "sd_nvic" prefix with softdevice
    
        nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_START);
    
        while(1)
        {
        } 
    }