diff --git a/HARDWARE/MOTOR/motor_driver.c b/HARDWARE/MOTOR/motor_driver.c index 4ef8195..2d7bf42 100644 --- a/HARDWARE/MOTOR/motor_driver.c +++ b/HARDWARE/MOTOR/motor_driver.c @@ -2,33 +2,99 @@ #include "motor_driver.h" #include "delay.h" // 全局变量 -static uint8_t pwm_duty = 0; // PWM占空比 0-100 -static MotorState motor_dir = MOTOR_STOP; - +static uint8_t L_pwm_duty = 0; // PWM占空比 0-100 +static uint8_t M_pwm_duty = 0; // PWM占空比 0-100 +static uint8_t R_pwm_duty = 0; // PWM占空比 0-100 +static MotorState L_motor_dir = MOTOR_STOP; +static MotorState M_motor_dir = MOTOR_STOP; +static MotorState R_motor_dir = MOTOR_STOP; // GPIO初始化 void DRV8832_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; + //左电机 引脚初始化 // 使能GPIOE时钟 - RCC_AHB1PeriphClockCmd(MOTOR_IN1_GPIO_CLK, ENABLE); + RCC_AHB1PeriphClockCmd(L_MOTOR_IN1_GPIO_CLK, ENABLE); // 配置IN1 (PE9) 和 IN2 (PE13) 为输出 - GPIO_InitStructure.GPIO_Pin = MOTOR_IN1_GPIO_PIN | MOTOR_IN2_GPIO_PIN; + GPIO_InitStructure.GPIO_Pin = L_MOTOR_IN1_GPIO_PIN | L_MOTOR_IN2_GPIO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_Init(MOTOR_IN1_GPIO_PORT, &GPIO_InitStructure); + GPIO_Init(L_MOTOR_IN1_GPIO_PORT, &GPIO_InitStructure); // 配置FAULTN (PE4) 为输入 - GPIO_InitStructure.GPIO_Pin = MOTOR_FAULT_GPIO_PIN; + GPIO_InitStructure.GPIO_Pin = L_MOTOR_FAULT_GPIO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上拉 - GPIO_Init(MOTOR_FAULT_GPIO_PORT, &GPIO_InitStructure); + GPIO_Init(L_MOTOR_FAULT_GPIO_PORT, &GPIO_InitStructure); // 初始状态:停止电机 - GPIO_ResetBits(MOTOR_IN1_GPIO_PORT, MOTOR_IN1_GPIO_PIN); - GPIO_ResetBits(MOTOR_IN2_GPIO_PORT, MOTOR_IN2_GPIO_PIN); + GPIO_ResetBits(L_MOTOR_IN1_GPIO_PORT, L_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(L_MOTOR_IN2_GPIO_PORT, L_MOTOR_IN2_GPIO_PIN); + + //=========================================================================== + //中电机 引脚初始化 + // 使能GPIOE时钟 + RCC_AHB1PeriphClockCmd(M_MOTOR_IN1_GPIO_CLK, ENABLE); + // 配置IN1 (PB3) + GPIO_InitStructure.GPIO_Pin = M_MOTOR_IN1_GPIO_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 推挽输出 + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(M_MOTOR_IN1_GPIO_PORT, &GPIO_InitStructure); + + // 配置 IN2 (PE14) 为输出 + GPIO_InitStructure.GPIO_Pin = M_MOTOR_IN2_GPIO_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 推挽输出 + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(M_MOTOR_IN2_GPIO_PORT, &GPIO_InitStructure); + + // 配置FAULTN (PE5) 为输入 + GPIO_InitStructure.GPIO_Pin = M_MOTOR_FAULT_GPIO_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上拉 + GPIO_Init(M_MOTOR_FAULT_GPIO_PORT, &GPIO_InitStructure); + + // 初始状态:停止电机 + GPIO_ResetBits(M_MOTOR_IN1_GPIO_PORT, M_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(M_MOTOR_IN2_GPIO_PORT, M_MOTOR_IN2_GPIO_PIN); + //=========================================================================== + //右电机 引脚初始化 + // 使能GPIOE时钟 + RCC_AHB1PeriphClockCmd(R_MOTOR_IN1_GPIO_CLK, ENABLE); + // 配置IN1 (PD15) + GPIO_InitStructure.GPIO_Pin = R_MOTOR_IN1_GPIO_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 推挽输出 + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(R_MOTOR_IN1_GPIO_PORT, &GPIO_InitStructure); + + // 配置 IN2 (PE15) 为输出 + GPIO_InitStructure.GPIO_Pin = R_MOTOR_IN2_GPIO_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 推挽输出 + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(R_MOTOR_IN2_GPIO_PORT, &GPIO_InitStructure); + + // 配置FAULTN (PE6) 为输入 + GPIO_InitStructure.GPIO_Pin = R_MOTOR_FAULT_GPIO_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上拉 + GPIO_Init(R_MOTOR_FAULT_GPIO_PORT, &GPIO_InitStructure); + + // 初始状态:停止电机 + GPIO_ResetBits(R_MOTOR_IN1_GPIO_PORT, R_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(R_MOTOR_IN2_GPIO_PORT, R_MOTOR_IN2_GPIO_PIN); + + + } // 初始化DRV8832 @@ -36,58 +102,16 @@ void DRV8832_Init(void) { DRV8832_GPIO_Init(); } -//===================================================================================== -// 电机基础控制函数 -void Motor_Control(MotorState state) { - switch(state) { - case MOTOR_STOP: // IN1=0, IN2=0 - GPIO_ResetBits(MOTOR_IN1_GPIO_PORT, MOTOR_IN1_GPIO_PIN); - GPIO_ResetBits(MOTOR_IN2_GPIO_PORT, MOTOR_IN2_GPIO_PIN); - break; - - case MOTOR_FORWARD: // IN1=1, IN2=0 - GPIO_SetBits(MOTOR_IN1_GPIO_PORT, MOTOR_IN1_GPIO_PIN); - GPIO_ResetBits(MOTOR_IN2_GPIO_PORT, MOTOR_IN2_GPIO_PIN); - break; - - case MOTOR_REVERSE: // IN1=0, IN2=1 - GPIO_ResetBits(MOTOR_IN1_GPIO_PORT, MOTOR_IN1_GPIO_PIN); - GPIO_SetBits(MOTOR_IN2_GPIO_PORT, MOTOR_IN2_GPIO_PIN); - break; - - case MOTOR_BRAKE: // IN1=1, IN2=1 - GPIO_SetBits(MOTOR_IN1_GPIO_PORT, MOTOR_IN1_GPIO_PIN); - GPIO_SetBits(MOTOR_IN2_GPIO_PORT, MOTOR_IN2_GPIO_PIN); - break; - } -} -// 获取故障状态 -uint8_t Motor_GetFaultStatus(void) { - // FAULTN引脚为低电平时表示故障 - if (GPIO_ReadInputDataBit(MOTOR_FAULT_GPIO_PORT, MOTOR_FAULT_GPIO_PIN) == Bit_RESET) { - return 1; // 故障 - } - return 0; // 正常 -} - -// 安全的方向切换(避免电流冲击) -void Motor_SafeDirectionChange(MotorState new_direction) { - // 先刹车 - Motor_Control(MOTOR_BRAKE); - delay_ms(5); // 5ms延迟 - - // 再切换到新方向 - Motor_Control(new_direction); -} // 使用TIM2实现软件PWM #define PWM_FREQUENCY 20000 // 20kHz PWM频率 #define PWM_RESOLUTION 100 // 100级调速分辨率 -static uint8_t pwm_counter = 0; // PWM计数器 - +static uint8_t L_pwm_counter = 0; // PWM计数器 +static uint8_t M_pwm_counter = 0; // PWM计数器 +static uint8_t R_pwm_counter = 0; // PWM计数器 // 定时器初始化 void PWM_Timer_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; @@ -118,65 +142,337 @@ void PWM_Timer_Init(void) { } // 定时器中断处理函数 -void TIM2_IRQHandler(void) { - if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { +void TIM2_IRQHandler(void) +{ + if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) + { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); - - pwm_counter++; - if (pwm_counter >= PWM_RESOLUTION) { - pwm_counter = 0; + + + + { //左电机运行 + L_pwm_counter++; + if (L_pwm_counter >= PWM_RESOLUTION) + { + L_pwm_counter = 0; } // 根据占空比和方向控制电机 - if (motor_dir == MOTOR_FORWARD) { - if (pwm_counter < pwm_duty) { + if (L_motor_dir == MOTOR_FORWARD) { + if (L_pwm_counter < L_pwm_duty) { // 正转:IN1=1, IN2=0 - GPIO_SetBits(MOTOR_IN1_GPIO_PORT, MOTOR_IN1_GPIO_PIN); - GPIO_ResetBits(MOTOR_IN2_GPIO_PORT, MOTOR_IN2_GPIO_PIN); + GPIO_SetBits(L_MOTOR_IN1_GPIO_PORT, L_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(L_MOTOR_IN2_GPIO_PORT, L_MOTOR_IN2_GPIO_PIN); } else { // 停止(滑行) - GPIO_ResetBits(MOTOR_IN1_GPIO_PORT, MOTOR_IN1_GPIO_PIN); - GPIO_ResetBits(MOTOR_IN2_GPIO_PORT, MOTOR_IN2_GPIO_PIN); + GPIO_ResetBits(L_MOTOR_IN1_GPIO_PORT, L_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(L_MOTOR_IN2_GPIO_PORT, L_MOTOR_IN2_GPIO_PIN); } } - else if (motor_dir == MOTOR_REVERSE) { - if (pwm_counter < pwm_duty) { + else if (L_motor_dir == MOTOR_REVERSE) { + if (L_pwm_counter < L_pwm_duty) { // 反转:IN1=0, IN2=1 - GPIO_ResetBits(MOTOR_IN1_GPIO_PORT, MOTOR_IN1_GPIO_PIN); - GPIO_SetBits(MOTOR_IN2_GPIO_PORT, MOTOR_IN2_GPIO_PIN); + GPIO_ResetBits(L_MOTOR_IN1_GPIO_PORT, L_MOTOR_IN1_GPIO_PIN); + GPIO_SetBits(L_MOTOR_IN2_GPIO_PORT, L_MOTOR_IN2_GPIO_PIN); } else { // 停止(滑行) - GPIO_ResetBits(MOTOR_IN1_GPIO_PORT, MOTOR_IN1_GPIO_PIN); - GPIO_ResetBits(MOTOR_IN2_GPIO_PORT, MOTOR_IN2_GPIO_PIN); + GPIO_ResetBits(L_MOTOR_IN1_GPIO_PORT, L_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(L_MOTOR_IN2_GPIO_PORT, L_MOTOR_IN2_GPIO_PIN); } } else { // 停止状态 - GPIO_ResetBits(MOTOR_IN1_GPIO_PORT, MOTOR_IN1_GPIO_PIN); - GPIO_ResetBits(MOTOR_IN2_GPIO_PORT, MOTOR_IN2_GPIO_PIN); + GPIO_ResetBits(L_MOTOR_IN1_GPIO_PORT, L_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(L_MOTOR_IN2_GPIO_PORT, L_MOTOR_IN2_GPIO_PIN); } + } + //-------------------------------------------------------------------------------- + { //中电机运行 + M_pwm_counter++; + if (M_pwm_counter >= PWM_RESOLUTION) + { + M_pwm_counter = 0; + } + + // 根据占空比和方向控制电机 + if (M_motor_dir == MOTOR_FORWARD) { + if (M_pwm_counter < M_pwm_duty) { + // 正转:IN1=1, IN2=0 + GPIO_SetBits(M_MOTOR_IN1_GPIO_PORT, M_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(M_MOTOR_IN2_GPIO_PORT, M_MOTOR_IN2_GPIO_PIN); + } else { + // 停止(滑行) + GPIO_ResetBits(M_MOTOR_IN1_GPIO_PORT, M_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(M_MOTOR_IN2_GPIO_PORT, M_MOTOR_IN2_GPIO_PIN); + } + } + else if (M_motor_dir == MOTOR_REVERSE) { + if (M_pwm_counter < M_pwm_duty) { + // 反转:IN1=0, IN2=1 + GPIO_ResetBits(M_MOTOR_IN1_GPIO_PORT, M_MOTOR_IN1_GPIO_PIN); + GPIO_SetBits(M_MOTOR_IN2_GPIO_PORT, M_MOTOR_IN2_GPIO_PIN); + } else { + // 停止(滑行) + GPIO_ResetBits(M_MOTOR_IN1_GPIO_PORT, M_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(M_MOTOR_IN2_GPIO_PORT, M_MOTOR_IN2_GPIO_PIN); + } + } + else { + // 停止状态 + GPIO_ResetBits(M_MOTOR_IN1_GPIO_PORT, M_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(M_MOTOR_IN2_GPIO_PORT, M_MOTOR_IN2_GPIO_PIN); + } + } + + //-------------------------------------------------------------------------------- + { //右电机运行 + R_pwm_counter++; + if (R_pwm_counter >= PWM_RESOLUTION) + { + R_pwm_counter = 0; + } + + // 根据占空比和方向控制电机 + if (R_motor_dir == MOTOR_FORWARD) + { + if (R_pwm_counter < R_pwm_duty) { + // 正转:IN1=1, IN2=0 + GPIO_SetBits(R_MOTOR_IN1_GPIO_PORT, R_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(R_MOTOR_IN2_GPIO_PORT, R_MOTOR_IN2_GPIO_PIN); + } else { + // 停止(滑行) + GPIO_ResetBits(R_MOTOR_IN1_GPIO_PORT, R_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(R_MOTOR_IN2_GPIO_PORT, R_MOTOR_IN2_GPIO_PIN); + } + } + else if (R_motor_dir == MOTOR_REVERSE) + { + if (R_pwm_counter < R_pwm_duty) { + // 反转:IN1=0, IN2=1 + GPIO_ResetBits(R_MOTOR_IN1_GPIO_PORT, R_MOTOR_IN1_GPIO_PIN); + GPIO_SetBits(R_MOTOR_IN2_GPIO_PORT, R_MOTOR_IN2_GPIO_PIN); + } else { + // 停止(滑行) + GPIO_ResetBits(R_MOTOR_IN1_GPIO_PORT, R_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(R_MOTOR_IN2_GPIO_PORT, R_MOTOR_IN2_GPIO_PIN); + } + } + else { + // 停止状态 + GPIO_ResetBits(R_MOTOR_IN1_GPIO_PORT, R_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(R_MOTOR_IN2_GPIO_PORT, R_MOTOR_IN2_GPIO_PIN); + } + } + //--------------------------------------------------------------------------------- + } } -// 设置电机速度和方向 -void Motor_SetSpeed(uint8_t speed, MotorState direction) { +//=========电机速度和方向设置================================================ +// 设置左电机速度和方向 +void L_Motor_SetSpeed(uint8_t speed, MotorState direction) +{ // 限制速度范围 if (speed > 100) speed = 100; // 如果速度为0或100%,直接使用基础控制 if (speed == 0) { - motor_dir = MOTOR_STOP; - pwm_duty = 0; - Motor_Control(MOTOR_STOP); + L_motor_dir = MOTOR_STOP; + L_pwm_duty = 0; + L_Motor_Control(MOTOR_STOP); } else if (speed == 100) { - motor_dir = direction; - pwm_duty = 100; - Motor_Control(direction); + L_motor_dir = direction; + L_pwm_duty = 100; + L_Motor_Control(direction); } else { // 设置PWM参数 - motor_dir = direction; - pwm_duty = speed; // 直接使用百分比 + L_motor_dir = direction; + L_pwm_duty = speed; // 直接使用百分比 } } + +// 设置中电机速度和方向 +void M_Motor_SetSpeed(uint8_t speed, MotorState direction) +{ + // 限制速度范围 + if (speed > 100) speed = 100; + + // 如果速度为0或100%,直接使用基础控制 + if (speed == 0) { + M_motor_dir = MOTOR_STOP; + M_pwm_duty = 0; + M_Motor_Control(MOTOR_STOP); + } + else if (speed == 100) { + M_motor_dir = direction; + M_pwm_duty = 100; + M_Motor_Control(direction); + } + else { + // 设置PWM参数 + M_motor_dir = direction; + M_pwm_duty = speed; // 直接使用百分比 + } +} + +// 设置右电机速度和方向 +void R_Motor_SetSpeed(uint8_t speed, MotorState direction) +{ + // 限制速度范围 + if (speed > 100) speed = 100; + + // 如果速度为0或100%,直接使用基础控制 + if (speed == 0) { + R_motor_dir = MOTOR_STOP; + R_pwm_duty = 0; + R_Motor_Control(MOTOR_STOP); + } + else if (speed == 100) { + R_motor_dir = direction; + R_pwm_duty = 100; + R_Motor_Control(direction); + } + else { + // 设置PWM参数 + R_motor_dir = direction; + R_pwm_duty = speed; // 直接使用百分比 + } +} +//=====================电机直接控制================================================================ +// 左电机基础控制函数 +void L_Motor_Control(MotorState state) + { + switch(state) { + case MOTOR_STOP: // IN1=0, IN2=0 + GPIO_ResetBits(L_MOTOR_IN1_GPIO_PORT, L_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(L_MOTOR_IN2_GPIO_PORT, L_MOTOR_IN2_GPIO_PIN); + break; + + case MOTOR_FORWARD: // IN1=1, IN2=0 + GPIO_SetBits(L_MOTOR_IN1_GPIO_PORT, L_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(L_MOTOR_IN2_GPIO_PORT, L_MOTOR_IN2_GPIO_PIN); + break; + + case MOTOR_REVERSE: // IN1=0, IN2=1 + GPIO_ResetBits(L_MOTOR_IN1_GPIO_PORT, L_MOTOR_IN1_GPIO_PIN); + GPIO_SetBits(L_MOTOR_IN2_GPIO_PORT, L_MOTOR_IN2_GPIO_PIN); + break; + + case MOTOR_BRAKE: // IN1=1, IN2=1 + GPIO_SetBits(L_MOTOR_IN1_GPIO_PORT, L_MOTOR_IN1_GPIO_PIN); + GPIO_SetBits(L_MOTOR_IN2_GPIO_PORT, L_MOTOR_IN2_GPIO_PIN); + break; + } +} + +// 中电机基础控制函数 +void M_Motor_Control(MotorState state) + { + switch(state) { + case MOTOR_STOP: // IN1=0, IN2=0 + GPIO_ResetBits(M_MOTOR_IN1_GPIO_PORT, M_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(M_MOTOR_IN2_GPIO_PORT, M_MOTOR_IN2_GPIO_PIN); + break; + + case MOTOR_FORWARD: // IN1=1, IN2=0 + GPIO_SetBits(M_MOTOR_IN1_GPIO_PORT, M_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(M_MOTOR_IN2_GPIO_PORT, M_MOTOR_IN2_GPIO_PIN); + break; + + case MOTOR_REVERSE: // IN1=0, IN2=1 + GPIO_ResetBits(M_MOTOR_IN1_GPIO_PORT, M_MOTOR_IN1_GPIO_PIN); + GPIO_SetBits(M_MOTOR_IN2_GPIO_PORT, M_MOTOR_IN2_GPIO_PIN); + break; + + case MOTOR_BRAKE: // IN1=1, IN2=1 + GPIO_SetBits(M_MOTOR_IN1_GPIO_PORT, M_MOTOR_IN1_GPIO_PIN); + GPIO_SetBits(M_MOTOR_IN2_GPIO_PORT, M_MOTOR_IN2_GPIO_PIN); + break; + } +} + +// 右电机基础控制函数 +void R_Motor_Control(MotorState state) + { + switch(state) { + case MOTOR_STOP: // IN1=0, IN2=0 + GPIO_ResetBits(R_MOTOR_IN1_GPIO_PORT, R_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(R_MOTOR_IN2_GPIO_PORT, R_MOTOR_IN2_GPIO_PIN); + break; + + case MOTOR_FORWARD: // IN1=1, IN2=0 + GPIO_SetBits(R_MOTOR_IN1_GPIO_PORT, R_MOTOR_IN1_GPIO_PIN); + GPIO_ResetBits(R_MOTOR_IN2_GPIO_PORT, R_MOTOR_IN2_GPIO_PIN); + break; + + case MOTOR_REVERSE: // IN1=0, IN2=1 + GPIO_ResetBits(R_MOTOR_IN1_GPIO_PORT, R_MOTOR_IN1_GPIO_PIN); + GPIO_SetBits(R_MOTOR_IN2_GPIO_PORT, R_MOTOR_IN2_GPIO_PIN); + break; + + case MOTOR_BRAKE: // IN1=1, IN2=1 + GPIO_SetBits(R_MOTOR_IN1_GPIO_PORT, R_MOTOR_IN1_GPIO_PIN); + GPIO_SetBits(R_MOTOR_IN2_GPIO_PORT, R_MOTOR_IN2_GPIO_PIN); + break; + } +} +//-------------------------------------------------------------------------------------------- +// 获取故障状态 +uint8_t L_Motor_GetFaultStatus(void) { + // FAULTN引脚为低电平时表示故障 + if (GPIO_ReadInputDataBit(L_MOTOR_FAULT_GPIO_PORT, L_MOTOR_FAULT_GPIO_PIN) == Bit_RESET) { + return 1; // 故障 + } + return 0; // 正常 +} + +// 安全的方向切换(避免电流冲击) +void L_Motor_SafeDirectionChange(MotorState new_direction) { + // 先刹车 + L_Motor_Control(MOTOR_BRAKE); + delay_ms(5); // 5ms延迟 + + // 再切换到新方向 + L_Motor_Control(new_direction); +} + + +// 获取故障状态 +uint8_t M_Motor_GetFaultStatus(void) { + // FAULTN引脚为低电平时表示故障 + if (GPIO_ReadInputDataBit(M_MOTOR_FAULT_GPIO_PORT, M_MOTOR_FAULT_GPIO_PIN) == Bit_RESET) { + return 1; // 故障 + } + return 0; // 正常 +} + +// 安全的方向切换(避免电流冲击) +void M_Motor_SafeDirectionChange(MotorState new_direction) { + // 先刹车 + M_Motor_Control(MOTOR_BRAKE); + delay_ms(5); // 5ms延迟 + + // 再切换到新方向 + M_Motor_Control(new_direction); +} + +// 获取故障状态 +uint8_t R_Motor_GetFaultStatus(void) { + // FAULTN引脚为低电平时表示故障 + if (GPIO_ReadInputDataBit(R_MOTOR_FAULT_GPIO_PORT, R_MOTOR_FAULT_GPIO_PIN) == Bit_RESET) { + return 1; // 故障 + } + return 0; // 正常 +} + +// 安全的方向切换(避免电流冲击) +void R_Motor_SafeDirectionChange(MotorState new_direction) { + // 先刹车 + R_Motor_Control(MOTOR_BRAKE); + delay_ms(5); // 5ms延迟 + + // 再切换到新方向 + R_Motor_Control(new_direction); +} \ No newline at end of file diff --git a/HARDWARE/MOTOR/motor_driver.h b/HARDWARE/MOTOR/motor_driver.h index 039eb27..aa94e65 100644 --- a/HARDWARE/MOTOR/motor_driver.h +++ b/HARDWARE/MOTOR/motor_driver.h @@ -7,18 +7,47 @@ #include "stm32f4xx_rcc.h" #include "stm32f4xx_tim.h" -// 引脚定义 -#define MOTOR_IN1_GPIO_PORT GPIOE -#define MOTOR_IN1_GPIO_PIN GPIO_Pin_9 -#define MOTOR_IN1_GPIO_CLK RCC_AHB1Periph_GPIOE +// 电机左 引脚定义 //PE9 PE13 PE4 +#define L_MOTOR_IN1_GPIO_PORT GPIOE +#define L_MOTOR_IN1_GPIO_PIN GPIO_Pin_9 +#define L_MOTOR_IN1_GPIO_CLK RCC_AHB1Periph_GPIOE -#define MOTOR_IN2_GPIO_PORT GPIOE -#define MOTOR_IN2_GPIO_PIN GPIO_Pin_13 -#define MOTOR_IN2_GPIO_CLK RCC_AHB1Periph_GPIOE +#define L_MOTOR_IN2_GPIO_PORT GPIOE +#define L_MOTOR_IN2_GPIO_PIN GPIO_Pin_13 +#define L_MOTOR_IN2_GPIO_CLK RCC_AHB1Periph_GPIOE -#define MOTOR_FAULT_GPIO_PORT GPIOE -#define MOTOR_FAULT_GPIO_PIN GPIO_Pin_4 -#define MOTOR_FAULT_GPIO_CLK RCC_AHB1Periph_GPIOE +#define L_MOTOR_FAULT_GPIO_PORT GPIOE +#define L_MOTOR_FAULT_GPIO_PIN GPIO_Pin_4 +#define L_MOTOR_FAULT_GPIO_CLK RCC_AHB1Periph_GPIOE + + + + +// 电机中 引脚定义 //PB3 PE14 PE5 +#define M_MOTOR_IN1_GPIO_PORT GPIOB +#define M_MOTOR_IN1_GPIO_PIN GPIO_Pin_3 +#define M_MOTOR_IN1_GPIO_CLK RCC_AHB1Periph_GPIOB + +#define M_MOTOR_IN2_GPIO_PORT GPIOE +#define M_MOTOR_IN2_GPIO_PIN GPIO_Pin_14 +#define M_MOTOR_IN2_GPIO_CLK RCC_AHB1Periph_GPIOE + +#define M_MOTOR_FAULT_GPIO_PORT GPIOE +#define M_MOTOR_FAULT_GPIO_PIN GPIO_Pin_5 +#define M_MOTOR_FAULT_GPIO_CLK RCC_AHB1Periph_GPIOE + +// 电机右 引脚定义 //PD15 PE15 PE6 +#define R_MOTOR_IN1_GPIO_PORT GPIOD +#define R_MOTOR_IN1_GPIO_PIN GPIO_Pin_15 +#define R_MOTOR_IN1_GPIO_CLK RCC_AHB1Periph_GPIOD + +#define R_MOTOR_IN2_GPIO_PORT GPIOE +#define R_MOTOR_IN2_GPIO_PIN GPIO_Pin_15 +#define R_MOTOR_IN2_GPIO_CLK RCC_AHB1Periph_GPIOE + +#define R_MOTOR_FAULT_GPIO_PORT GPIOE +#define R_MOTOR_FAULT_GPIO_PIN GPIO_Pin_6 +#define R_MOTOR_FAULT_GPIO_CLK RCC_AHB1Periph_GPIOE // 电机状态 typedef enum { @@ -34,5 +63,7 @@ void Motor_Control(MotorState state); uint8_t Motor_GetFaultStatus(void); void Motor_SetSpeed(uint8_t speed, MotorState direction); void PWM_Timer_Init(void); - +void L_Motor_Control(MotorState state); +void M_Motor_Control(MotorState state); +void R_Motor_Control(MotorState state); #endif /* __MOTOR_DRIVER_H */ \ No newline at end of file diff --git a/OBJ/USART.axf b/OBJ/USART.axf index c4dc9a8..41a9889 100644 Binary files a/OBJ/USART.axf and b/OBJ/USART.axf differ diff --git a/OBJ/USART.build_log.htm b/OBJ/USART.build_log.htm index ad92669..ae1878f 100644 --- a/OBJ/USART.build_log.htm +++ b/OBJ/USART.build_log.htm @@ -3,45 +3,48 @@
 

