This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Timers not working on nRF52

Hello,

I'm developing a simple code that make use of timers using NRF-based boards and Arduino code.

The following code works fine on a Simblee nRF51822 board, but it crashes on a Adafruit Feather nRF52832 board (the variable 'flag' is never incremented).

long flag = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("setup");
timer_config();
}

void loop() {
// put your main code here, to run repeatedly:
Serial.println(flag);
delay(10);
}

void timer_config(void)
{
    NRF_TIMER2->TASKS_STOP = 1;                                      // Stop timer
    NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer;                        
    NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit;               
    NRF_TIMER2->PRESCALER = 9;                                       
    NRF_TIMER2->TASKS_CLEAR = 1;                                     // Clear timer 
    NRF_TIMER2->CC[0] = 25000; // CC[0] register holds interval count value i.e your desired cycle
    NRF_TIMER2->INTENSET = TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos;                 // Enable COMAPRE0 
    Interrupt
    NRF_TIMER2->SHORTS = (TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos);         // Count then 
    Complete mode enabled
    NVIC_EnableIRQ(TIMER2_IRQn);
    NRF_TIMER2->TASKS_START = 1;                                                                           // Start TIMER
 }

void TIMER2_IRQHandler(void)
{
    flag++;
    if (NRF_TIMER2->EVENTS_COMPARE[0] != 0)
    {        
         flag++;  //Increment flag
         NRF_TIMER2->EVENTS_COMPARE[0] = 0;
     }
  }

Any suggestion?

Thanks!

Simone

Related