STM32 study notes -L298N drive module - Motor

Novice, feeling ten days of learning curve is gone a lot, so I plan to learn the knowledge of the record, and share with you, do not hold anything against me, I write from the perspective of a very novice.

1、STM32F103RCT6

I was first learning SCM, choose a mini version of punctual atomic family, learning only later to find out what the board are similar, as long as learn to look at the manual just fine. Recommended forums: CSDN, punctual atomic official website, 51 black and forums . Hands-on video tutorials can be followed by punctuality atom provided the basic experiments do it to learn a more in-depth principle, you can get a multiplier effect. Not because they will not timid, once you do not understand the knowledge to find information immediately, or do not know to ask, well, feel a little bit more.

2, L298N motor drive module

This picture is looking from Taobao
They must not only think Taobao to buy things, but also a lot of information can be found above. Explain:
motor A output (OUT1 and OUT2) : positive and negative wire is connected to the motor, which does not matter what negative positive, reverse it later if you want to change it back again, when you are connected you will find two conducting, this is the knowledge of the motor, if you want to understand the internal structure of the motor, REFER [1] portal .
12V power and ground : 12V power connector connected to a 12V battery positive line and negative line is grounded, but also to the microcontroller to the job on the 12V power supply, as long as not higher than the voltage not less than 16V 8V like La.
5V output : This is because there L298N module comes with a regulator function, it can give out 5v voltage, if he has done after car, powered microcontroller can choose this option, but for beginners should not be used, for the time being ignored.
A channel enable (EN1) : What can be so ...... I just started to learn the do not know, but if you learn a lighting experiment, you know you want to configure IO ports enable clock or something, which is enabled which means that it open working condition. This enabled interface is used to control the PWM input PWM in the end if you do not know what something is, you first understand it as something one can control the motor speed. In other words, if you simply just want to let the motor turn up, can not control the EN1 first, put that hat to cover it, and it is connected to the 5v, that is, it is not enabled. If you later want to use PWM to control, then pulled his hat, the EN pin to the PWM output of the microcontroller.
Microcontroller IO port control input (IN1, IN2) : These two legs are connected to a single-chip IO two ports, if you were to give a high-low, the motor can turn.

So, the rest of the IN3, IN4, EN1, OUT1 and OUT2 have empathy 1,10.

3, software

Give everyone a share on tips Keil5 to find where in the interface edit configuration Click to go
Here Insert Picture Description
to learn the lighting experiments, we know how to configure the pin, and then know how to pin high and low, and create works of attention, c h files and documents with go

没有 PWM 控制.

// 先配置引脚
void Motor_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能PB端口时钟
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13;//端口配置
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      //推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     //50M
  GPIO_Init(GPIOB, &GPIO_InitStructure);		//根据设定参数初始化GPIOB 
  //暂时先把IO口拉低电平,也可以不拉
  GPIO_ResetBits(GPIOB,GPIO_Pin_12|GPIO_Pin_13);
}
//在main函数里
void main(void)
{
	Motor_Init();
	while(1)
	{
		delay_ms(1000);
		GPIO_ResetBits(GPIOB,GPIO_Pin_12);//正转
		GPIO_SetBits(GPIOB,GPIO_Pin_13);
		delay_ms(6000);
		GPIO_ResetBits(GPIOB,GPIO_Pin_13);//反转
		GPIO_SetBits(GPIOB,GPIO_Pin_12);
		delay_ms(1000);
	}
}

Through the above code can be achieved simply rotating the motor, the motor can also make Zuozhuanyouzhuan, look at it you realize
wiring here is PB12-> IN1, PB13-> IN2, EN1 hat to cover.

有 PWM 控制.

//定义电机,左电机为A(PB12,PB13,PA8),右电机为B(PB14,PB15,PA11)
//PB(12,13,14,15)接驱动模块IN脚,PA(8,11)接EN脚用来PWM输出
/////在.c文件里
void Motor_Init(void)
{
	RCC->APB2ENR|=1<<3;       //PORTB时钟使能 ,位2  
	GPIOB->CRH&=0X0000FFFF;   //PORTB12 13 14 15推挽输出
	GPIOB->CRH|=0X22220000;   //PORTB12 13 14 15推挽输出
}
void PWM_Init(u16 arr,u16 psc)
{		 					 
	MiniBalance_Motor_Init();  //初始化电机控制所需IO
	RCC->APB2ENR|=1<<11;       //使能TIM1时钟    
	RCC->APB2ENR|=1<<2;        //PORTA时钟使能     
	GPIOA->CRH&=0XFFFF0FF0;    //PORTA8 11复用输出
	GPIOA->CRH|=0X0000B00B;    //PORTA8 11复用输出
	TIM1->ARR=arr;             //设定计数器自动重装值 
	TIM1->PSC=psc;             //预分频器不分频
	TIM1->CCMR2|=6<<12;        //CH4 PWM1模式	
	TIM1->CCMR1|=6<<4;         //CH1 PWM1模式	   7<<4是PWM2模式
	TIM1->CCMR2|=1<<11;        //CH4预装载使能	 
	TIM1->CCMR1|=1<<3;         //CH1预装载使能	  
	TIM1->CCER|=1<<12;         //CH4输出使能	   
	TIM1->CCER|=1<<0;          //CH1输出使能	
	TIM1->BDTR |= 1<<15;       //TIM1必须要这句话才能输出PWM,MOE主输出使能
	TIM1->CR1=0x8000;          //ARPE使能 
	TIM1->CR1|=0x01;           //使能定时器1 											  
}


/////在.h文件里
#ifndef __MOTOR_H
#define __MOTOR_H
#include <sys.h>	 
#define AIN1   PBout(12)
#define AIN2   PBout(13)
#define BIN1   PBout(14)
#define BIN2   PBout(15)
#define PWMA   TIM1->CCR1  //赋值给PWMA就可以直接修改寄存器,发出不同PWM了
#define PWMB   TIM1->CCR4  

void MiniBalance_PWM_Init(u16 arr,u16 psc);
void MiniBalance_Motor_Init(void);
#endif


//在main函数里
void main(void)
{
	Motor_Init();
	PWM_Init(7199,0);
	while(1)
	{
		IN1=1;IN2=0;//左电机
		IN3=1;IN4=0;//右电机
		PWMA=4000;PWMB=4000;//直接操作寄存器,改变PWM
	}
}

Remember, if you use the PWM, it is imperative EN hat pulled. Can be a library function version can also register version, I prefer to register points, very direct.

** By the way, note the PWM value can not be negative. ** here, the motor can move, as you play it.

Released four original articles · won praise 15 · views 1854

Guess you like

Origin blog.csdn.net/fengge2018/article/details/98453798