礦ision Build Log

Tool Versions:

-IDE-Version: μVision V5.25.3.0 -Copyright (C) 2018 ARM Ltd and ARM Germany GmbH. All rights reserved. -License Information: cgy cgy, cgy, LIC=CRMFW-QWL2E-RC0JZ-J6WMG-6WJZU-2F249 +IDE-Version: μVision V5.38.0.0 +Copyright (C) 2022 ARM Ltd and ARM Germany GmbH. All rights reserved. +License Information: baibaila3 baibaila3@163.com, keil, LIC=47GU5-GE5WI-JJF6S-L5PAY-BRK7N-23XAJ Tool Versions: -Toolchain: MDK-ARM Professional Version: 5.15.0 +Toolchain: MDK-ARM Plus Version: 5.22 Toolchain Path: C:\Keil_v5\ARM\ARMCC\Bin -C Compiler: Armcc.exe V5.05 update 2 (build 169) -Assembler: Armasm.exe V5.05 update 2 (build 169) -Linker/Locator: ArmLink.exe V5.05 update 2 (build 169) -Library Manager: ArmAr.exe V5.05 update 2 (build 169) -Hex Converter: FromElf.exe V5.05 update 2 (build 169) -CPU DLL: SARMCM3.DLL V5.15.0 -Dialog DLL: DCM.DLL V1.13.2.0 -Target DLL: STLink\ST-LINKIII-KEIL_SWO.dll V2.0.15.0_KEIL -Dialog DLL: TCM.DLL V1.14.5.0 +C Compiler: Armcc.exe V5.06 update 4 (build 422) +Assembler: Armasm.exe V5.06 update 4 (build 422) +Linker/Locator: ArmLink.exe V5.06 update 4 (build 422) +Library Manager: ArmAr.exe V5.06 update 4 (build 422) +Hex Converter: FromElf.exe V5.06 update 4 (build 422) +CPU DLL: SARMCM3.DLL V5.22 +Dialog DLL: DCM.DLL V1.13.9.0 +Target DLL: STLink\ST-LINKIII-KEIL_SWO.dll V2.0.18.0 +Dialog DLL: TCM.DLL V1.21.0.0

