一、L298N电机驱动板
电源引脚
VCC 外接直流电源引脚,电压范围在5~35V之间 GND GND是接地引脚,连接到电源负极 5V 驱动芯片内部逻辑供电引脚,如果安装了5V跳帽,则此引脚可输出5V电压,为微控板或其他电路提供电力供给,如果拔掉5V跳帽,则需要独立外接5V电源
控制引脚
-
IN1 & IN2 电机驱动器A的输入引脚,控制电机A转动及旋转角度 IN1输入高电平HIGH,IN2输入低电平LOW,对应电机A正转 IN1输入低电平LOW,IN2输入高电平HIGH,对应电机A反转 IN1、IN2同时输入高电平HIGH或低电平LOW,对应电机A停止转动 调速就是改变IN1、IN2高电平的占空比(需拔掉ENA处跳帽) -
IN3 & IN4 电机驱动器B的输入引脚,控制电机B转动及旋转角度 IN3输入高电平HIGH,IN4输入低电平LOW,对应电机B正转 IN3输入低电平LOW,IN4输入高电平HIGH,对应电机B反转 IN3、IN4同时输入高电平HIGH或低电平LOW,对应电机B停止转动 调速就是改变IN3、IN4高电平的占空比(需拔掉ENB处跳帽)
输出引脚
- OUT1 & OUT2 电机驱动器A的输出引脚,接直流电机A或步进电机的A+和A-
- OUT3 & OUT3 电机驱动器B的输出引脚,接直流电机B或步进电机的B+和B-
调速控制引脚
- ENA 电机A调速开关引脚,拔掉跳帽,使用PWM对电机A调速,插上电机A高速运行
- ENB 电机B调速开关引脚,拔掉跳帽,使用PWM对电机B调速,插上电机B高速运行
OUT1、OUT2和OUT3、OUT4之间分别接两个直流电机Motor1、Motor2,IN1、IN2、IN3、IN4引脚从单片机接入控制电平,控制电机的正反转,ENA、ENB接控制使能端,控制电机调速,L298N控制逻辑关系图如下:
二、STM32的PWM输出
1、通用定时器(TIM2 ~ TIM5) 每个定时都有 4个独立通道作为输出 2、高级控制定时器(TIM1 & TIM8) 该款定时器可产生7路 PWM输出: 3.STM32F1的TIM3输出引脚 本文用到STM32F1的TIM3的通道1~4,下面对TIM3的引脚作简要介绍: 本实验选择没有重映像的四个引脚,即PA6、PA7、PB0、PB1(有无重映像会在TIM3初始化部分的代码有所体现)
三、硬件连接
本设计用到两个L298N驱动板,四个小黄电机,一块STM32F103ZET6开发板 第一块L298N接左前电机和左后电机: ENA—PA6,ENB—PA7 左前电机:IN1—PB7,IN2—PB6 左后电机:IN3—PB9,IN4—PB2
第二块L298N接右前电机和右后电机: ENA—PB0,ENB—PB1 左前电机:IN1—PB13,IN2—PB12 左后电机:IN3—PB15,IN4—PB14
四、主要代码
timer.c
#include "timer.h"
#include "led.h"
#include "usart.h"
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE );
TIM_Cmd(TIM3, ENABLE);
}
void TIM3_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 |GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3,ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
timer.h
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
void TIM3_Int_Init(u16 arr,u16 psc);
void TIM3_PWM_Init(u16 arr,u16 psc);
#endif
MTR_GPIO.c(电机驱动部分)
#include "MTR_GPIO.h"
void MTR_CarBrakeAll(void){
MTR1_BRAKE;
MTR2_BRAKE;
MTR3_BRAKE;
MTR4_BRAKE;
}
void MTR_CarRight(void){
MTR1_CW;
MTR2_CW;
MTR3_CCW;
MTR4_CCW;
}
void MTR_CarLeft(void){
MTR1_CCW;
MTR2_CCW;
MTR3_CW;
MTR4_CW;
}
void MTR_CarBack(void){
MTR1_CCW;
MTR2_CCW;
MTR3_CCW;
MTR4_CCW;
}
void MTR_CarForward(void){
MTR1_CW;
MTR2_CW;
MTR3_CW;
MTR4_CW;
}
void MTR_GPIOInit(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(MTR1_GPIO_CLK|MTR2_GPIO_CLK|MTR3_GPIO_CLK|MTR4_GPIO_CLK,ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = MTR1_GPIO_PIN;
GPIO_Init(MTR1_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = MTR2_GPIO_PIN;
GPIO_Init(MTR2_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = MTR3_GPIO_PIN;
GPIO_Init(MTR3_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = MTR4_GPIO_PIN;
GPIO_Init(MTR4_GPIO_PORT, &GPIO_InitStructure);
}
MTR_GPIO.h
#ifndef __MTR_GPIO_H
#define __MTR_GPIO_H
#include "stm32f10x.h"
#define MTR1_GPIO_PORT GPIOB
#define MTR1_GPIO_CLK RCC_APB2Periph_GPIOB
#define MTR1_GPIO_PIN GPIO_Pin_6|GPIO_Pin_7
#define MTR1_CW {GPIO_ResetBits(MTR1_GPIO_PORT,GPIO_Pin_6);GPIO_SetBits(MTR1_GPIO_PORT,GPIO_Pin_7);}
#define MTR1_CCW {GPIO_SetBits(MTR1_GPIO_PORT,GPIO_Pin_6);GPIO_ResetBits(MTR1_GPIO_PORT,GPIO_Pin_7);}
#define MTR1_BRAKE GPIO_ResetBits(MTR1_GPIO_PORT,MTR1_GPIO_PIN);
#define MTR2_GPIO_PORT GPIOB
#define MTR2_GPIO_CLK RCC_APB2Periph_GPIOB
#define MTR2_GPIO_PIN GPIO_Pin_2|GPIO_Pin_9
#define MTR2_CW {GPIO_ResetBits(MTR2_GPIO_PORT,GPIO_Pin_2);GPIO_SetBits(MTR2_GPIO_PORT,GPIO_Pin_9);}
#define MTR2_CCW {GPIO_SetBits(MTR2_GPIO_PORT,GPIO_Pin_2);GPIO_ResetBits(MTR2_GPIO_PORT,GPIO_Pin_9);}
#define MTR2_BRAKE GPIO_ResetBits(MTR2_GPIO_PORT,MTR2_GPIO_PIN);
#define MTR3_GPIO_PORT GPIOB
#define MTR3_GPIO_CLK RCC_APB2Periph_GPIOB
#define MTR3_GPIO_PIN GPIO_Pin_12|GPIO_Pin_13
#define MTR3_CW {GPIO_ResetBits(MTR3_GPIO_PORT,GPIO_Pin_12);GPIO_SetBits(MTR3_GPIO_PORT,GPIO_Pin_13);}
#define MTR3_CCW {GPIO_SetBits(MTR3_GPIO_PORT,GPIO_Pin_12);GPIO_ResetBits(MTR3_GPIO_PORT,GPIO_Pin_13);}
#define MTR3_BRAKE GPIO_ResetBits(MTR3_GPIO_PORT,MTR3_GPIO_PIN);
#define MTR4_GPIO_PORT GPIOB
#define MTR4_GPIO_CLK RCC_APB2Periph_GPIOB
#define MTR4_GPIO_PIN GPIO_Pin_14|GPIO_Pin_15
#define MTR4_CW {GPIO_ResetBits(MTR4_GPIO_PORT,GPIO_Pin_14);GPIO_SetBits(MTR4_GPIO_PORT,GPIO_Pin_15);}
#define MTR4_CCW {GPIO_SetBits(MTR4_GPIO_PORT,GPIO_Pin_14);GPIO_ResetBits(MTR4_GPIO_PORT,GPIO_Pin_15);}
#define MTR4_BRAKE GPIO_ResetBits(MTR4_GPIO_PORT,MTR4_GPIO_PIN);
void MTR_CarBrakeAll(void);
void MTR_CarRight(void);
void MTR_CarLeft(void);
void MTR_CarBack(void);
void MTR_CarForward(void);
void MTR_GPIOInit(void);
#endif
main.c
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "timer.h"
#include "MTR_GPIO.h"
int main(void)
{
u16 led0pwmval=0;
u8 dir=1;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
TIM3_PWM_Init(899,0);
MTR_GPIOInit();
while(1)
{
delay_ms(10);
if(dir)led0pwmval++;
else led0pwmval--;
MTR_CarForward();
if(led0pwmval>800)dir=0;
if(led0pwmval==0)dir=1;
TIM_SetCompare1(TIM3,led0pwmval);
TIM_SetCompare2(TIM3,led0pwmval);
TIM_SetCompare3(TIM3,led0pwmval);
TIM_SetCompare4(TIM3,led0pwmval);
}
}
|