problem: 如果想用计时器定时微秒级,不要使能自动重载:代码如下:
static uint16_t counter; void Delay_us(uint32_t us){ counter=0xffff-us-5; __HAL_TIM_SetCounter(&htim14,counter); HAL_TIM_Base_Start(&htim14); while(counter<0xffff-6){ counter=__HAL_TIM_GetCounter(&htim14); } HAL_TIM_Base_Stop(&htim14); }
否则会出现不能退出的异常
fix code
static uint16_t counter; void Delay_us(uint32_t us){//max time 1500us(1.5ms) counter=0xffff-us-99; __HAL_TIM_SetCounter(&htim14,counter); HAL_TIM_Base_Start(&htim14); while(counter<0xffff-100&&counter>0xffff-1500){ counter=__HAL_TIM_GetCounter(&htim14); } HAL_TIM_Base_Stop(&htim14); }