2024-05-13 01:49 AM
Hello,
I have a small problem when using a custom delay function as follows :
void delay_us(uint32_t delayus) {
uint32_t start_count = __HAL_TIM_GET_COUNTER(&htim2);
while ((__HAL_TIM_GET_COUNTER(&htim2) - start_count) < delayus) {
}
}
The attached image shows the configuration of timer 2.
This delay function should allow me to generate short pulses. except that sometimes it does and sometimes nothing happens when I use it. I don't know what the problem is. Could you please help me?
Solved! Go to Solution.
2024-05-13 06:00 AM
again promotion to int
2024-05-13 06:06 AM
2024-05-13 06:09 AM
Promotion to int and
while (((uint32_t)(__HAL_TIM_GET_COUNTER(&htim2) - start_count)) < delayus) {
}
2024-05-13 06:11 AM - edited 2024-05-13 06:12 AM
I just tried (on F303 , TIM3 ) : works, as it should do.
+
Did you set counter period (ARR) to something big ? 0xFFFF .
+ counter mode up ?
2024-05-13 06:12 AM - edited 2024-05-13 06:14 AM
2024-05-13 06:14 AM
while (((uint32_t)(__HAL_TIM_GET_COUNTER(&htim2) - start_count)) < delayus) { }
and ARR of timer must be 0xFFFFFFFF
2024-05-13 07:08 AM
@Radosław wrote:Promotion to int and
while (((uint32_t)(__HAL_TIM_GET_COUNTER(&htim2) - start_count)) < delayus) {
}
Even with this modification it still works randomly
2024-05-13 07:10 AM
@Radosław wrote:while (((uint32_t)(__HAL_TIM_GET_COUNTER(&htim2) - start_count)) < delayus) { }
and ARR of timer must be 0xFFFFFFFF
yes timer ARR is 0xFFFFFFFF
2024-05-13 07:12 AM
Interrupt can be a reason for longer delay, or hal funtion do something