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

Setting TIMER2 interval

How do I set TIMER2 to interrupt every 1 ms?

Sounds like a very basic question, but I haven't succeed. For TIMER1 I can get it working, but for TIMER2 the highest frequecy I have achieved is about 100 Hz.

The code is as follows:

void init_timer2(void)
{
	// Start 32 MHz crystal oscillator
	NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
	NRF_CLOCK->TASKS_HFCLKSTART = 1;
	// Wait for the external oscillator to start up
	while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {
		// Do nothing.
	}
	NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer;
	NRF_TIMER2->PRESCALER = 4; // what should be here?
	NVIC_EnableIRQ(TIMER2_IRQn);
        NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_16Bit;
        NRF_TIMER2->CC[0] = ???;
        NRF_TIMER2->CC[1] = ???;
        NRF_TIMER2->CC[2] = ???;
        NRF_TIMER2->CC[3] = ???; 
	NRF_TIMER2->INTENSET = TIMER_INTENSET_COMPARE2_Enabled
	<< TIMER_INTENSET_COMPARE2_Pos;
	sd_softdevice_forward_to_application();
	NRF_TIMER2->TASKS_START = 1;
}

I don't know what should be put into the place of '???'s above. I guess there is a good documentation somewhere, so I would be happy to get a pointer there, too. Indeed, a working code would be best.

Edit: Format (please use preformatted text button when inserting code)

  • Hello, I am struggling with timers in general, I so happen to use TIMER2 as well with my BLE UART app all I need is a way to generate 1sec and in another similar app 2 ms, 2 versions of HEX, how can I generate this? I seem to be calculating it wrong according to my scope output, this example was not anywhere near 1 ms and neither is the math in the comments.

Related