Hi,
I wanted to create fixed rate waveform. I first used nrf_delay_us with nrf_gpio_pin_toggle. But I realized it is not accurate enough to do it, especially when the device is advertising and bluetooth pairing happens.
So I changed it to use nrf_drv_timer. Below is how I'm doing it (main app is using APP_TIMER).
const nrf_drv_timer_t HALF_TIMER = NRF_DRV_TIMER_INSTANCE(1);
void timer_event_handler(nrf_timer_event_t event_type, void* p_context)
{
switch(event_type)
{
case NRF_TIMER_EVENT_COMPARE1:
nrf_gpio_pin_toggle(LEFT_PIN);
nrf_gpio_pin_toggle(RIGHT_PIN);
break;
default:
//Do nothing.
break;
}
}
void TimerInit ()
{
nrf_timer_frequency_set(HALF_TIMER.p_reg, NRF_TIMER_FREQ_1MHz);
nrf_drv_timer_init(&HALF_TIMER, NULL, timer_event_handler);
uint32_t time_ticks = nrf_drv_timer_us_to_ticks(&HALF_TIMER, HALF_CLOCK); //HALF_CLOCK is 200
nrf_drv_timer_extended_compare(
&HALF_TIMER, NRF_TIMER_CC_CHANNEL1, time_ticks, NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK, true);
nrf_drv_timer_enable(&HALF_TIMER);
}
But still it doesn't work accurately. Result is like below.
Like the capture shows, sometimes longer wave happens (I didn't capture it but shorter wave also happens). How can I fix it? Can anyone suggest me how to avoid such behavior?
Thanks, Brian