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

@@ -9,13 +9,6 @@ volatile bool g_ultrasonic_measure_done = false;
void ultrasonic_tx_init(void) {
// TODO
rcu_periph_clock_enable(RCU_GPIOA);
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_0);
gpio_output_options_set(GPIOA, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
gpio_bit_reset(GPIOA, GPIO_PIN_0);
rcu_periph_clock_enable(US_TX_GPIO_RCU);
gpio_mode_set(US_TX_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, US_TX_GPIO_PIN);
@@ -59,23 +52,32 @@ void ultrasonic_tx_init(void) {
timer_counter_value_config(US_TX_TIMER, 0);
timer_interrupt_enable(US_TX_TIMER, TIMER_INT_UP);
nvic_irq_enable(TIMER13_IRQn, 2);
nvic_irq_enable(TIMER13_IRQn, 2U);
}
void ultrasonic_pwm_out_cycles(void) {
g_ultrasonic_measure_done = false;
// 停止发射定时器
timer_disable(TIMER13);
timer_channel_output_state_config(TIMER13, TIMER_CH_0, TIMER_CCX_ENABLE);
// 重置并启动回波计时器
timer_disable(US_ECHO_TIMER);
// 重要:清除所有中断标志
// timer_interrupt_flag_clear(US_ECHO_TIMER, TIMER_INT_FLAG_CH0);
// timer_interrupt_flag_clear(US_ECHO_TIMER, TIMER_INT_FLAG_UP);
// 重置计数器
timer_counter_value_config(US_ECHO_TIMER, 0);
// 启动发射PWM
timer_enable(US_TX_TIMER);
// TODO 启动后续回波计时器
// timer_enable(US_ECHO_TIMER);
// 启动回波计时器
timer_enable(US_ECHO_TIMER);
}
void ultrasonic_receive_exti_config(void) {
@@ -88,75 +90,51 @@ void ultrasonic_receive_exti_config(void) {
exti_init(US_RX_GPIO_EXTI, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
exti_flag_clear(US_RX_GPIO_EXTI);
// exti_interrupt_enable(EXTI_0);
}
void ultrasonic_echo_timer_config(void) {
// 使能TIMER16时钟
rcu_periph_clock_enable(US_ECHO_RCU);
// 复位定时器到默认状态
timer_deinit(US_ECHO_TIMER);
// 配置基本定时器参数
timer_parameter_struct timer_initpara;
timer_struct_para_init(&timer_initpara);
// 设置分频器72MHz ÷ 72 = 1MHz每个计数 = 1us
timer_initpara.prescaler = 71; // (72-1)实际分频比为72
timer_initpara.prescaler = 71; // 72MHz/72 = 1MHz (1us per count)
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
// 设置周期999 (0-999共1000个计数) = 1000us = 1ms最大计时
timer_initpara.period = 999; // 1000us重装载周期
timer_initpara.period = (ULTRASONIC_MAX_TOF_RELOAD - 1);
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
// 初始化定时器基本参数
timer_init(US_ECHO_TIMER, &timer_initpara);
// 配置通道0用于240us比较中断不是PWM输出仅用于比较
timer_auto_reload_shadow_disable(US_ECHO_TIMER);
// 配置输出比较通道0
timer_oc_parameter_struct timer_oc_initpara;
timer_channel_output_struct_para_init(&timer_oc_initpara);
// 禁用输出,仅用于内部比较中断
timer_oc_initpara.outputstate = TIMER_CCX_DISABLE;
timer_oc_initpara.outputstate = TIMER_CCX_ENABLE;
timer_oc_initpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_channel_output_config(US_ECHO_TIMER, US_ECHO_CH, &timer_oc_initpara);
timer_channel_output_config(US_ECHO_TIMER, TIMER_CH_0, &timer_oc_initpara);
// 设置比较模式为时间比较不是PWM模式
timer_channel_output_mode_config(US_ECHO_TIMER, US_ECHO_CH, TIMER_OC_MODE_TIMING);
// 配置输出比较模式
timer_channel_output_mode_config(US_ECHO_TIMER, TIMER_CH_0, TIMER_OC_MODE_TIMING);
// 设置240us时触发比较中断计数值239因为从0开始计数
timer_channel_output_pulse_value_config(US_ECHO_TIMER, US_ECHO_CH, 239);
// 设置比较值
timer_channel_output_pulse_value_config(US_ECHO_TIMER, TIMER_CH_0, (ULTRASONIC_TX_RINGDOWN_RELOAD - 1));
// 启用比较中断CH0和重装载中断UP
timer_interrupt_enable(US_ECHO_TIMER, TIMER_INT_CH0); // 240us比较中断
timer_interrupt_enable(US_ECHO_TIMER, TIMER_INT_UP); // 1000us重装载中断
// 清除中断标志
timer_interrupt_flag_clear(US_ECHO_TIMER, TIMER_INT_FLAG_CH0);
timer_interrupt_flag_clear(US_ECHO_TIMER, TIMER_INT_FLAG_UP);
// 使能中断
timer_interrupt_enable(US_ECHO_TIMER, TIMER_INT_UP); // UP中断
timer_interrupt_enable(US_ECHO_TIMER, TIMER_INT_CH0); // CH0中断
// 配置NVIC中断优先级设置为较高优先级1确保及时响应
nvic_irq_enable(TIMER16_IRQn, 1);
nvic_irq_enable(US_ECHO_TIMER_IRQ, 1U);
// 注意定时器配置完成但不启动需要在其他地方调用timer_enable()启动
// timer_single_pulse_mode_config(US_ECHO_TIMER, TIMER_SP_MODE_SINGLE);
}
void ultrasonic_config(void) {
ultrasonic_tx_init();
// ultrasonic_transmit_debounce_delay(TIME_CORRECTION_US);
// ultrasonic_receive_exti_config();
ultrasonic_receive_exti_config();
ultrasonic_echo_timer_config();
}
// uint16_t ultrasonic_calc_distance(void) {
// // while (!ultrasonicMeasurementDone);
// // uint32_t us_value = timer_channel_capture_value_register_read(US_ECHO_TIMER, US_ECHO_CH);
// uint16_t distance = (TIME_CORRECTION_US + g_capture_value) * 17;
// /*
// * (TIME_CORRECTION_US + us_value) * 340 m/s
// * -----------------------------------------
// * 1000 000
// * ----------------------------------------------
// * 2
// */
// return distance;
// }