generated from Template/H563ZI-HAL-CMake-Template
add Motor function
This commit is contained in:
86
fun/Motor.c
Normal file
86
fun/Motor.c
Normal file
@@ -0,0 +1,86 @@
|
||||
#include "Motor.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
|
||||
/*******
|
||||
PWMA --- TIM4_CHANNEL4
|
||||
|
||||
PMWB --- TIM3_CHANNEL4
|
||||
|
||||
STBY --- 5V Ĭ<><C4AC>5V <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
||||
*******/
|
||||
void PWM_GPIO_TIM_Init(void)
|
||||
{
|
||||
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_4);
|
||||
HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_4);
|
||||
}
|
||||
|
||||
|
||||
void MotorA_Dir(uint8_t dir)
|
||||
{
|
||||
if(0 == dir) // ǰ<><C7B0>
|
||||
{
|
||||
HAL_GPIO_WritePin(AIN1_GPIO_Port,AIN1_Pin,GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(AIN2_GPIO_Port,AIN2_Pin,GPIO_PIN_RESET);
|
||||
}
|
||||
else // <20><>ת
|
||||
{
|
||||
HAL_GPIO_WritePin(AIN1_GPIO_Port,AIN1_Pin,GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(AIN2_GPIO_Port,AIN2_Pin,GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void MotorB_Dir(uint8_t dir)
|
||||
{
|
||||
if(0 == dir) // ǰ<><C7B0>
|
||||
{
|
||||
HAL_GPIO_WritePin(BIN1_GPIO_Port,BIN1_Pin,GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(BIN2_GPIO_Port,BIN2_Pin,GPIO_PIN_RESET);
|
||||
}
|
||||
else // <20><>ת
|
||||
{
|
||||
HAL_GPIO_WritePin(BIN1_GPIO_Port,BIN1_Pin,GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(BIN2_GPIO_Port,BIN2_Pin,GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>A<EFBFBD><41><EFBFBD>ٶ<EFBFBD>
|
||||
void MotorA_Speed(uint8_t speed)
|
||||
{
|
||||
__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_4,speed);
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>B<EFBFBD><42><EFBFBD>ٶ<EFBFBD>
|
||||
void MotorB_Speed(uint8_t speed)
|
||||
{
|
||||
__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_4,speed);
|
||||
}
|
||||
|
||||
|
||||
// ʾ<><CABE> ͬʱ<CDAC><CAB1><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD>A/B
|
||||
// <20><>ʾ <20><><EFBFBD><EFBFBD> Dir 0 ---- ǰ<><C7B0> go forward
|
||||
// <20><><EFBFBD><EFBFBD> Dir 1 ---- <20><><EFBFBD><EFBFBD> go forward
|
||||
void DriveBOTH(int16_t speedA,int16_t speedB)
|
||||
{
|
||||
// <20><><EFBFBD><EFBFBD> A
|
||||
if (speedA >= 0) MotorA_Dir(0);
|
||||
else
|
||||
{
|
||||
MotorA_Dir(1);
|
||||
speedA = -speedA;
|
||||
}
|
||||
MotorA_Speed(speedA);
|
||||
|
||||
// <20><><EFBFBD><EFBFBD> B
|
||||
if (speedB >= 0) MotorA_Dir(0);
|
||||
else
|
||||
{
|
||||
MotorB_Dir(1);
|
||||
speedB = -speedB;
|
||||
}
|
||||
MotorA_Speed(speedB);
|
||||
}
|
||||
Reference in New Issue
Block a user