STM32 避障小车 —— L298N

本文介绍制作过程中用到的 L298N 模块

模块介绍

大家拿到模块后可以看到该模块有许多接口,下面介绍下各接口的作用。
STM32 避障小车 —— L298N_第1张图片

  1. 动力电源正极:7 - 45V 供电
  2. 公共接地:这个地方注意一定要与开发板共地,否则会造成模块工作不正常
  3. 信号电源正极:当图中左侧跳帽连接时为 5V 输出,可用于单片机供电(需连接 3.3V 电源稳压模块)
  4. 控制信号输入(IN1,IN2,IN3,IN4):连接 TIM3_CH1-CH4 通过 PWM 控制电机转速
  5. OUT1-OUT4:OUT1,OUT2 接马达 A | OUT3,OUT4 接马达 B
STM32 避障小车 —— L298N_第2张图片

表格出处

采用 PWM 调整占空比来调整电机的转动速度,注意一定要给足够的电压!

与单片机连接

我们使用 TIM3 来输出 PWM,故将 IN1 - IN4 连接到 TIM3_CH1 - CH4 控制电机转动。
STM32 避障小车 —— L298N_第3张图片

IN1 -> PA6
IN2 -> PA7
IN3 -> PB0
IN4 -> PB1

程序部分

简易测试程序,可用以测试驱动板是否正常工作

void car_go(void);
void car_left(void);

int main() {

        GPIO_InitTypeDef  GPIO_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA,ENABLE);//使能GPIO的外设时钟

        GPIO_InitStructure.GPIO_Pin =GPIO_Pin_6|GPIO_Pin_7;//选择要用的GPIO引脚
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //设置引脚模式为推免输出模式                         
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//设置引脚速度为50MHZ         
        GPIO_Init(GPIOA,&GPIO_InitStructure);//调用库函数,初始化GPIO

        GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0|GPIO_Pin_1;//选择要用的GPIO引脚
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //设置引脚模式为推免输出模式                
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//设置引脚速度为50MHZ         
        GPIO_Init(GPIOB,&GPIO_InitStructure);//调用库函数,初始化GPIO

        car_go();//前进

}


void car_go(void) {  //前进

        GPIO_SetBits(GPIOA,GPIO_Pin_6);   //控制左边轮胎
        GPIO_ResetBits(GPIOA,GPIO_Pin_7);

        GPIO_SetBits(GPIOB,GPIO_Pin_0);    //控制右边轮胎
        GPIO_ResetBits(GPIOB,GPIO_Pin_1);

}

void car_left(void) {  //左转

        GPIO_SetBits(GPIOA,GPIO_Pin_6);   //控制左边轮胎
        GPIO_ResetBits(GPIOA,GPIO_Pin_7);

        GPIO_ResetBits(GPIOB,GPIO_Pin_0); //控制右边轮胎
        GPIO_SetBits(GPIOB,GPIO_Pin_1);   

}

正式程序,PWM 调速控制

L298N.h

#ifndef __L298N_H
#define __L298N_H

#include 

void TIM3_PWM_Init(void);
void Go_Forward(void);
void Go_Back(void);
void Stop(void);
void Turn_Left(void);
void Turn_Right(void);

#endif /*__L298N_H*/

L298N.c

#include 

