Simplify the use of timers, timer13 sends ultrasonic waves, timer16 counts (debounce time and flight time)

The ultrasonic transducer is driven only after each command is issued.
This commit is contained in:
2025-08-25 17:05:47 +08:00
parent 16c5a31a63
commit 2d752eed5e
8 changed files with 98 additions and 136 deletions

View File

@@ -102,27 +102,18 @@ void SysTick_Handler(void) {
delay_decrement();
}
void TIMER16_IRQHandler(void) {
// 处理通道0比较中断240us时触发
if (timer_interrupt_flag_get(TIMER16, TIMER_INT_FLAG_CH0)) {
void TIMER16_IRQHandler(void)
{
// CH0比较中断 (ULTRASONIC_TX_RINGDOWN_RELOAD)
if(timer_interrupt_flag_get(TIMER16, TIMER_INT_FLAG_CH0) != RESET) {
timer_interrupt_flag_clear(TIMER16, TIMER_INT_FLAG_CH0);
// 在240us时执行的操作
g_ultrasonic_measure_done = true; // 设置测量完成标志
// TODO: 在这里可以执行其他240us时需要的操作
// 例如启动其他定时器、设置GPIO、记录时间戳等
exti_interrupt_enable(US_RX_GPIO_EXTI);
}
// 处理重装载中断1000us时触发即超时
if (timer_interrupt_flag_get(TIMER16, TIMER_INT_FLAG_UP)) {
// UP更新中断 (ULTRASONIC_MAX_TOF_RELOAD)
if(timer_interrupt_flag_get(TIMER16, TIMER_INT_FLAG_UP) != RESET) {
timer_interrupt_flag_clear(TIMER16, TIMER_INT_FLAG_UP);
// 达到最大时间1ms自动关闭定时器
timer_disable(TIMER16);
// TODO: 超时处理逻辑
// 如果需要,可以在这里设置超时标志或执行其他清理操作
timer_disable(US_ECHO_TIMER);
}
}
@@ -136,14 +127,12 @@ void EXTI0_1_IRQHandler(void) {
if (exti_interrupt_flag_get(US_RX_GPIO_EXTI) == SET)
{
exti_interrupt_flag_clear(US_RX_GPIO_EXTI);
// g_capture_value = timer_channel_capture_value_register_read(US_ECHO_TIMER, US_ECHO_CH);
timer_disable(US_ECHO_TIMER);
g_ultrasonic_measure_done = true; // 超声转换完成,置位flag后有命令处理部分回包
exti_interrupt_disable(US_RX_GPIO_EXTI);
}
}
/* PWM周期计数器 - 使用ULTRASONIC_TX_CYCLES宏 */
volatile uint8_t pwm_cycle_count = 0;
@@ -156,7 +145,6 @@ void TIMER13_IRQHandler(void)
if(pwm_cycle_count >= (ULTRASONIC_TX_CYCLES)) {
timer_disable(TIMER13);
pwm_cycle_count = 0;
// g_ultrasonic_measure_done = true; // 注释掉现在由TIMER16在240us时设置
}
}