This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Regarding nRF51 Timer

Hi. I'm using PCA10028 and SDK10.0.0, S130 v1.0.0. And "ble_app_hrs_c" example is my reference code.

It works well before i add code for timer as below.

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 = 0;                             //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] = 32768;							   //Set value for TIMER2 compare register 0 
NRF_TIMER2->CC[1] = 1;                                 //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 

}

void TIMER2_IRQHandler(void) {

nrf_gpio_pin_toggle(GPIO_TOGGLE_PIN_1);

}

int main(void) { bool erase_bonds;

// Initialize.
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, NULL);
buttons_leds_init(&erase_bonds);
uart_init();

#if 1 // timer test nrf_gpio_cfg_output(GPIO_TOGGLE_PIN); start_timer(); #endif

....... }

It does'nt work well after i add "start_timer()" function. I don't understand. Can you tell me why? I hope your reply.

Parents
  • You have to clear the generated events in the irq handler

        NRF_TIMER2->EVENTS_COMPARE[0] = 0;  
        NRF_TIMER2->EVENTS_COMPARE[1] = 0;  
        NRF_TIMER2->EVENTS_COMPARE[2] = 0;  
        NRF_TIMER2->EVENTS_COMPARE[3] = 0; 
    

    You also need to clear the timer when either COMPARE1 or COMPARE0 event is generated depending on which ever logic you need.'

    NRF_TIMER2->SHORTS |= TIMER_SHORTS_COMPARE0_CLEAR_Msk; 
    

    or

    NRF_TIMER2->SHORTS |= TIMER_SHORTS_COMPARE1_CLEAR_Msk;
    
Reply
  • You have to clear the generated events in the irq handler

        NRF_TIMER2->EVENTS_COMPARE[0] = 0;  
        NRF_TIMER2->EVENTS_COMPARE[1] = 0;  
        NRF_TIMER2->EVENTS_COMPARE[2] = 0;  
        NRF_TIMER2->EVENTS_COMPARE[3] = 0; 
    

    You also need to clear the timer when either COMPARE1 or COMPARE0 event is generated depending on which ever logic you need.'

    NRF_TIMER2->SHORTS |= TIMER_SHORTS_COMPARE0_CLEAR_Msk; 
    

    or

    NRF_TIMER2->SHORTS |= TIMER_SHORTS_COMPARE1_CLEAR_Msk;
    
Children
Related