2026-02-11 01:18:00 +08:00
|
|
|
|
// motor_driver.c
|
|
|
|
|
|
#include "motor_driver.h"
|
|
|
|
|
|
#include "delay.h"
|
|
|
|
|
|
// ȫ<>ֱ<EFBFBD><D6B1><EFBFBD>
|
2026-02-25 11:13:03 +08:00
|
|
|
|
static uint8_t L_pwm_duty = 0; // PWMռ<4D>ձ<EFBFBD> 0-100
|
|
|
|
|
|
static uint8_t M_pwm_duty = 0; // PWMռ<4D>ձ<EFBFBD> 0-100
|
|
|
|
|
|
static uint8_t R_pwm_duty = 0; // PWMռ<4D>ձ<EFBFBD> 0-100
|
|
|
|
|
|
static MotorState L_motor_dir = MOTOR_STOP;
|
|
|
|
|
|
static MotorState M_motor_dir = MOTOR_STOP;
|
|
|
|
|
|
static MotorState R_motor_dir = MOTOR_STOP;
|
2026-02-11 01:18:00 +08:00
|
|
|
|
// GPIO<49><4F>ʼ<EFBFBD><CABC>
|
|
|
|
|
|
void DRV8832_GPIO_Init(void) {
|
|
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
|
|
|
2026-02-25 11:13:03 +08:00
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>ų<EFBFBD>ʼ<EFBFBD><CABC>
|
2026-02-11 01:18:00 +08:00
|
|
|
|
// ʹ<><CAB9>GPIOEʱ<45><CAB1>
|
2026-02-25 11:13:03 +08:00
|
|
|
|
RCC_AHB1PeriphClockCmd(L_MOTOR_IN1_GPIO_CLK, ENABLE);
|
2026-02-11 01:18:00 +08:00
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>IN1 (PE9) <20><> IN2 (PE13) Ϊ<><CEAA><EFBFBD><EFBFBD>
|
2026-02-25 11:13:03 +08:00
|
|
|
|
GPIO_InitStructure.GPIO_Pin = L_MOTOR_IN1_GPIO_PIN | L_MOTOR_IN2_GPIO_PIN;
|
2026-02-11 01:18:00 +08:00
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
|
|
|
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
|
|
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
2026-02-25 11:13:03 +08:00
|
|
|
|
GPIO_Init(L_MOTOR_IN1_GPIO_PORT, &GPIO_InitStructure);
|
2026-02-11 01:18:00 +08:00
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>FAULTN (PE4) Ϊ<><CEAA><EFBFBD><EFBFBD>
|
2026-02-25 11:13:03 +08:00
|
|
|
|
GPIO_InitStructure.GPIO_Pin = L_MOTOR_FAULT_GPIO_PIN;
|
2026-02-11 01:18:00 +08:00
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
|
|
|
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // <20><><EFBFBD><EFBFBD>
|
2026-02-25 11:13:03 +08:00
|
|
|
|
GPIO_Init(L_MOTOR_FAULT_GPIO_PORT, &GPIO_InitStructure);
|
2026-02-11 01:18:00 +08:00
|
|
|
|
|
|
|
|
|
|
// <20><>ʼ״̬<D7B4><CCAC>ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
|
2026-02-25 11:13:03 +08:00
|
|
|
|
GPIO_ResetBits(L_MOTOR_IN1_GPIO_PORT, L_MOTOR_IN1_GPIO_PIN);
|
|
|
|
|
|
GPIO_ResetBits(L_MOTOR_IN2_GPIO_PORT, L_MOTOR_IN2_GPIO_PIN);
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
//<2F>е<EFBFBD><D0B5><EFBFBD> <20><><EFBFBD>ų<EFBFBD>ʼ<EFBFBD><CABC>
|
|
|
|
|
|
// ʹ<><CAB9>GPIOEʱ<45><CAB1>
|
|
|
|
|
|
RCC_AHB1PeriphClockCmd(M_MOTOR_IN1_GPIO_CLK, ENABLE);
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>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; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
|
|
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
|
|
|
|
GPIO_Init(M_MOTOR_IN1_GPIO_PORT, &GPIO_InitStructure);
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> IN2 (PE14) Ϊ<><CEAA><EFBFBD><EFBFBD>
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = M_MOTOR_IN2_GPIO_PIN;
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
|
|
|
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
|
|
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
|
|
|
|
GPIO_Init(M_MOTOR_IN2_GPIO_PORT, &GPIO_InitStructure);
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>FAULTN (PE5) Ϊ<><CEAA><EFBFBD><EFBFBD>
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = M_MOTOR_FAULT_GPIO_PIN;
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
|
|
|
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
GPIO_Init(M_MOTOR_FAULT_GPIO_PORT, &GPIO_InitStructure);
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ʼ״̬<D7B4><CCAC>ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
|
|
|
|
|
|
GPIO_ResetBits(M_MOTOR_IN1_GPIO_PORT, M_MOTOR_IN1_GPIO_PIN);
|
|
|
|
|
|
GPIO_ResetBits(M_MOTOR_IN2_GPIO_PORT, M_MOTOR_IN2_GPIO_PIN);
|
|
|
|
|
|
//===========================================================================
|
|
|
|
|
|
//<2F>ҵ<EFBFBD><D2B5><EFBFBD> <20><><EFBFBD>ų<EFBFBD>ʼ<EFBFBD><CABC>
|
|
|
|
|
|
// ʹ<><CAB9>GPIOEʱ<45><CAB1>
|
|
|
|
|
|
RCC_AHB1PeriphClockCmd(R_MOTOR_IN1_GPIO_CLK, ENABLE);
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>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; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
|
|
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
|
|
|
|
GPIO_Init(R_MOTOR_IN1_GPIO_PORT, &GPIO_InitStructure);
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> IN2 (PE15) Ϊ<><CEAA><EFBFBD><EFBFBD>
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = R_MOTOR_IN2_GPIO_PIN;
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
|
|
|
|
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
|
|
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
|
|
|
|
|
GPIO_Init(R_MOTOR_IN2_GPIO_PORT, &GPIO_InitStructure);
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>FAULTN (PE6) Ϊ<><CEAA><EFBFBD><EFBFBD>
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = R_MOTOR_FAULT_GPIO_PIN;
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
|
|
|
|
|
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
GPIO_Init(R_MOTOR_FAULT_GPIO_PORT, &GPIO_InitStructure);
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ʼ״̬<D7B4><CCAC>ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
|
|
|
|
|
|
GPIO_ResetBits(R_MOTOR_IN1_GPIO_PORT, R_MOTOR_IN1_GPIO_PIN);
|
|
|
|
|
|
GPIO_ResetBits(R_MOTOR_IN2_GPIO_PORT, R_MOTOR_IN2_GPIO_PIN);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-02-11 01:18:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ʼ<EFBFBD><CABC>DRV8832
|
|
|
|
|
|
void DRV8832_Init(void) {
|
|
|
|
|
|
DRV8832_GPIO_Init();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ʹ<><CAB9>TIM2ʵ<32><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>PWM
|
|
|
|
|
|
#define PWM_FREQUENCY 20000 // 20kHz PWMƵ<4D><C6B5>
|
|
|
|
|
|
#define PWM_RESOLUTION 100 // 100<30><30><EFBFBD><EFBFBD><EFBFBD>ٷֱ<D9B7><D6B1><EFBFBD>
|
|
|
|
|
|
|
2026-02-25 11:13:03 +08:00
|
|
|
|
static uint8_t L_pwm_counter = 0; // PWM<57><4D><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
static uint8_t M_pwm_counter = 0; // PWM<57><4D><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
static uint8_t R_pwm_counter = 0; // PWM<57><4D><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2026-02-11 01:18:00 +08:00
|
|
|
|
// <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
|
|
|
|
|
void PWM_Timer_Init(void) {
|
|
|
|
|
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
|
|
|
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
|
|
|
|
|
|
|
|
|
|
// ʹ<><CAB9>TIM2ʱ<32><CAB1>
|
|
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
TIM_TimeBaseStructure.TIM_Period = (SystemCoreClock / (2 * PWM_FREQUENCY)) - 1; // 168MHz/2/20kHz
|
|
|
|
|
|
TIM_TimeBaseStructure.TIM_Prescaler = 0;
|
|
|
|
|
|
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
|
|
|
|
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
|
|
|
|
|
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
|
|
|
|
|
|
|
|
|
|
|
// ʹ<>ܶ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
|
|
|
|
|
|
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>NVIC
|
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
|
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
|
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
|
|
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|
|
|
|
|
TIM_Cmd(TIM2, ENABLE);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ʱ<EFBFBD><CAB1><EFBFBD>жϴ<D0B6><CFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2026-02-25 11:13:03 +08:00
|
|
|
|
void TIM2_IRQHandler(void)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
|
|
|
|
|
|
{
|
2026-02-11 01:18:00 +08:00
|
|
|
|
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
2026-02-25 11:13:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{ //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
L_pwm_counter++;
|
|
|
|
|
|
if (L_pwm_counter >= PWM_RESOLUTION)
|
|
|
|
|
|
{
|
|
|
|
|
|
L_pwm_counter = 0;
|
|
|
|
|
|
}
|
2026-02-11 01:18:00 +08:00
|
|
|
|
|
2026-02-25 11:13:03 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>ռ<EFBFBD>ձȺͷ<C8BA><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD>
|
|
|
|
|
|
if (L_motor_dir == MOTOR_FORWARD) {
|
|
|
|
|
|
if (L_pwm_counter < L_pwm_duty) {
|
|
|
|
|
|
// <20><>ת<EFBFBD><D7AA>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);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>
|
|
|
|
|
|
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 (L_motor_dir == MOTOR_REVERSE) {
|
|
|
|
|
|
if (L_pwm_counter < L_pwm_duty) {
|
|
|
|
|
|
// <20><>ת<EFBFBD><D7AA>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);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>
|
|
|
|
|
|
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(L_MOTOR_IN1_GPIO_PORT, L_MOTOR_IN1_GPIO_PIN);
|
|
|
|
|
|
GPIO_ResetBits(L_MOTOR_IN2_GPIO_PORT, L_MOTOR_IN2_GPIO_PIN);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
|
|
{ //<2F>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
M_pwm_counter++;
|
|
|
|
|
|
if (M_pwm_counter >= PWM_RESOLUTION)
|
|
|
|
|
|
{
|
|
|
|
|
|
M_pwm_counter = 0;
|
2026-02-11 01:18:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ռ<EFBFBD>ձȺͷ<C8BA><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD>
|
2026-02-25 11:13:03 +08:00
|
|
|
|
if (M_motor_dir == MOTOR_FORWARD) {
|
|
|
|
|
|
if (M_pwm_counter < M_pwm_duty) {
|
2026-02-11 01:18:00 +08:00
|
|
|
|
// <20><>ת<EFBFBD><D7AA>IN1=1, IN2=0
|
2026-02-25 11:13:03 +08:00
|
|
|
|
GPIO_SetBits(M_MOTOR_IN1_GPIO_PORT, M_MOTOR_IN1_GPIO_PIN);
|
|
|
|
|
|
GPIO_ResetBits(M_MOTOR_IN2_GPIO_PORT, M_MOTOR_IN2_GPIO_PIN);
|
2026-02-11 01:18:00 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>
|
2026-02-25 11:13:03 +08:00
|
|
|
|
GPIO_ResetBits(M_MOTOR_IN1_GPIO_PORT, M_MOTOR_IN1_GPIO_PIN);
|
|
|
|
|
|
GPIO_ResetBits(M_MOTOR_IN2_GPIO_PORT, M_MOTOR_IN2_GPIO_PIN);
|
2026-02-11 01:18:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-25 11:13:03 +08:00
|
|
|
|
else if (M_motor_dir == MOTOR_REVERSE) {
|
|
|
|
|
|
if (M_pwm_counter < M_pwm_duty) {
|
2026-02-11 01:18:00 +08:00
|
|
|
|
// <20><>ת<EFBFBD><D7AA>IN1=0, IN2=1
|
2026-02-25 11:13:03 +08:00
|
|
|
|
GPIO_ResetBits(M_MOTOR_IN1_GPIO_PORT, M_MOTOR_IN1_GPIO_PIN);
|
|
|
|
|
|
GPIO_SetBits(M_MOTOR_IN2_GPIO_PORT, M_MOTOR_IN2_GPIO_PIN);
|
2026-02-11 01:18:00 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>
|
2026-02-25 11:13:03 +08:00
|
|
|
|
GPIO_ResetBits(M_MOTOR_IN1_GPIO_PORT, M_MOTOR_IN1_GPIO_PIN);
|
|
|
|
|
|
GPIO_ResetBits(M_MOTOR_IN2_GPIO_PORT, M_MOTOR_IN2_GPIO_PIN);
|
2026-02-11 01:18:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
// ֹͣ״̬
|
2026-02-25 11:13:03 +08:00
|
|
|
|
GPIO_ResetBits(M_MOTOR_IN1_GPIO_PORT, M_MOTOR_IN1_GPIO_PIN);
|
|
|
|
|
|
GPIO_ResetBits(M_MOTOR_IN2_GPIO_PORT, M_MOTOR_IN2_GPIO_PIN);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
|
|
{ //<2F>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
R_pwm_counter++;
|
|
|
|
|
|
if (R_pwm_counter >= PWM_RESOLUTION)
|
|
|
|
|
|
{
|
|
|
|
|
|
R_pwm_counter = 0;
|
2026-02-11 01:18:00 +08:00
|
|
|
|
}
|
2026-02-25 11:13:03 +08:00
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ռ<EFBFBD>ձȺͷ<C8BA><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD>
|
|
|
|
|
|
if (R_motor_dir == MOTOR_FORWARD)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (R_pwm_counter < R_pwm_duty) {
|
|
|
|
|
|
// <20><>ת<EFBFBD><D7AA>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 {
|
|
|
|
|
|
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>
|
|
|
|
|
|
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) {
|
|
|
|
|
|
// <20><>ת<EFBFBD><D7AA>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 {
|
|
|
|
|
|
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//---------------------------------------------------------------------------------
|
|
|
|
|
|
|
2026-02-11 01:18:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-25 11:13:03 +08:00
|
|
|
|
//=========<3D><><EFBFBD><EFBFBD><EFBFBD>ٶȺͷ<C8BA><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>================================================
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶȺͷ<C8BA><CDB7><EFBFBD>
|
|
|
|
|
|
void L_Motor_SetSpeed(uint8_t speed, MotorState direction)
|
|
|
|
|
|
{
|
2026-02-11 01:18:00 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ٶȷ<D9B6>Χ
|
|
|
|
|
|
if (speed > 100) speed = 100;
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>Ϊ0<CEAA><30>100%<25><>ֱ<EFBFBD><D6B1>ʹ<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
if (speed == 0) {
|
2026-02-25 11:13:03 +08:00
|
|
|
|
L_motor_dir = MOTOR_STOP;
|
|
|
|
|
|
L_pwm_duty = 0;
|
|
|
|
|
|
L_Motor_Control(MOTOR_STOP);
|
2026-02-11 01:18:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (speed == 100) {
|
2026-02-25 11:13:03 +08:00
|
|
|
|
L_motor_dir = direction;
|
|
|
|
|
|
L_pwm_duty = 100;
|
|
|
|
|
|
L_Motor_Control(direction);
|
2026-02-11 01:18:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>PWM<57><4D><EFBFBD><EFBFBD>
|
2026-02-25 11:13:03 +08:00
|
|
|
|
L_motor_dir = direction;
|
|
|
|
|
|
L_pwm_duty = speed; // ֱ<><D6B1>ʹ<EFBFBD>ðٷֱ<D9B7>
|
2026-02-11 01:18:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-25 11:13:03 +08:00
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD>ٶȺͷ<C8BA><CDB7><EFBFBD>
|
|
|
|
|
|
void M_Motor_SetSpeed(uint8_t speed, MotorState direction)
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ٶȷ<D9B6>Χ
|
|
|
|
|
|
if (speed > 100) speed = 100;
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>Ϊ0<CEAA><30>100%<25><>ֱ<EFBFBD><D6B1>ʹ<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
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 {
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>PWM<57><4D><EFBFBD><EFBFBD>
|
|
|
|
|
|
M_motor_dir = direction;
|
|
|
|
|
|
M_pwm_duty = speed; // ֱ<><D6B1>ʹ<EFBFBD>ðٷֱ<D9B7>
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>ٶȺͷ<C8BA><CDB7><EFBFBD>
|
|
|
|
|
|
void R_Motor_SetSpeed(uint8_t speed, MotorState direction)
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ٶȷ<D9B6>Χ
|
|
|
|
|
|
if (speed > 100) speed = 100;
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>Ϊ0<CEAA><30>100%<25><>ֱ<EFBFBD><D6B1>ʹ<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
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 {
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>PWM<57><4D><EFBFBD><EFBFBD>
|
|
|
|
|
|
R_motor_dir = direction;
|
|
|
|
|
|
R_pwm_duty = speed; // ֱ<><D6B1>ʹ<EFBFBD>ðٷֱ<D9B7>
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//=====================<3D><><EFBFBD><EFBFBD>ֱ<EFBFBD>ӿ<EFBFBD><D3BF><EFBFBD>================================================================
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƺ<EFBFBD><C6BA><EFBFBD>
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƺ<EFBFBD><C6BA><EFBFBD>
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƺ<EFBFBD><C6BA><EFBFBD>
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------
|
|
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>״̬
|
|
|
|
|
|
uint8_t L_Motor_GetFaultStatus(void) {
|
|
|
|
|
|
// FAULTN<54><4E><EFBFBD><EFBFBD>Ϊ<EFBFBD>͵<EFBFBD>ƽʱ<C6BD><CAB1>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
|
|
|
|
|
|
if (GPIO_ReadInputDataBit(L_MOTOR_FAULT_GPIO_PORT, L_MOTOR_FAULT_GPIO_PIN) == Bit_RESET) {
|
|
|
|
|
|
return 1; // <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0; // <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ȫ<EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
void L_Motor_SafeDirectionChange(MotorState new_direction) {
|
|
|
|
|
|
// <20><>ɲ<EFBFBD><C9B2>
|
|
|
|
|
|
L_Motor_Control(MOTOR_BRAKE);
|
|
|
|
|
|
delay_ms(5); // 5ms<6D>ӳ<EFBFBD>
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD>
|
|
|
|
|
|
L_Motor_Control(new_direction);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>״̬
|
|
|
|
|
|
uint8_t M_Motor_GetFaultStatus(void) {
|
|
|
|
|
|
// FAULTN<54><4E><EFBFBD><EFBFBD>Ϊ<EFBFBD>͵<EFBFBD>ƽʱ<C6BD><CAB1>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
|
|
|
|
|
|
if (GPIO_ReadInputDataBit(M_MOTOR_FAULT_GPIO_PORT, M_MOTOR_FAULT_GPIO_PIN) == Bit_RESET) {
|
|
|
|
|
|
return 1; // <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0; // <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ȫ<EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
void M_Motor_SafeDirectionChange(MotorState new_direction) {
|
|
|
|
|
|
// <20><>ɲ<EFBFBD><C9B2>
|
|
|
|
|
|
M_Motor_Control(MOTOR_BRAKE);
|
|
|
|
|
|
delay_ms(5); // 5ms<6D>ӳ<EFBFBD>
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD>
|
|
|
|
|
|
M_Motor_Control(new_direction);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>״̬
|
|
|
|
|
|
uint8_t R_Motor_GetFaultStatus(void) {
|
|
|
|
|
|
// FAULTN<54><4E><EFBFBD><EFBFBD>Ϊ<EFBFBD>͵<EFBFBD>ƽʱ<C6BD><CAB1>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
|
|
|
|
|
|
if (GPIO_ReadInputDataBit(R_MOTOR_FAULT_GPIO_PORT, R_MOTOR_FAULT_GPIO_PIN) == Bit_RESET) {
|
|
|
|
|
|
return 1; // <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0; // <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ȫ<EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
void R_Motor_SafeDirectionChange(MotorState new_direction) {
|
|
|
|
|
|
// <20><>ɲ<EFBFBD><C9B2>
|
|
|
|
|
|
R_Motor_Control(MOTOR_BRAKE);
|
|
|
|
|
|
delay_ms(5); // 5ms<6D>ӳ<EFBFBD>
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD>
|
|
|
|
|
|
R_Motor_Control(new_direction);
|
|
|
|
|
|
}
|