Hey Guys,
Who can tell me what are the differences between the timer0, timer1 and timer2? And if I want to set up timer interrupt in timer mode, the interrupt will be triggered every 5s, how can I set up the timer?
Best regards,
Billy Hu
Hey Guys,
Who can tell me what are the differences between the timer0, timer1 and timer2? And if I want to set up timer interrupt in timer mode, the interrupt will be triggered every 5s, how can I set up the timer?
Best regards,
Billy Hu
Hi Billy,
There is no difference between the timers, they all perform the same. However TIMER0 is reserved by the SoftDevice, so it is not available for you to use.
The following code will set up an interrupt on compare start and compare end events, adjust the value of NRF_TIMER2->CC[0] for a longer time.
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
nrf_gpio_pin_set(GPIO_TOGGLE_PIN); //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
nrf_gpio_pin_clear(GPIO_TOGGLE_PIN); //Clear LED
}
}
Note that for a slow timer like 5s you might be better off using the real time clock, which will save you energy.
Best regards,
Øyvind
Øyvind, Timer 1&2 which are left over from SoftDevice can work with 8/16 bits, right?
Yes, see Table 146: BITMODE in the product specification.
Hello Øyvind,
Now I want to achieve this effect that the mcu in system off mode, when I have pressed a button more than 3s and the mcu will wake up and come to system on mode? Do you have any idea about this?
Best regards,
Billy Hu
From the nRF51 reference manual:
The system can be woken up from System OFF mode either from the DETECT signal (when active)
generated by the GPIO peripheral, by the ANADETECT signal (when active) generated by the LPCOMP
module, or from a reset. When the system wakes up from OFF mode, a system reset is performed.
This means that you can either use the GPIO to wakeup and then utilize timers to count to 3 s in your application, and then go back OFF mode if you did not hold the button down for long enough.
Or you can use the LPCOMP along with an external capacitor that you charge until it reaches a given voltage and then have ANADETECT trigger on that voltage.