6 years, 6 months ago.

Implementing nrf51822 timer example

Hi,

I am using the PCA10001 eval board and I have been able to get things up and running using the mbed platform but now I am implementing a timer based interrupt for that I was using a ticker. But detaching and reattaching the ticker results in the ticker not working properly. My time for calling the interrupt is 1ms.

Facing issues with the ticker I want to use the nrf51 timer1 or 2. I ran the example code here https://github.com/NordicSemiconductor/nrf51-TIMER-examples/blob/master/timer_example_timer_mode/main.c

with a few modifications

#include "mbed.h"

DigitalOut myled(LED1);
RawSerial pc(USBTX, USBRX);

void start_timer(void)
{       
  NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer;  // Set the timer in Counter Mode
  NRF_TIMER2->TASKS_CLEAR = 1;               // clear the task first to be usable for later
    NRF_TIMER2->PRESCALER = 6;                             //Set prescaler. Higher number gives slower timer. Prescaler = 0 gives 16MHz timer
    NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit;       //Set counter to 16 bit resolution
    NRF_TIMER2->CC[0] = 25000;                             //Set value for TIMER2 compare register 0
    NRF_TIMER2->CC[1] = 5;                                 //Set value for TIMER2 compare register 1
        
  // Enable interrupt on Timer 2, both for CC[0] and CC[1] compare match events
    NRF_TIMER2->INTENSET = (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos) | (TIMER_INTENSET_COMPARE1_Enabled << TIMER_INTENSET_COMPARE1_Pos);
  

  NVIC_EnableIRQ(TIMER2_IRQn);
        
  NRF_TIMER2->TASKS_START = 1;               // Start TIMER2
}
        
/** TIMTER2 peripheral interrupt handler. This interrupt handler is called whenever there it a TIMER2 interrupt
 */
void TIMER2_IRQHandler(void)
{
    if ((NRF_TIMER2->EVENTS_COMPARE[0] != 0) && ((NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE0_Msk) != 0))
  {
        NRF_TIMER2->EVENTS_COMPARE[0] = 0;           //Clear compare register 0 event   
        myled=1;         //Set LED
  }
    
    if ((NRF_TIMER2->EVENTS_COMPARE[1] != 0) && ((NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE1_Msk) != 0))
  {
        NRF_TIMER2->EVENTS_COMPARE[1] = 0;           //Clear compare register 1 event
        myled=0;      //Clear LED
  }
}

int main(void)
{
      pc.baud(9600);

    start_timer();                              //Configure and start timer
    
    while(true) 
    {
//              pc.printf("%i\n",1);

//         Enter System ON sleep mode
        __WFE();
        // Make sure any pending events are cleared
        __SEV();
        __WFE();
        
//         For more information on the WFE - SEV - WFE sequence, please refer to the following Devzone article:
         //https://devzone.nordicsemi.com/index.php/how-do-you-put-the-nrf51822-chip-to-sleep#reply-1589
    }
}

It compiles with no errors but nothing happens. Any ideas on what I could be doing wrong.

I have tried using both updated and old mbed libraries.

I commented on Stack Overflow as well, but what do you mean with "But detaching and reattaching the ticker results in the ticker not working properly."

posted by Jan Jongboom 27 Sep 2017
Be the first to answer this question.