generated from hulk/gd32e23x_template_cmake_vscode
Rewrite the M1 command to send the drive signal after receiving the M1 command, and turn on TIMER16 at the same time. Set the measurement completion flag after 240us, and turn off TIMER16 after the maximum TOF timing.
This commit is contained in:
@@ -259,11 +259,15 @@ void handle_command(const uint8_t *frame, uint8_t len) {
|
||||
|
||||
// 启动超声波PWM发送
|
||||
ultrasonic_pwm_out_cycles();
|
||||
|
||||
// 启动TIMER16用于240us精确计时
|
||||
timer_counter_value_config(TIMER16, 0); // 复位计数器
|
||||
timer_enable(TIMER16); // 启动定时器
|
||||
|
||||
// 等待超声测量完成,最多等待1ms (100次 * 10us)
|
||||
// PWM发送约需16.7us (5周期 × 3.33us),超时时间充足
|
||||
// TIMER16将在240us时设置g_ultrasonic_measure_done = true
|
||||
uint16_t timeout_count = 0;
|
||||
const uint16_t max_timeout = 100;
|
||||
const uint16_t max_timeout = 150;
|
||||
|
||||
while (!g_ultrasonic_measure_done && timeout_count < max_timeout) {
|
||||
delay_10us(1); // 延时 10us
|
||||
@@ -277,7 +281,7 @@ void handle_command(const uint8_t *frame, uint8_t len) {
|
||||
// 测量完成,发送正常响应
|
||||
send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok));
|
||||
} else {
|
||||
// 超时,发送读取错误包(理论上不应该发生,因为PWM只需16.7us)
|
||||
// 超时,发送读取错误包(理论上不应该发生,因为TIMER16会在240us时触发)
|
||||
static const uint8_t timeout_error_data[] = { 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
send_response(RESP_TYPE_OK, timeout_error_data, sizeof(timeout_error_data));
|
||||
}
|
||||
|
Reference in New Issue
Block a user