stm32库函数版本tb6612控制两个电机
时间: 2023-11-29 08:04:35 浏览: 139
以下是使用STM32库函数控制两个电机的示例代码:
```c
#include "stm32f10x.h"
#define MOTOR1_IN1_PIN GPIO_Pin_0
#define MOTOR1_IN2_PIN GPIO_Pin_1
#define MOTOR1_PWM_PIN GPIO_Pin_6
#define MOTOR2_IN1_PIN GPIO_Pin_2
#define MOTOR2_IN2_PIN GPIO_Pin_3
#define MOTOR2_PWM_PIN GPIO_Pin_7
void motor_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// Enable GPIO clocks
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
// Configure motor control pins as outputs
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = MOTOR1_IN1_PIN | MOTOR1_IN2_PIN | MOTOR2_IN1_PIN | MOTOR2_IN2_PIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure PWM pins as alternate function outputs
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = MOTOR1_PWM_PIN | MOTOR2_PWM_PIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// Enable TIM4 clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
// Configure TIM4 as PWM timer
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; // 1 MHz PWM frequency
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // 1 kHz PWM frequency
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
// Configure PWM output channels
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
// Enable TIM4 outputs
TIM_CtrlPWMOutputs(TIM4, ENABLE);
// Start TIM4 timer
TIM_Cmd(TIM4, ENABLE);
}
void motor_set_speed(uint8_t motor, int16_t speed)
{
uint16_t pwm_val = abs(speed);
// Limit speed to maximum value
if (pwm_val > 1000)
{
pwm_val = 1000;
}
// Set PWM output channels based on motor number and direction
if (speed > 0)
{
if (motor == 1)
{
GPIO_SetBits(GPIOA, MOTOR1_IN1_PIN);
GPIO_ResetBits(GPIOA, MOTOR1_IN2_PIN);
TIM_SetCompare1(TIM4, pwm_val);
}
else if (motor == 2)
{
GPIO_SetBits(GPIOA, MOTOR2_IN1_PIN);
GPIO_ResetBits(GPIOA, MOTOR2_IN2_PIN);
TIM_SetCompare2(TIM4, pwm_val);
}
}
else if (speed < 0)
{
if (motor == 1)
{
GPIO_ResetBits(GPIOA, MOTOR1_IN1_PIN);
GPIO_SetBits(GPIOA, MOTOR1_IN2_PIN);
TIM_SetCompare1(TIM4, pwm_val);
}
else if (motor == 2)
{
GPIO_ResetBits(GPIOA, MOTOR2_IN1_PIN);
GPIO_SetBits(GPIOA, MOTOR2_IN2_PIN);
TIM_SetCompare2(TIM4, pwm_val);
}
}
else
{
if (motor == 1)
{
GPIO_ResetBits(GPIOA, MOTOR1_IN1_PIN);
GPIO_ResetBits(GPIOA, MOTOR1_IN2_PIN);
TIM_SetCompare1(TIM4, 0);
}
else if (motor == 2)
{
GPIO_ResetBits(GPIOA, MOTOR2_IN1_PIN);
GPIO_ResetBits(GPIOA, MOTOR2_IN2_PIN);
TIM_SetCompare2(TIM4, 0);
}
}
}
int main(void)
{
motor_init();
while (1)
{
// Set motor 1 to full speed forward
motor_set_speed(1, 1000);
// Set motor 2 to half speed reverse
motor_set_speed(2, -500);
}
}
```
阅读全文