How to start the timer in counter mode in nrf52 and how to count the number of pulses received in a pin ?
Thank you
How to start the timer in counter mode in nrf52 and how to count the number of pulses received in a pin ?
Thank you
You should look into the PPI.
Here is an example, which uses the PPI. It uses a timer to toggle a GPIO, but you can change the GPIO to "event" instead of "task", and use the timer in counter mode. The timer can be configured something like this:
void timer_init()
{
NRF_TIMER3->BITMODE = TIMER_BITMODE_BITMODE_24Bit << TIMER_BITMODE_BITMODE_Pos;
NRF_TIMER3->PRESCALER = 0;
NRF_TIMER3->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk << TIMER_RELOAD_CC_NUM;
NRF_TIMER3->MODE = TIMER_MODE_MODE_Counter << TIMER_MODE_MODE_Pos;
NRF_TIMER3->CC[TIMER_RELOAD_CC_NUM] = TIMER_RELOAD;
}
Then you need to start the timer with:
NRF3_TIMER3->TASKS_START = 1;
Then configure the GPIO like in the example, and set a PPI channel something like the following:
NRF_PPI->CH[PPI_CH_1].EEP = (uint32_t)&NRF_GPIOTE->EVENTS_IN[GPIOTE_CH];
NRF_PPI->CH[PPI_CH_1].TEP = (uint32_t)&NRF_TIMER3->TASKS_COUNT;
Then you can get the count value by checking the timer value:
NRF_TIMER3->TASKS_CAPTURE[0];
and check the value with:
uint32_t value = NRF_TIMER3->CC[0];
you can also reset the counter using:
NRF_TIMER3->TASKS_CLEAR;
I have not tested this right now, so the calls are from my memory, but it should look something like this.
Best regards,
Edvin
You should look into the PPI.
Here is an example, which uses the PPI. It uses a timer to toggle a GPIO, but you can change the GPIO to "event" instead of "task", and use the timer in counter mode. The timer can be configured something like this:
void timer_init()
{
NRF_TIMER3->BITMODE = TIMER_BITMODE_BITMODE_24Bit << TIMER_BITMODE_BITMODE_Pos;
NRF_TIMER3->PRESCALER = 0;
NRF_TIMER3->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk << TIMER_RELOAD_CC_NUM;
NRF_TIMER3->MODE = TIMER_MODE_MODE_Counter << TIMER_MODE_MODE_Pos;
NRF_TIMER3->CC[TIMER_RELOAD_CC_NUM] = TIMER_RELOAD;
}
Then you need to start the timer with:
NRF3_TIMER3->TASKS_START = 1;
Then configure the GPIO like in the example, and set a PPI channel something like the following:
NRF_PPI->CH[PPI_CH_1].EEP = (uint32_t)&NRF_GPIOTE->EVENTS_IN[GPIOTE_CH];
NRF_PPI->CH[PPI_CH_1].TEP = (uint32_t)&NRF_TIMER3->TASKS_COUNT;
Then you can get the count value by checking the timer value:
NRF_TIMER3->TASKS_CAPTURE[0];
and check the value with:
uint32_t value = NRF_TIMER3->CC[0];
you can also reset the counter using:
NRF_TIMER3->TASKS_CLEAR;
I have not tested this right now, so the calls are from my memory, but it should look something like this.
Best regards,
Edvin