L298N drives two-way motor buttons to control forward and reverse

One L298N has a two-way drive circuit, which can drive two motors to rotate at the same time. In the hardware part, it has been assembled when making a small car. One drive is used to control the two motors, and the two-way drive is to control four The motor is a four-wheel four-wheel drive car, so I didn't disassemble and modify it because it was too troublesome.

The blogger’s homepage has introduced the use of L298N. In order to avoid some small partners not understanding, let’s re-introduce it for everyone’s convenience.

1. The schematic diagram of the structure introduction of L298N is as follows

1.12v voltage input, GND for the ground terminal, 5v I think it is a 5v output interface, but I didn’t make too much description because I didn’t understand it in the last section. Now I can confirm that I can output a 5v power supply.

When the vcc terminal is connected to a voltage of 7~12v, the positive 5v does not need to be connected to the voltage, and the GND is connected to the GND of the single-chip microcomputer, and a 5v voltage can be output to the single-chip microcomputer.

2. Enable terminal, L298N has two enable ports, namely ENA and ENB, which are connected to the onboard 5v with a jumper cap, and the logic input digital signal 0, 1 can output the signal at the corresponding output terminal, because There are only digital signals 0, 1, so the speed regulation of the motor cannot be realized, only the left and right rotation of the motor can be realized. To adjust the speed, pull out the jumper cap, and use a PWM signal pulse wave to connect to the enable terminal, that is, ENA or ENB, so that the speed of the motor can be realized by controlling the duty cycle of the PWM.

3 logic input ports, IN1-IN4 four input ports, corresponding to the corresponding output ports, give 0, 1 to control the forward and reverse of the motor

4. The output port is connected to the positive and negative poles of the motor, regardless of the positive and negative poles.

2. Instructions for use of L298N:

DC motor drive:

L298N can output two channels to control the rotation of two motors, and the enable terminals ENA and ENB are active at high level.

The truth table is shown in the figure:

  We need to adjust the speed of the motor. We need to set IN1 and IN2 to determine the rotation direction of the motor, and then unplug the jumper cap and connect to the PWM signal. Adjusting the duty cycle can realize the speed regulation of the motor. Note that when the enable terminal signal is 0, the motor is in a stopped state. When the enable terminal is 1 and the logic inputs are all 0 or 1, the motor brakes, which will prevent the motor from rotating. , the motor will rotate normally only when the logic input is 01 or 10.
 

 3. Part of the code based on STM32

1. PWM initialization, because it is to drive two motors, so the two channels must be enabled during initialization. If you don’t know it, you can go to the homepage to have a look. In the experiment, we initialized four channels, and it is possible to choose two of them.

Timer 2 initialization

// 通用定时器2中断初始化
// 这里时钟选择APB1的2倍,而APB1为36M
// arr自动重装值
// psc时钟分频数
void TIM2_Int_Init(u16 arr,u16 psc)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // 时钟使能

	TIM_TimeBaseStructure.TIM_Period = arr; //  自动重装值
	TIM_TimeBaseStructure.TIM_Prescaler =psc; // 时钟分频数
	TIM_TimeBaseStructure.TIM_ClockDivision = 0; // 设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  // 向上计数
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //时候基数单位 

	TIM_Cmd(TIM2, ENABLE);  //使能
							 
}

PWM initialization 

