一、硬件要求
STM32F407ZGT6+L298N+两个电机+12V电源
定义了6个GPIO,4个作为逻辑输入(正反转控制) 2两个作为使能端
IN0 IN1 连接 D0 D1,作为电机A的逻辑输入
IN2 IN3 连接 B5 B6,作为电机B的逻辑输入
PF8 为电机A的使能端 PF 7为电机B的使能端
实物链接图
二、代码部分
完整工程文件:
Main.c
#include "stm32f4xx.h"
#include "delay.h"
#include "key.h"
#include "led.h"
#include "pwm.h"
#include "Motor.h"
//STM32F407ZGT6——L298N
//EN_A---PF6
//IN0----PD0
//IN1----PD1
//EN_B---PF7
//IN2----PB5
//IN3----PB6
int main(void)
{
u8 key;
u16 Motor1pwmval=499;
u16 Motor2pwmval=499;
u8 Level1 = 0;
u8 Level2 = 0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组 2
delay_init(168); //初始化延时函数
MOTOR_Init();
LED_Init(); //初始化 LED 端口
KEY_Init();
TIM13_PWM_Init(500-1,84-1); //84M/84=1Mhz的计数频率,重装载值500,所以PWM频率为 1M/500=2Khz.
TIM11_PWM_Init(500-1,84-1);
LED1 = 1;LED0 = 1;
IN0 = 0;IN1 = 1;ENABLE_A = 1;
IN2 = 0;IN3 = 1;ENABLE_B = 1;
while(1)
{
key=KEY_Scan(0);
if(key)
{
if(key==1)//风速
{
Level1++;if(Level1==4)Level1 = 0;
}
else if(key==4)
{
Level2++;if(Level2==4)Level2 = 0;
}
}
switch(Level1)
{
case 0:Motor1pwmval = 499;LED0 = 1;
break;
case 1:Motor1pwmval = 250;LED0 = 0;
break;
case 2:Motor1pwmval = 100;LED0 = 0;
break;
case 3:Motor1pwmval = 0;LED0 = 0;
break;
}
switch(Level2)
{
case 0:Motor2pwmval = 499;LED1 = 1;
break;
case 1:Motor2pwmval = 250;LED1 = 0;
break;
case 2:Motor2pwmval = 100;LED1 = 0;
break;
case 3:Motor2pwmval = 0;LED1 = 0;
break;
}
TIM_SetCompare1(TIM13,Motor1pwmval); //修改比较值,修改占空比
TIM_SetCompare1(TIM11,Motor2pwmval); //修改比较值,修改占空比
}
}
Motor.c
#include "Motor.h"
//初始化PB12和PB13为输出口.并使能这两个口的时钟
//MOTOR IN1 IN2 IO初始化
void MOTOR_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //推挽输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //推挽输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
// GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //推挽输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //推挽输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
}