Project:

-D:\fighterteam\project\gu\make\code\sensor_2026\sensor_2026\USER\USART.uvprojx -Project File Date: 02/11/2026 +E:\CGY_2026\GIT\sensor_2026 - 副本\USER\USART.uvprojx +Project File Date: 02/22/2026

Output:

-*** Using Compiler 'V5.05 update 2 (build 169)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin' +*** Using Compiler 'V5.06 update 4 (build 422)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin' Build target 'USART' +compiling main.c... +linking... +Program Size: Code=6820 RO-data=424 RW-data=120 ZI-data=1024 +FromELF: creating hex file... "..\OBJ\USART.axf" - 0 Error(s), 0 Warning(s).

Software Packages used:

Package Vendor: Keil - http://www.keil.com/pack/Keil.STM32F4xx_DFP.2.14.0.pack - Keil.STM32F4xx_DFP.2.14.0 + http://www.keil.com/pack/Keil.STM32F4xx_DFP.2.11.0.pack + Keil.STM32F4xx_DFP.2.11.0 STMicroelectronics STM32F4 Series Device Support, Drivers and Examples

Collection of Component include folders:

- .\RTE\_USART - C:\Keil_v5\ARM\PACK\Keil\STM32F4xx_DFP\2.14.0\Drivers\CMSIS\Device\ST\STM32F4xx\Include + C:/Keil_v5/ARM/PACK/Keil/STM32F4xx_DFP/2.11.0/Drivers/CMSIS/Device/ST/STM32F4xx/Include