void TIM3_PWM_Init(void) {

        GPIO_InitTypeDef GPIO_InitStructer; 
        TIM_TimeBaseInitTypeDef TIM_TimeBaseStructer;
        TIM_OCInitTypeDef TIM_OCInitStructer; 

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);

        //初始化TIM3
        TIM_TimeBaseStructer.TIM_Period = 899;
        TIM_TimeBaseStructer.TIM_Prescaler = 0;
        TIM_TimeBaseStructer.TIM_CounterMode = TIM_CounterMode_Up;
        TIM_TimeBaseStructer.TIM_ClockDivision = 0;
        TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructer);

        //初始化GPIOA6/GPIOA7  (TIM3_CH1/TIM3_CH2)
        GPIO_InitStructer.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;   
        GPIO_InitStructer.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructer.GPIO_Mode = GPIO_Mode_AF_PP;  
        GPIO_Init(GPIOA, &GPIO_InitStructer);   

        //初始化GPIOB0/GPIOB1  (TIM3_CH3/TIM3_CH4)
        GPIO_InitStructer.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;  
        GPIO_InitStructer.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructer.GPIO_Mode = GPIO_Mode_AF_PP;  
        GPIO_Init(GPIOB, &GPIO_InitStructer); 

        //PWM通道一
        TIM_OCInitStructer.TIM_OCMode = TIM_OCMode_PWM2;
        TIM_OCInitStructer.TIM_OutputState = TIM_OutputState_Enable;  /*比较输出使能,决定信号是否通过外部引脚输出。value:0(Disable)、1(Enable)。*/
        TIM_OCInitStructer.TIM_OCPolarity = TIM_OCPolarity_High;  /*比较输出极性,决定定时器通道有效电平的极性。*/
        TIM_OCInitStructer.TIM_Pulse = 900;
        TIM_OC1Init(TIM3, &TIM_OCInitStructer);
        //Enables or disables the TIMx peripheral Preload register on CCR1.
        TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);

        //PWM通道二
        TIM_OCInitStructer.TIM_OCMode = TIM_OCMode_PWM2;
        TIM_OCInitStructer.TIM_OutputState = TIM_OutputState_Enable;  /*比较输出使能,决定信号是否通过外部引脚输出。value:0(Disable)、1(Enable)。*/
        TIM_OCInitStructer.TIM_OCPolarity = TIM_OCPolarity_High;  /*比较输出极性,决定定时器通道有效电平的极性。*/
        TIM_OCInitStructer.TIM_Pulse = 900;
        TIM_OC2Init(TIM3, &TIM_OCInitStructer);
        TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);

        //PWM通道三
        TIM_OCInitStructer.TIM_OCMode = TIM_OCMode_PWM2;
        TIM_OCInitStructer.TIM_OutputState = TIM_OutputState_Enable;  /*比较输出使能,决定信号是否通过外部引脚输出。value:0(Disable)、1(Enable)。*/
        TIM_OCInitStructer.TIM_OCPolarity = TIM_OCPolarity_High;  /*比较输出极性,决定定时器通道有效电平的极性。*/
        TIM_OCInitStructer.TIM_Pulse = 900;
        TIM_OC3Init(TIM3, &TIM_OCInitStructer);
        TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);

        //PWM通道四
        TIM_OCInitStructer.TIM_OCMode = TIM_OCMode_PWM2;
        TIM_OCInitStructer.TIM_OutputState = TIM_OutputState_Enable;  /*比较输出使能,决定信号是否通过外部引脚输出。value:0(Disable)、1(Enable)。*/
        TIM_OCInitStructer.TIM_OCPolarity = TIM_OCPolarity_High;  /*比较输出极性,决定定时器通道有效电平的极性。*/
        TIM_OCInitStructer.TIM_Pulse = 900;
        TIM_OC4Init(TIM3, &TIM_OCInitStructer);
        TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);

        TIM_Cmd(TIM3, ENABLE);
}

//前进
void Go_Forward(void) {

        //Sets the TIMx Capture Compare1 Register value
        TIM_SetCompare1(TIM3, 400);
        TIM_SetCompare2(TIM3, 900);
        TIM_SetCompare3(TIM3, 400);
        TIM_SetCompare4(TIM3, 900);

}

//停止
void Stop(void) {

        TIM_SetCompare1(TIM3, 900);
        TIM_SetCompare2(TIM3, 900);
        TIM_SetCompare3(TIM3, 900);
        TIM_SetCompare4(TIM3, 900);

}

//后退
void Go_Back(void) {

        TIM_SetCompare1(TIM3, 900);
        TIM_SetCompare2(TIM3, 400);
        TIM_SetCompare3(TIM3, 900);
        TIM_SetCompare4(TIM3, 400);

}

//左转
void Turn_Left(void) {

        TIM_SetCompare1(TIM3, 900);
        TIM_SetCompare2(TIM3, 300);
        TIM_SetCompare3(TIM3, 300);
        TIM_SetCompare4(TIM3, 900);

}

//右转
void Turn_Right(void) {

        TIM_SetCompare1(TIM3, 300);
        TIM_SetCompare2(TIM3, 900);
        TIM_SetCompare3(TIM3, 900);
        TIM_SetCompare4(TIM3, 300);

}

文章相关

上一篇:STM32 避障小车 —— 前期准备
下一篇:STM32 避障小车 —— HC-SR04

你可能感兴趣的:(STM32)