212 lines
7.7 KiB
C
212 lines
7.7 KiB
C
|
#include "bsp_motor.h"
|
||
|
|
||
|
#include <stdint.h>
|
||
|
#include <stdio.h>
|
||
|
|
||
|
#include "stm32f10x.h" // Device header
|
||
|
#include "stm32f10x_gpio.h"
|
||
|
|
||
|
/* A IN1 */
|
||
|
#define A_IN1_GPIO_PORT (GPIOA)
|
||
|
#define A_IN1_GPIO_PIN (GPIO_Pin_4)
|
||
|
/* A IN2 */
|
||
|
#define A_IN2_GPIO_PORT (GPIOA)
|
||
|
#define A_IN2_GPIO_PIN (GPIO_Pin_5)
|
||
|
/* B IN1 */
|
||
|
#define B_IN1_GPIO_PORT (GPIOA)
|
||
|
#define B_IN1_GPIO_PIN (GPIO_Pin_6)
|
||
|
/* B IN2 */
|
||
|
#define B_IN2_GPIO_PORT (GPIOA)
|
||
|
#define B_IN2_GPIO_PIN (GPIO_Pin_7)
|
||
|
|
||
|
/* 电机驱动PWM定时器 */
|
||
|
#define TIM_MOTOR TIM2
|
||
|
#define TIM_MOTOR_IRQn TIM2_IRQn
|
||
|
#define TIM_MOTOR_PERIPH_RCC RCC_APB1Periph_TIM2
|
||
|
#define TIM_MOTOR_PWN_GPIO_RCC RCC_APB2Periph_GPIOA
|
||
|
#define TIM_MOTOR_PWN_GPIO_PORT GPIOA
|
||
|
#define TIM_MOTOR_PWN_GPIO_PIN GPIO_Pin_2
|
||
|
|
||
|
/*************************************************************************************
|
||
|
* @brief 对外输出至电机驱动板IN接口的GPIO初始化
|
||
|
*
|
||
|
* @warning 【不可重入,阻塞等警告】
|
||
|
* @note 【重大修改】
|
||
|
*************************************************************************************/
|
||
|
void bsp_InitGPIO_MotorOut(void)
|
||
|
{
|
||
|
GPIO_InitTypeDef GPIO_InitStructure;
|
||
|
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
||
|
|
||
|
|
||
|
/* AIN1 初始化 */
|
||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||
|
GPIO_InitStructure.GPIO_Pin = A_IN1_GPIO_PIN;
|
||
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
|
GPIO_Init(A_IN1_GPIO_PORT, &GPIO_InitStructure);
|
||
|
/* AIN2 初始化 */
|
||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||
|
GPIO_InitStructure.GPIO_Pin = A_IN2_GPIO_PIN;
|
||
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
|
GPIO_Init(A_IN2_GPIO_PORT, &GPIO_InitStructure);
|
||
|
/* BIN1 初始化 */
|
||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||
|
GPIO_InitStructure.GPIO_Pin = B_IN1_GPIO_PIN;
|
||
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
|
GPIO_Init(B_IN1_GPIO_PORT, &GPIO_InitStructure);
|
||
|
/* BIN2 初始化 */
|
||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||
|
GPIO_InitStructure.GPIO_Pin = B_IN2_GPIO_PIN;
|
||
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||
|
GPIO_Init(B_IN2_GPIO_PORT, &GPIO_InitStructure);
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
/*************************************************************************************
|
||
|
* @brief 初始化驱动电机的定时器
|
||
|
*
|
||
|
* @warning 【不可重入,阻塞等警告】
|
||
|
* @note 【重大修改】
|
||
|
*************************************************************************************/
|
||
|
void bsp_InitMotorTimer(void)
|
||
|
{
|
||
|
GPIO_InitTypeDef GPIO_InitStructure;
|
||
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
||
|
NVIC_InitTypeDef NVIC_InitStructure;
|
||
|
TIM_OCInitTypeDef TIM_OCInitStructure;
|
||
|
/* 1. 使能TIM时钟 */
|
||
|
RCC_APB1PeriphClockCmd(TIM_MOTOR_PERIPH_RCC, ENABLE);
|
||
|
/* 2. 使能对应的GPIO引脚 */
|
||
|
GPIO_Init(TIM_MOTOR_PWN_GPIO_PORT, &GPIO_InitStructure); //初始化 GPIOA.2
|
||
|
RCC_APB2PeriphClockCmd(TIM_MOTOR_PWN_GPIO_RCC, ENABLE); //使能 PA 端口时钟
|
||
|
GPIO_InitStructure.GPIO_Pin = TIM_MOTOR_PWN_GPIO_PIN;
|
||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO 口速度为 50MHz
|
||
|
GPIO_Init(TIM_MOTOR_PWN_GPIO_PORT, &GPIO_InitStructure);
|
||
|
/* 3. Configures the TIMx internal Clock */
|
||
|
TIM_InternalClockConfig(TIM_MOTOR);
|
||
|
/* 4. 定义时基单元 */
|
||
|
TIM_TimeBaseStructure.TIM_Period = (100u - 1); // 周期 ARR自动重装器的值
|
||
|
TIM_TimeBaseStructure.TIM_Prescaler = (36u - 1) ; // PSC预分频器的值
|
||
|
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
|
||
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 计数器模式
|
||
|
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; // 重复计数器的值,高级定时器才有
|
||
|
TIM_TimeBaseInit(TIM_MOTOR, &TIM_TimeBaseStructure);
|
||
|
/* 5. 输出比较 */
|
||
|
TIM_OCStructInit(&TIM_OCInitStructure);
|
||
|
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
|
||
|
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
|
||
|
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
|
||
|
TIM_OCInitStructure.TIM_Pulse = 0; //CCR
|
||
|
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
|
||
|
/* 6. TIMx enable counter */
|
||
|
TIM_Cmd(TIM_MOTOR, ENABLE);
|
||
|
}
|
||
|
|
||
|
/*************************************************************************************
|
||
|
* @brief 初始化电机驱动器
|
||
|
*
|
||
|
* @warning 【不可重入,阻塞等警告】
|
||
|
* @note 【重大修改】
|
||
|
*************************************************************************************/
|
||
|
void bsp_InitMotor(void)
|
||
|
{
|
||
|
bsp_InitGPIO_MotorOut();
|
||
|
|
||
|
bsp_InitMotorTimer();
|
||
|
}
|
||
|
/*************************************************************************************
|
||
|
* @brief A IN1 开
|
||
|
*
|
||
|
* @warning 【不可重入,阻塞等警告】
|
||
|
* @note 【重大修改】
|
||
|
*************************************************************************************/
|
||
|
void bsp_AIN1_ON(void)
|
||
|
{
|
||
|
GPIO_SetBits(A_IN1_GPIO_PORT, A_IN1_GPIO_PIN);
|
||
|
}
|
||
|
/*************************************************************************************
|
||
|
* @brief A IN1 关
|
||
|
*
|
||
|
* @warning 【不可重入,阻塞等警告】
|
||
|
* @note 【重大修改】
|
||
|
*************************************************************************************/
|
||
|
void bsp_AIN1_OFF(void)
|
||
|
{
|
||
|
GPIO_ResetBits(A_IN1_GPIO_PORT, A_IN1_GPIO_PIN);
|
||
|
}
|
||
|
/*************************************************************************************
|
||
|
* @brief A IN2 开
|
||
|
*
|
||
|
* @warning 【不可重入,阻塞等警告】
|
||
|
* @note 【重大修改】
|
||
|
*************************************************************************************/
|
||
|
void bsp_AIN2_ON(void)
|
||
|
{
|
||
|
GPIO_SetBits(A_IN2_GPIO_PORT, A_IN2_GPIO_PIN);
|
||
|
}
|
||
|
/*************************************************************************************
|
||
|
* @brief A IN2 关
|
||
|
*
|
||
|
* @warning 【不可重入,阻塞等警告】
|
||
|
* @note 【重大修改】
|
||
|
*************************************************************************************/
|
||
|
void bsp_AIN2_OFF(void)
|
||
|
{
|
||
|
GPIO_ResetBits(A_IN2_GPIO_PORT, A_IN2_GPIO_PIN);
|
||
|
}
|
||
|
/*************************************************************************************
|
||
|
* @brief B IN1 开
|
||
|
*
|
||
|
* @warning 【不可重入,阻塞等警告】
|
||
|
* @note 【重大修改】
|
||
|
*************************************************************************************/
|
||
|
void bsp_BIN1_ON(void)
|
||
|
{
|
||
|
GPIO_SetBits(B_IN1_GPIO_PORT, B_IN1_GPIO_PIN);
|
||
|
}
|
||
|
/*************************************************************************************
|
||
|
* @brief B IN1 关
|
||
|
*
|
||
|
* @warning 【不可重入,阻塞等警告】
|
||
|
* @note 【重大修改】
|
||
|
*************************************************************************************/
|
||
|
void bsp_BIN1_OFF(void)
|
||
|
{
|
||
|
GPIO_ResetBits(B_IN1_GPIO_PORT, B_IN1_GPIO_PIN);
|
||
|
}
|
||
|
/*************************************************************************************
|
||
|
* @brief B IN2 开
|
||
|
*
|
||
|
* @warning 【不可重入,阻塞等警告】
|
||
|
* @note 【重大修改】
|
||
|
*************************************************************************************/
|
||
|
void bsp_BIN2_ON(void)
|
||
|
{
|
||
|
GPIO_SetBits(B_IN2_GPIO_PORT, B_IN2_GPIO_PIN);
|
||
|
}
|
||
|
/*************************************************************************************
|
||
|
* @brief B IN2 关
|
||
|
*
|
||
|
* @warning 【不可重入,阻塞等警告】
|
||
|
* @note 【重大修改】
|
||
|
*************************************************************************************/
|
||
|
void bsp_BIN2_OFF(void)
|
||
|
{
|
||
|
GPIO_ResetBits(B_IN2_GPIO_PORT, B_IN2_GPIO_PIN);
|
||
|
}
|
||
|
/*************************************************************************************
|
||
|
* @brief PWM 通道3 设置pwm
|
||
|
* @param[in/out] Compare【参数注释】
|
||
|
*
|
||
|
* @warning 【不可重入,阻塞等警告】
|
||
|
* @note 【重大修改】
|
||
|
*************************************************************************************/
|
||
|
void PWM_SetCompare3(uint16_t Compare)
|
||
|
{
|
||
|
TIM_SetCompare3(TIM_MOTOR, Compare);
|
||
|
}
|
||
|
|