L298n电机驱动板
7V~12VVCC:接入7V—12V的电源。
GND:接地。注:当单片机与驱动不是同一电源时要供地。
5V输出:可以输出5V的电源。
电机A/B:接入电机。
IN1~IN4:控制电机正反转,转速信号接口。
电机A/B使能:通过跳帽控制是否使能电机。
控制方式
1.直接给高低电平
通过引脚的高低搭配,控制电机的正反转和停止,速度最大不可调。
2.PWM方式控制
通过快速的改变其中一个引脚的高低电平,使电机的开关不停的来回切换从而改变电机的转速。
3.改变电机A/B的使能
去掉A/B电机使能跳帽,另接到单片机引脚上,通过pwm迅速使能失能A/B电机的开关,来改变转速。
代码详解
以电机A为例
IN1=PA1; IN2=PA2; ENA=PA0;
1…第一种控制方式
引脚初始化
void Motor_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1| GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
电机运行函数
void run()
{
GPIO_SetBits(GPIOA, GPIO_Pin_1);
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
}
void back()
{
GPIO_SetBits(GPIOA, GPIO_Pin_2);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}
void stop()
{
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
}
2.第二种控制方式
pwm初始化
void PWM_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_InternalClockConfig(TIM2);
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 = 36 - 1; //PSC
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
TIM_OCInitTypeDef TIM_OCInitStructure;
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);//通道2
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_Pulse = 0; //CCR
TIM_OC3Init(TIM2, &TIM_OCInitStructure);//通道3
TIM_Cmd(TIM2, ENABLE);
}
设置占空比函数
void MOTO_SetCompare2(uint16_t Compare) //PA1 PWM最大100
{
TIM_SetCompare2(TIM2, Compare);
}
void MOTO_SetCompare3(uint16_t Compare)//PA2
{
TIM_SetCompare3(TIM2, Compare);
}
电机运行函数
void moto(int i,int j) //i=0反转 i=1正转 j为转速 最大100
{
if(i==1)
{
MOTO_SetCompare2(J);
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
}
if(i==0)
{
MOTO_SetCompare3(J);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}
}
3.第三种控制方式
ENA 初始化
void ENA_PWMInit(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_InternalClockConfig(TIM2);
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 = 36 - 1; //PSC
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
TIM_OCInitTypeDef TIM_OCInitStructure;
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);
TIM_Cmd(TIM2, ENABLE);
}
引脚初始化
void Motor_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1| GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
设置占空比函数文章来源:https://www.toymoban.com/news/detail-600879.html
void MOTO_SetCompare1(uint16_t Compare) //PA0 PWM最大100
{
TIM_SetCompare1(TIM2, Compare);
}
电机运行函数文章来源地址https://www.toymoban.com/news/detail-600879.html
void moto(int i,int j) //i=0反转 i=1正转 j为转速 最大100
{
if(i==1)
{
MOTO_SetCompare1(J);
GPIO_SetBits(GPIOA, GPIO_Pin_1 );
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
}
if(i==0)
{
MOTO_SetCompare1(J);
GPIO_SetBits(GPIOA, GPIO_Pin_2);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}
}
到了这里,关于L298n电机驱动+stm32+pwm的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!