2023-08-02 03:20 AM - edited 2023-08-02 03:27 AM
2023-08-02 06:48 AM
void DELAY_TIM_Us(TIM_HandleTypeDef *htim, uint16_t time)
{
__HAL_TIM_SET_COUNTER(htim,0);
while(__HAL_TIM_GET_COUNTER(htim)<time){}
}
Not the greatest scheme, but it should work ± overhead. Why do you think it's wrong?
2023-08-02 06:55 AM
How wrong? Perhaps express in units of time or frequency..
Perhaps your expectations or premise is wrong? Is the MCU actually running at the frequency in question. Could you print out the frequency and bus clocks to see what the MCU thinks its running at. Check HSE_VALUE matches what's clocking the MCU.
A TIM clocked at 1 MHz should be able to resolve within a micro-second or so. A faster clock will have finer granularity. A slower one will obviously be rougher. The MCU can't interrupt at excessively high rates, figure a few hundred KHz in this case, shrinks the more you do in the handler/callback
2023-08-02 03:12 PM - edited 2023-08-02 03:15 PM
when I use function "DS18B20_DelayUs(&DS1, 1); // delay 1 us ". result in logic analyzer = 3.875us.
Do you know why is that?
2023-08-02 03:14 PM
when I use function "DS18B20_DelayUs(&DS1, 1); // delay 1 us ". result in logic analyzer = 3.875us.
Do you know why is that?
2023-08-02 03:55 PM - edited 2023-08-02 03:58 PM
That is due to overhead and jitter in your timer. Calling functions takes times, returning from functions takes time, your timer has increment of 1us, so expect a jitter of 1us on top of the overhead.
You can get more accurate by using DWT->CYCCNT, reducing the function calls to a minimum, and compiling your code with optimizations on, but you will always incur some amount of overhead.
Do a 1s delay (or 1ms) and see if the actual delay is close to expected. Overhead will be minimal in that example so it will tell you if the timer is running at the expected rate.
2023-08-04 07:23 AM
https://controllerstech.com/create-1-microsecond-delay-stm32/
void delay_us (uint16_t us)
{
__HAL_TIM_SET_COUNTER(&htim1,0); // set the counter value a 0
while (__HAL_TIM_GET_COUNTER(&htim1) < us); // wait for the counter to reach the us input in the parameter
}
The code can easily be converted to be based on a free-running timer and the delay function would become reentrant. This just proves again that the ControllerTech site is made by a complete beginner without any real understanding of the software development.