From 3865a9ec37dd7d2991672ed479646755fb2625fc Mon Sep 17 00:00:00 2001
From: liuzhigang <15608060653@163.com>
Date: 星期四, 13 十一月 2025 11:09:49 +0800
Subject: [PATCH] 新增PWM控制,蓝牙功能

---
 STM32H743/FML/pwm_ctrol.c |  157 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 153 insertions(+), 4 deletions(-)

diff --git a/STM32H743/FML/pwm_ctrol.c b/STM32H743/FML/pwm_ctrol.c
index a28c8b3..fefdff0 100644
--- a/STM32H743/FML/pwm_ctrol.c
+++ b/STM32H743/FML/pwm_ctrol.c
@@ -2,15 +2,28 @@
 #include "stdarg.h"
 #include "string.h"
 #include "pwm_ctrol.h"
-#include "mainex.h"
 #include "bluetooth.h"
+#include "stm32h7xx_it.h"
+#include "stm32h7xx_hal.h"
+#include "main.h"
+#include "mainex.h"
+#include "DBG.h"
+#include "Uart.h"
+#include "HIDO_Util.h"
 
+#define STATE_WAIT_RISING   0
+#define STATE_WAIT_FALLING  1
+uint32_t rising_time = 0;
+uint32_t falling_time = 0;
+uint32_t pulse_width_us = 0;
+uint8_t  capture_state = STATE_WAIT_RISING;;
 
 
 
 
 // 设置所有电机到指定占空比
-void set_all_pwm(uint16_t duty) {
+void set_all_pwm(uint16_t duty)
+{
     __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, duty);
     __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, duty);
     __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, duty);
@@ -22,7 +35,8 @@
 }
 
 // 向左:前轮左转,后轮右转
-void set_pwm_left() {
+void set_pwm_left()
+{
     __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 1000); // 前左
     __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 2000); // 前右
     __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 2000); // 后左
@@ -30,9 +44,144 @@
 }
 
 // 向右:前轮右转,后轮左转
-void set_pwm_right() {
+void set_pwm_right()
+{
     __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 2000); // 前左
     __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 1000); // 前右
     __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 1000); // 后左
     __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, 2000); // 后右
 }
+
+
+// 映射函数:将 [-100,100] 映射到 [1000,2000]
+uint32_t Map(int16_t input, int16_t in_min, int16_t in_max, uint32_t out_min, uint32_t out_max)
+{
+    return (input - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
+}
+// 设置电机 PWM(前进/后退)
+void Set_Motor_PWM(int16_t speed)
+{
+    static HIDO_UINT8 l_Motor[50];
+    uint32_t pulse = Map(speed, -100, 100, 1000, 2000);  // -100~100 → 1000~2000
+    __HAL_TIM_SetCompare(&MOTOR_TIM, MOTOR_CHANNEL, pulse);
+	HIDO_UtilSnprintf((HIDO_CHAR *)l_Motor, sizeof(l_Motor), "Motor cortrol:speed=%d,pulse=%d\r\n", speed,pulse);
+    Uart_Send(UART_ID_DBG, (HIDO_UINT8 *)l_Motor, strlen(l_Motor));
+}
+
+// 设置转向 PWM(左转/右转)
+void Set_Steering_PWM(int16_t steer)
+{
+   static HIDO_UINT8 l_Steering[50];
+    uint32_t pulse = Map(steer, -100, 100, 1000, 2000);  // -100~100 → 1000~2000
+    __HAL_TIM_SetCompare(&STEERING_TIM, STEERING_CHANNEL, pulse);
+	HIDO_UtilSnprintf((HIDO_CHAR *)l_Steering, sizeof(l_Steering), "Steering cortrol:steer=%d,pulse=%d\r\n", steer,pulse);
+    Uart_Send(UART_ID_DBG, (HIDO_UINT8 *)l_Steering, strlen(l_Steering));
+	
+}
+
+void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
+{
+    static HIDO_UINT8 l_pulse_width[20];
+
+    if (htim->Instance == TIM4)
+    {
+        uint32_t current_time = htim->Instance->CCR1;
+
+        if (capture_state == STATE_WAIT_RISING)
+        {
+            // 当前是上升沿 → 记录并切换到等待下降沿
+            rising_time = current_time;
+            // 切换为下降沿触发
+            htim->Instance->CCER &= ~TIM_CCER_CC1P; // 上升沿
+            htim->Instance->CCER |= TIM_CCER_CC1NP; // 加上 NP 表示非反相?实际应使用极性控制函数
+
+            // 更推荐使用 HAL 函数设置极性
+            __HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_FALLING);
+
+            capture_state = STATE_WAIT_FALLING;
+        }
+        else if (capture_state == STATE_WAIT_FALLING)
+        {
+            // 当前是下降沿 → 计算高电平宽度
+            uint32_t pulse_width = current_time - rising_time;
+
+            if (pulse_width > 65535)  // 超过最大合理值
+            {
+                pulse_width = 0; // 或者标记为无效
+            }
+
+            //printf("High Pulse Width: %lu μs\n", pulse_width);
+            HIDO_UtilSnprintf((HIDO_CHAR *)l_pulse_width, sizeof(l_pulse_width), "pulse_width=%d\r\n", pulse_width);
+            Uart_Send(UART_ID_DBG, (HIDO_UINT8 *)l_pulse_width, strlen(l_pulse_width));
+            // 切回上升沿触发
+            __HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING);
+            capture_state = STATE_WAIT_RISING;
+        }
+
+        // 清除中断标志
+        __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC1);
+    }
+}
+
+#if 0
+/**
+  * @brief  Update Callback (for overflow protection)
+  */
+void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
+{
+    if (htim->Instance == TIM4)
+    {
+        // 只在长时间无响应时才重置状态
+        static uint32_t last_reset_ms = 0;
+
+        if (HAL_GetTick() - last_reset_ms > 100)  // 100ms 超时
+        {
+            capture_state = 0;
+            __HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING);
+            last_reset_ms = HAL_Get_Tick();
+            printf("TIM4 Overflow Reset (timeout)\r\n");
+        }
+    }
+}
+
+#endif
+#if 0
+void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
+{
+    if (htim->Instance == TIM4 && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
+    {
+        uint32_t current_value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
+
+        if (capture_state == 0)
+        {
+            // 上升沿:记录起始时间
+            rising_time = current_value;
+            printf("Rising Edge: %lu\r\n", rising_time);
+
+            // 切换为下降沿检测
+            __HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_FALLING);
+            capture_state = 1;
+        }
+        else if (capture_state == 1)
+        {
+            // 下降沿:计算脉宽
+            falling_time = current_value;
+            pulse_width_us = falling_time - rising_time;
+
+            // 防止负数(防止溢出)
+            if (pulse_width_us > 65535)  // 超过最大合理值
+            {
+                pulse_width_us = 0; // 或者标记为无效
+            }
+
+            printf("Falling Edge: %lu\r\n", falling_time);
+            printf("Pulse Width: %lu μs\r\n", pulse_width_us);
+
+            // 重新设置为上升沿,等待下一个周期
+            __HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING);
+            capture_state = 0;
+        }
+    }
+}
+#endif
+

--
Gitblit v1.9.3