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.
#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
| TIMER_INTENCLR_COMPARE1_Disabled
| TIMER_INTENCLR_COMPARE2_Disabled
| TIMER_INTENCLR_COMPARE3_Disabled
| TIMER_INTENCLR_COMPARE4_Disabled
| TIMER_INTENCLR_COMPARE5_Disabled; /**< Disable all interrupts */
}
void TIMEROUT_TIMER1_Start(uint32_t timeout_us)
{
NRF_TIMER1->TASKS_STOP = 1;
NRF_TIMER1->TASKS_CLEAR = 1;
// this line: set TIMER1 to 0 (start value)
// this line: hand over the time "timeout_us" to TIMER1 (end time)
// this line: stop TIMER1 if end time ("timeout_us") is reached
NRF_TIMER1->TASKS_START = 1;
}
void Write_Byte(uint8_t byte)
{
int walk;
for (walk = 0; walk < 8; ++walk)
{
nrf_gpio_pin_clear(OUTPUT_PIN);
if (byte & 0x01)
{
TIMEROUT_TIMER1_Start(1); //instead of nrf_delay_us()
nrf_gpio_pin_set(OUTPUT_PIN);
TIMEROUT_TIMER1_Start(60); //instead of nrf_delay_us()
}
else
{
TIMEROUT_TIMER1_Start(60); //instead of nrf_delay_us()
nrf_gpio_pin_set(OUTPUT_PIN);
TIMEROUT_TIMER1_Start(1); //instead of nrf_delay_us()
}
byte >>= 1;
}
}
/**
* @brief Function for application main entry.
*/
int main(void)
{
uint32_t err_code;
TIMEOUT_TIMER1_Init;
uint8_t AA = 0xAA;
nrf_gpio_cfg_output(OUTPUT_PIN);
while (true)
{
Write_Byte(AA);
nrf_delay_us(20);
}
}
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


