This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Create accurate microsecond timer without interrupts

Hey guys,

I want to create an accurate timer without interrupts. My timer has to be very accurate because I have to send HIGH and LOW signals for 1us. I have read this post to know that I need high-frequency clock source (HFCLK, 16 MHz), which means better resolution (62.5 ns) and higher power consumption (typ. 5 or 70 uA depending on HFCLK source). But I don't know how to program my code so I followed this post and this post. But I have still some problems with it. I post my code below.

My timer should only replace the unaccurate nrf_delay_us.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdint.h>
#include "nrf_delay.h"
#include "app_error.h"
#include "nrf_gpio.h"
#include "nrf_drv_timer.h"
#define OUTPUT_PIN 18
/** @brief Function for Timer 1 initialization.
*/
void TIMEOUT_TIMER1_Init(void)
{
NRF_TIMER1->TASKS_STOP = 1; /**< Stop Timer 1 */
NRF_TIMER1->TASKS_CLEAR = 1; /**< Clear Timer 1 */
NRF_TIMER1->MODE = 0; /**< Mode 0: Timer */
NRF_TIMER1->BITMODE = 3; /**< Bitmode 3: 32 bit (for what?, What happens if I choose 16 bit?) */
NRF_TIMER1->PRESCALER = 4; /**< Prescaler 4: 1us Timer -> f(TIMER) = 16MHz / (2^PRESCALER) = 1MHz -> t(TIMER) = 1us */
NRF_TIMER1->INTENCLR = TIMER_INTENCLR_COMPARE0_Disabled
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

In line 19: (Init) Why should I use 32 bits and not 16 bits? where is the difference?

In lines 35 - 37: (time function) I hope you can help me here. I don't know how I can hand over my timeout value to my function and to my timer, so that the timer can work with it...

I hope, you can help me.

Thanks in advance,

Christoph