《电子DIY》之使用STM32驱动电机(PWM方式)(控制电机正反转)(用于智能小车轮子的控制)

L298N介绍:
驱动芯片相关介绍

程序源码:

#include "motor.h"
void Motor_Init(u16 period,u16 prescaler)
{
	GPIO_InitTypeDef GPIO_InitStruct;
#if Motor_PWM_Mode
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef TIM_OCInitStruct;
#endif
	
	RCC_APB2PeriphClockCmd(MOTORPORTClock,ENABLE);
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin=MOTOR_PIN_IN1|MOTOR_PIN_IN2|MOTOR_PIN_IN3|MOTOR_PIN_IN4;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(MOTORPORT,&GPIO_InitStruct);
#if Motor_PWM_Mode
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	
	RCC_APB2PeriphClockCmd(MOTORPORTClock,ENABLE);
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIO_InitStruct.GPIO_Pin=MOTOR_PIN_EN1|MOTOR_PIN_EN2;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(MOTORPORT,&GPIO_InitStruct);
	
	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period=period;
	TIM_TimeBaseInitStruct.TIM_Prescaler=prescaler;
	TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStruct);

	TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM2;;
	TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;
	TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;
	TIM_OC1Init(TIM4,&TIM_OCInitStruct);
    TIM_OC2Init(TIM4,&TIM_OCInitStruct);
	
	TIM_ARRPreloadConfig(TIM4,ENABLE);
	TIM_SetCompare1(TIM4,period/2);
	TIM_SetCompare2(TIM4,period/2);
	
	TIM_Cmd(TIM4,ENABLE);
	
#else
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin=MOTOR_PIN_EN1|MOTOR_PIN_EN2;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(MOTORPORT,&GPIO_InitStruct);
	Left_EN=Right_EN=0;	
#endif
	Right_IN1=Right_IN2=Left_IN3=Left_IN4=0;
}

void Motor_Stop(void)
{
	Right_IN1=Right_IN2=Left_IN3=Left_IN4=0;
	
#if Motor_PWM_Mode==0
	Left_EN=Right_EN=0;	
#endif
}

void Motor_Start(void)
{
#if Motor_PWM_Mode==0
	Left_EN=Right_EN=1;	
#endif
	Right_IN1=1;
	Right_IN2=0;
	Left_IN3=1;
	Left_IN4=0;
}

void Motor_Forward(void)
{
	Right_IN1=1;
	Right_IN2=0;
	Left_IN3=1;
	Left_IN4=0;
}

void Motor_Reverse(void)
{
	Right_IN1=0;
	Right_IN2=1;
	Left_IN3=0;
	Left_IN4=1;
}

void Motor_Right(void)
{
	Right_IN1=0;
	Right_IN2=1;
	Left_IN3=1;
	Left_IN4=0;
}

void Motor_Left(void)
{
	Right_IN1=1;
	Right_IN2=0;
	Left_IN3=0;
	Left_IN4=1;
}

void Motor_SetPWM(u16 right,u16 left)
{
	TIM_SetCompare1(TIM4,right);
	TIM_SetCompare2(TIM4,left);
}






#ifndef __MOTOR_H
#define __MOTOR_H
#include "sys.h"
#define MOTORPORT GPIOB
#define MOTORPORTClock  RCC_APB2Periph_GPIOB 
#define Motor_PWM_Mode  1

#define MOTOR_PIN_IN1 GPIO_Pin_4
#define MOTOR_PIN_IN2 GPIO_Pin_5
#define MOTOR_PIN_IN3 GPIO_Pin_8
#define MOTOR_PIN_IN4 GPIO_Pin_9
#define MOTOR_PIN_EN1 GPIO_Pin_6
#define MOTOR_PIN_EN2 GPIO_Pin_7

#define Right_IN1 PBout(4)
#define Right_IN2 PBout(5)
#define Left_IN3  PBout(8)
#define Left_IN4  PBout(9)
#define Right_EN  PBout(6)
#define Left_EN   PBout(7)

void Motor_Init(u16 period,u16 prescaler);
void Motor_Stop(void);
void Motor_Start(void);
void Motor_Forward(void);
void Motor_Reverse(void);
void Motor_Right(void);
void Motor_Left(void);
void Motor_SetPWM(u16 right,u16 left);
#endif 

你可能感兴趣的:(《电子DIY》,智能小车)