Hello Everyone,
Maybe this is a very silly question. I am trying to implement timer in nrf51822 with S130, SDK11.
Code snippet:
void TIMER1_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
// do the required stuff when interrupt is fired
}
}
void start_timer(void)
{
NRF_TIMER1->MODE = TIMER_MODE_MODE_Timer;
NRF_TIMER1->TASKS_CLEAR = 1;
// set prescalar n
// f = 16 MHz / 2^(n)
uint8_t prescaler = 0;
NRF_TIMER1->PRESCALER = prescaler;
NRF_TIMER1->BITMODE = TIMER_BITMODE_BITMODE_32Bit;
// 16 MHz clock generates timer tick every 1/(16000000) s = 62.5 nano s
// With compare enabled, the interrupt is fired every: 62.5 * comp1 nano s
// = 0.0625*comp1 micro seconds
// multiply this by 2^(prescalar)
uint16_t comp1 = 10000;
// set compare
NRF_TIMER1->CC[1] = comp1;
countToUs = 0.0625 * comp1 * (1 << prescaler); // time in us, set conversion factor
// (1<<prescalar) left shift 1 with prescalar
//example: (1<<0) = 1
// (1<<1) = 2
// (1<<2) = 4 and so on
// enable compare 1
NRF_TIMER1->INTENSET =
(TIMER_INTENSET_COMPARE1_Enabled << TIMER_INTENSET_COMPARE1_Pos); // INTSET- interrupt set register
// use the shorts register to clear compare 1
NRF_TIMER1->SHORTS = (TIMER_SHORTS_COMPARE1_CLEAR_Enabled <<
TIMER_SHORTS_COMPARE1_CLEAR_Pos);
NVIC_EnableIRQ(TIMER1_IRQn); // enable IRQ_handler
NRF_TIMER1->TASKS_START = 1;// start timer
}
According to calculation: For 8 seconds timer, I need to set prescalar to 8 and comp1 to 1000000.
"countToUs = 0.0625 * comp1 * (1 << prescaler); // time in us, set conversion factor" Is the calculation correct? or am I going wrong in somewhere?
Thank you.