您现在的位置是:首页 >技术交流 >基于STM32F103的PWM电机驱动A4950网站首页技术交流

基于STM32F103的PWM电机驱动A4950

变优秀吧 2023-05-29 04:00:02
简介基于STM32F103的PWM电机驱动A4950

1.基本介绍

A4950的峰值输出电流可达±3.5 A,工作电压为7.6~40v。

优点:相对于L298N模块控制2个电机正反转仅需要4个I/O口,体积小,发热低。

缺点:需要2个PWM引脚才能控制正反转,所以控制2个电机需要4PWM引脚,会使用STM32F103芯片一个定时器的全部PWM通道。

A4950模块是靠输入2路的PWM占空比的差值,来设定输出电压的大小,当差值为0时输出为0,反之当占空比差值为100时输出最大。

2.引脚功能和接线

引脚名称输入输出接线功能
AOUT1    输出        接A电机正输出电压控制电机
AOUT2    输出        接A电机负输出电压控制电机
BOUT1    输出        接B电机正输出电压控制电机
BOUT2    输出        接B电机负输出电压控制电机
AIN1    输入          接STM32 A7控制电机输出电压
AIN2    输入          接STM32 B1控制电机输出电压
BIN1    输入          接STM32 A6控制电机输出电压
BIN2    输入          接STM32 B0控制电机输出电压
VCC    输入          接12V电源总电源
VM    输入          接5V电源给内部逻辑电路供电
GND    输出         接电源地接地

3程序设计

接下来我使用STM32F103C8T6的标准库代码为例

定时器3初始化代码

#include "stm32f10x.h"                  // Device header

void PWM_Init3(void)
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

	GPIO_InitTypeDef GPIO_InitStructure1;
	GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
	GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure1);

	GPIO_InitTypeDef GPIO_InitStructure2;
	GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
	GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure2);

	TIM_InternalClockConfig(TIM3);
	
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	
	TIM_TimeBaseInitStructure.TIM_Period  = 100-1;  //ARR
	TIM_TimeBaseInitStructure.TIM_Prescaler  = 3600-1; //PSC
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
	
	TIM_OCInitTypeDef 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_OC1Init(TIM3,&TIM_OCInitStructure);
	TIM_OC2Init(TIM3,&TIM_OCInitStructure);
	TIM_OC4Init(TIM3,&TIM_OCInitStructure);
    TIM_OC3Init(TIM3,&TIM_OCInitStructure);

	TIM_ClearFlag(TIM3, TIM_FLAG_Update);
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	NVIC_Init(&NVIC_InitStructure);
	
	TIM_Cmd(TIM3,ENABLE);
 }

 
 
void PWM_SetCompare1(uint16_t Compare)
 {
	 TIM_SetCompare1 (TIM3,Compare);
 }
  
void PWM_SetCompare2(uint16_t Compare)
 {
	 TIM_SetCompare2 (TIM3,Compare);
 }
 
void PWM_SetCompare3(uint16_t Compare)
 {
	 TIM_SetCompare3 (TIM3,Compare);
 }

void PWM_SetCompare4(uint16_t Compare)
 {
	 TIM_SetCompare4 (TIM3,Compare);
 }

电机控制函数

#include "stm32f10x.h"                  // Device header
#include "PWN.h"
void Motor_Init(void)
{
	PWM_Init3();
}
// you
void Motor_SetSpeed2(int16_t Speed)
{
	
		PWM_SetCompare3(50-Speed/2);
		PWM_SetCompare1(50+Speed/2);

	
}
//zuo
void Motor_SetSpeed1(int16_t Speed)
{
	
		PWM_SetCompare2(50-Speed/2);
		PWM_SetCompare4(50+Speed/2);

}

 mian函数代码

#include "stm32f10x.h"                  // Device header
#include "Motor.h"
int main (void)
{
	Motor_Init(); //电机驱动初始化
	while(1)
	{
		
		Motor_SetSpeed1(20);//给电机1 向前20速度
		Motor_SetSpeed2(20);//给电机2 向前20速度
		
	}

}

全部源码链接:主控/C8T6/A4950 · 亟待/electronic-game-code - 码云 - 开源中国 (gitee.com)https://gitee.com/AD123zsg/electronic-game-code/tree/master/%E4%B8%BB%E6%8E%A7/C8T6/A4950

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。