Collection of Component Files used:

-Build Time Elapsed: 00:00:00 +Build Time Elapsed: 00:00:01
diff --git a/OBJ/USART.htm b/OBJ/USART.htm index 0d343fa..460a8d2 100644 --- a/OBJ/USART.htm +++ b/OBJ/USART.htm @@ -3,7 +3,7 @@ Static Call Graph - [..\OBJ\USART.axf]

Static Call Graph for image ..\OBJ\USART.axf


-

#<CALLGRAPH># ARM Linker, 5050169: Last Updated: Wed Feb 11 09:09:32 2026 +

#<CALLGRAPH># ARM Linker, 5060422: Last Updated: Wed Feb 25 11:07:10 2026

Maximum Stack Usage = 124 bytes + Unknown(Cycles, Untraceable Function Pointers)

Call chain for Maximum Stack Depth:

@@ -111,7 +111,6 @@ Function Pointers
  • UsageFault_Handler from stm32f4xx_it.o(i.UsageFault_Handler) referenced from startup_stm32f40_41xxx.o(RESET)
  • WWDG_IRQHandler from startup_stm32f40_41xxx.o(.text) referenced from startup_stm32f40_41xxx.o(RESET)
  • __main from entry.o(.ARM.Collect$$$$00000000) referenced from startup_stm32f40_41xxx.o(.text) -
  • fputc from usart.o(i.fputc) referenced from printfb.o(i.__0printf$bare)
  • main from main.o(i.main) referenced from entry9a.o(.ARM.Collect$$$$0000000B)

    @@ -121,25 +120,25 @@ Global Symbols

    __main (Thumb, 0 bytes, Stack size unknown bytes, entry.o(.ARM.Collect$$$$00000000))
    [Address Reference Count : 1]

    -

    _main_stk (Thumb, 0 bytes, Stack size unknown bytes, entry2.o(.ARM.Collect$$$$00000001)) +

    _main_stk (Thumb, 0 bytes, Stack size unknown bytes, entry2.o(.ARM.Collect$$$$00000001)) -

    _main_scatterload (Thumb, 0 bytes, Stack size unknown bytes, entry5.o(.ARM.Collect$$$$00000004)) -

    [Calls]