A4950电机驱动模块特性
电机A的两个线分别接AOUT1、AOUT2,电机B的两个线分别接BOUT1、BOUT2。控制电机时,直接将PB6、PB7(PB8、PB9)接不同占空比的PWM。由于AIN1和AIN2(BIN1和BIN2)之间存在压差,电机可以转动,并且压差越大,转速越快。凭此我们通过调节PWM控制小车速度以及方向。
这里我们使用TIM4的1、2、3、4通道
timer.h
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
void pwm_init(u16 arr,u16 psc);
#endif
timer.c
#include "timer.h"
void pwm_init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_Initure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM4_CHxHandler;//定时器4通道x句柄
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_Initure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;//配置IO
GPIO_Initure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Initure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_Initure);
GPIO_Initure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9;
GPIO_Init(GPIOB,&GPIO_Initure);
TIM_TimeBaseStructure.TIM_Period = arr;//配置定时器
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM4_CHxHandler.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
TIM4_CHxHandler.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM4_CHxHandler.TIM_OCPolarity = TIM_OCPolarity_High; //输出比较极性高
TIM_OC1Init(TIM4, &TIM4_CHxHandler); //配置pwm通道
TIM_OC2Init(TIM4, &TIM4_CHxHandler);
TIM_OC3Init(TIM4, &TIM4_CHxHandler);
TIM_OC4Init(TIM4, &TIM4_CHxHandler);
TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);//预装载使能
TIM_OC2PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIM_Cmd(TIM4,ENABLE); //使能定时器
}
motor.h
#ifndef __MOTOR_H
#define __MOTOR_H
#include "sys.h"
void motor_forth(void);
void motor_back(void);
void motor_left(void);
void motor_right(void);
void motor_stop(void);
#endif
motor.c
#include "motor.h"
#include "delay.h"
//TIM_SetCompare1,2对应左边电机,TIM_SetCompare3,4对应右边电机
void motor_forth()//前行
{
TIM_SetCompare1(TIM4,599);
TIM_SetCompare2(TIM4,899);
TIM_SetCompare3(TIM4,593);
TIM_SetCompare4(TIM4,899);
}
void motor_back()//后退
{
TIM_SetCompare1(TIM4,899);
TIM_SetCompare2(TIM4,700);
TIM_SetCompare3(TIM4,899);
TIM_SetCompare4(TIM4,700);
}
void motor_left()//左转
{
TIM_SetCompare1(TIM4,899);
TIM_SetCompare2(TIM4,700);
TIM_SetCompare3(TIM4,750);
TIM_SetCompare4(TIM4,899);
delay_ms(565);
motor_stop();
}
void motor_right()//右转
{
TIM_SetCompare1(TIM4,750);
TIM_SetCompare2(TIM4,899);
TIM_SetCompare3(TIM4,899);
TIM_SetCompare4(TIM4,700);
delay_ms(593);
motor_stop();
}
void motor_stop()
{
TIM_SetCompare1(TIM4,899);
TIM_SetCompare2(TIM4,899);
TIM_SetCompare3(TIM4,899);
TIM_SetCompare4(TIM4,899);
}
下面是裸机跑的主函数————(私奔?裸奔??)
main.c
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "motor.h"
#include "timer.h"
/************************************************************************/
int main(void)
{
SystemInit();
delay_init(); //延时函数初始化 nms<=1864
LED_Init(); //初始化与LED连接的硬件接口
pwm_init(899,0);
motor_stop();
delay_ms(1800);
delay_ms(1800);
while(1)
{
LED=0;
motor_forth();
delay_ms(1500);
LED=1;
motor_stop();
delay_ms(1500);
LED=0;
motor_left();
delay_ms(900);
LED=1;
motor_stop();
delay_ms(1500);
LED=0;
motor_forth();
delay_ms(1000);
LED=1;
motor_stop();
delay_ms(1500);
LED=0;
motor_right();
delay_ms(900);
LED=1;
motor_stop();
delay_ms(1500);
}
}
主函数功能是测试电机运作是否正常,以及调试出合适的PWM输出。