Measuring elapsed time in nanoseconds
I need to measure the time it takes for a component to reach a certain voltage, read out with the ADC. So far I have been measuring in microseconds but it turned out that that is not precise enough.
I found the following code to measure time on Stackoverflow and it works well, but I only superficially understand it and I can't really modify it.
// setup
__HAL_RCC_TIM2_CLK_ENABLE();
TIM2->PSC = HAL_RCC_GetPCLK1Freq()/1000000 - 1;
TIM2->CR1 = TIM_CR1_CEN;
// read time in microseconds
time_start = TIM2->CNT;
Can someone explain to me how it works and if I can modify it to measure nanoseconds? There might a super obvious way to do it but, again, I don't understand the code (This is what I get for copying code from the internet I guess)
Is there maybe a better solution than this?
Some additional info:
I don't need the measurement to be super accurate, whatever the chip itself can provide is probably enough for my needs.
I've set up the ADC in continuous conversion mode and DMA circular which I think is the fastest it can measure but if there is an even faster method, please let me know.
