你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

STM32基于hal库的智能小车—红外寻迹

2021/12/28 12:53:18

材料:

(1)stm32f407zgt6最小系统开发板

(2)l298n电机驱动模块1个

(3)四个电机

(4)循迹红外模块3个

一、组装

(1)L298N电机驱动模块与stm32开发板接线如下图:

说明:PWM接线把跳线帽扒开接外面引脚 CH1接ENA,CH2接ENB。

(2)寻迹红外接线:

二、主要程序

1、STM32CUBEMX配置如下:

(1)引脚配置:

说明:

1)motor11、motor12 、motor21、motor22 为电机驱动引脚

2)  sensor1、sensor2、sensor3 为红外引脚

(2)配置RCC时钟:  

 

(3) 时钟的配置: 

 (4)预分频、分频和占空比配置:

 三、程序

main.c

HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_4);
TIM3->CCR1=TIM3->CCR4=80;//两个电机的速度范围(0-100),越大越快。
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

    Tracking();//寻迹
  }
  /* USER CODE END 3 */

 main.h

#include "Track.h"

电机程序: 

 Motor.c

#include "motor.h"
 
//前进
void car_go_straight(void)
{
   HAL_GPIO_WritePin(motor11_GPIO_Port,motor11_Pin,GPIO_PIN_SET);
   HAL_GPIO_WritePin(motor12_GPIO_Port,motor12_Pin,GPIO_PIN_RESET);
	
   HAL_GPIO_WritePin(motor21_GPIO_Port,motor21_Pin,GPIO_PIN_SET);
   HAL_GPIO_WritePin(motor22_GPIO_Port,motor22_Pin,GPIO_PIN_RESET);
}
 
//右转
void car_go_right(void)
{
   HAL_GPIO_WritePin(motor11_GPIO_Port,motor11_Pin,GPIO_PIN_SET);
   HAL_GPIO_WritePin(motor12_GPIO_Port,motor12_Pin,GPIO_PIN_RESET);
 
   HAL_GPIO_WritePin(motor21_GPIO_Port,motor21_Pin,GPIO_PIN_RESET);
   HAL_GPIO_WritePin(motor22_GPIO_Port,motor22_Pin,GPIO_PIN_SET);
 
}
 
//左转
void car_go_left(void)
{
   HAL_GPIO_WritePin(motor11_GPIO_Port,motor11_Pin,GPIO_PIN_RESET);
   HAL_GPIO_WritePin(motor12_GPIO_Port,motor12_Pin,GPIO_PIN_SET);
 
   HAL_GPIO_WritePin(motor21_GPIO_Port,motor21_Pin,GPIO_PIN_SET);
   HAL_GPIO_WritePin(motor22_GPIO_Port,motor22_Pin,GPIO_PIN_RESET); 
 
}
 
 
//停止
void car_go_ahead(void)
{
   HAL_GPIO_WritePin(motor11_GPIO_Port,motor11_Pin,GPIO_PIN_RESET);
   HAL_GPIO_WritePin(motor12_GPIO_Port,motor12_Pin,GPIO_PIN_RESET);
 
   HAL_GPIO_WritePin(motor21_GPIO_Port,motor21_Pin,GPIO_PIN_RESET);
   HAL_GPIO_WritePin(motor22_GPIO_Port,motor22_Pin,GPIO_PIN_RESET);
 
}
 
 
//后退
void car_go_after(void)
{
   HAL_GPIO_WritePin(motor11_GPIO_Port,motor11_Pin,GPIO_PIN_RESET);
   HAL_GPIO_WritePin(motor12_GPIO_Port,motor12_Pin,GPIO_PIN_SET);
 
   HAL_GPIO_WritePin(motor21_GPIO_Port,motor21_Pin,GPIO_PIN_RESET);
   HAL_GPIO_WritePin(motor22_GPIO_Port,motor22_Pin,GPIO_PIN_SET);
 
}

 Motor.h

#ifndef __MOTOR_H_
#define __MOTOR_H_
 
#include "main.h"
 
void car_go_straight(void);
void car_go_right(void);
void car_go_left(void);
void car_go_ahead(void);
void car_go_after(void);
 
#endif

寻迹程序:

Track.c

#include "Track.h"
#include "motor.h"

void Tracking(void)
{
	   ///右转
		if ((HAL_GPIO_ReadPin(sensor1_GPIO_Port,sensor1_Pin)==1)&&(HAL_GPIO_ReadPin(sensor2_GPIO_Port,sensor2_Pin)==0)) 
		while(1)//10
		{
			car_go_right();
			if((HAL_GPIO_ReadPin(sensor1_GPIO_Port,sensor1_Pin)==1)&&(HAL_GPIO_ReadPin(sensor2_GPIO_Port,sensor2_Pin)==1)&&(HAL_GPIO_ReadPin(sensor3_GPIO_Port,sensor3_Pin)==0))
			break ;	
		}
		
		//左转
		else  if((HAL_GPIO_ReadPin(sensor1_GPIO_Port,sensor1_Pin)==0)&&(HAL_GPIO_ReadPin(sensor2_GPIO_Port,sensor2_Pin)==1)) 
		while(1)//01
		{
			car_go_left();
		 if((HAL_GPIO_ReadPin(sensor1_GPIO_Port,sensor1_Pin)==1)&&(HAL_GPIO_ReadPin(sensor2_GPIO_Port,sensor2_Pin)==1)&&(HAL_GPIO_ReadPin(sensor3_GPIO_Port,sensor3_Pin)==0))
			break ;	
		}
		
		/直行
		else
			car_go_straight();
		 	
 }

Track.h

#ifndef __TRACK_H_
#define __TRACK_H_

#include "main.h"

extern void Tracking(void);

#endif