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

NRF52840 used Timer2 to detect count

I used 

condition:

const nrf_drv_timer_t m_timer2 = NRF_DRV_TIMER_INSTANCE(2);

nrf_drv_timer_config_t timer_config = {.frequency = NRF_TIMER_FREQ_1MHz,
.mode = NRF_TIMER_MODE_TIMER,
.bit_width = NRF_TIMER_BIT_WIDTH_16,
.interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY,
.p_context = NULL};

err_code = nrf_drv_timer_init(&m_timer2, &timer_config, NULL);

void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
int Counter;
switch(pin)
{
case 31:
Timer_Counter++;
if(Timer_Counter == 1)
{
nrf_drv_timer_enable(&m_timer2);
if(Enter_Flag == 1)
{
read_sensor_data();
}
}
else if(Timer_Counter == 2)
{
nrfx_timer_pause(&m_timer2);
Counter = (int)nrf_drv_timer_capture(&m_timer2,NRF_TIMER_CC_CHANNEL2);
NRF_LOG_INFO("Current cout: %d \n\r", Counter);
nrf_drv_timer_disable(&m_timer2);
nrf_drv_timer_clear(&m_timer2);
Timer_Counter = 0;
}
else
{
Timer_Counter = 0;
}
break;
default:
break;
}
}


But the Counter data distortion.

<info> app: Current cout: 5001
<info> app: Current cout: 5000
<info> app: Current cout: 4999
<info> app: Current cout: 4999
<info> app: Current cout: 5014   <==
distortion
<info> app: Current cout: 4999
<info> app: Current cout: 5000
<info> app: Current cout: 5000

Parents Reply Children
No Data
Related