void TIM2_PWM_Init(u16 arr,u16 psc)
{
	 GPIO_InitTypeDef GPIO_InitStructure;                
	 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;       
	 TIM_OCInitTypeDef  TIM_OCInitStructure;               //定义结构体
	
	 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);  //使能定时器3时钟
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB  | RCC_APB2Periph_AFIO| 
     RCC_APB2Periph_GPIOA, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟

     GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
	 GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2 , ENABLE); //Timer2部分重映射


     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
	 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //复用推挽输出
	 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	 GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIO

     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_15; //TIMx_CHx
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //复用推挽输出
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_Init(GPIOA, &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(TIM2, &TIM_TimeBaseStructure);// 初始化时钟基数单位
   
 
	 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;   //脉宽调制模式2
 	 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;       // 输出极性为高    
     TIM_OCInitStructure.TIM_OutputState=	TIM_OutputState_Enable;   //比较输出使能
     TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值

    //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
     TIM_OC1Init(TIM2, &TIM_OCInitStructure);  
     TIM_OC2Init(TIM2, &TIM_OCInitStructure);  //
     TIM_OC3Init(TIM2, &TIM_OCInitStructure);  //
     TIM_OC4Init(TIM2, &TIM_OCInitStructure);  //
    
 
     //使能预装载寄存器
     TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
     TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
     TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
     TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
	
	 TIM_ARRPreloadConfig(TIM2, ENABLE);      //使能自动装载允许位
 
	 TIM_Cmd(TIM2, ENABLE);   //使能定时器
 
}

2. Motor initialization, using a DC geared motor without an encoder.

void motro_Init(void)
{
 
 GPIO_InitTypeDef  GPIO_InitStructure;
 	
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);	 //使能GPIOF端口
	
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4| GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7;//端口设置			 
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //设置速度
 GPIO_Init(GPIOF, &GPIO_InitStructure);					 //初始化GPIOF
 GPIO_SetBits(GPIOF,GPIO_Pin_4| GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7);						 //输出高电平
}
 

3. Button initialization

void KEY_Init(void) //IO初始化
{ 
 	GPIO_InitTypeDef GPIO_InitStructure;
 
 	//使能 PORTA,PORTE时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE);

	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_4|GPIO_Pin_3;//KEY0-KEY1
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉
 	GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化io

	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //下拉
	GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化io

}

3.1. Button scan function

u8 KEY_Scan(u8 mode)
{	 
	static u8 key_up=1;//按键松开标志
	if(mode)key_up=1;  //支持连按	  
	if(key_up&&(KEY0==0||KEY1==0||WK_UP==1))
	{
		delay_ms(10);//去抖动
		key_up=0;
		if(KEY0==0)return KEY0_PRES;
		else if(KEY1==0)return KEY1_PRES;
		else if(WK_UP==1)return WKUP_PRES;
	}else if(KEY0==1&&KEY1==1&&WK_UP==0)key_up=1; 	    
 	return 0;// 无按键按下
}

4. Main function

 int main(void)
{ 
  u8 key; 
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统优先级分组2
  delay_init();  //延时函数初始化
	uart_init(115200);//初始串口波特率为115200
  KEY_Init();  //按键初始化
	motro_Init();//电机初始化
	TIM2_PWM_Init(899,0);	//PWM初始化  
   while(1) //ʵÏֱȽÏÖµ´Ó0-300µÝÔö£¬µ½300ºó´Ó300-0µÝ¼õ£¬Ñ­»·
	{
 	 	key=KEY_Scan(0);		//得到键值
	   	if(key)
		{						   
			switch(key)
			{				 
				case KEY0_PRES:	//控制电机翻转
				  F4=0;
				  F5=0;
				  F6=1;  
				  F7=1;
					break;
				case KEY1_PRES:	//
				  F4=1;
				  F5=1;
				  F6=0;  
				  F7=0;
					break;
			}
		}else delay_ms(10); 	 
        //设置占空比
		TIM_SetCompare1(TIM2,350);
        TIM_SetCompare2(TIM2,350);       
        TIM_SetCompare3(TIM2,350);         
        TIM_SetCompare4(TIM2,350);  
	}
}

4. Experimental results:

PWM controls the car to turn forward and backward

The shot is not very good, so let’s just wait and see. Haha, it is not easy to create if you need to like the complete project file, add a favorite and leave an email in the comment area.

Guess you like

Origin blog.csdn.net/m0_63171897/article/details/127345321