本文主要是介绍STM32F1与STM32CubeIDE编程实例-红外寻迹传感器驱动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
红外寻迹传感器驱动
红外寻迹传感器具有一对红外线发射管与接收管,发射管发射出一定频率的红外线,当检测方向遇到障碍物(反射面)时,红外线反射回来被接收管接收,经过比较器电路处理之后,输出接口会输出一个数字信号(低电平或高电平,取决于电路设计),有效范围一般为2~30CM。传感器主动红外反射探测,因此反射率和形状是探测距离的关键。其中黑色探测距离小,白色距离大;小面积物体距离小,大面积距离大。
本实例将演示如何使用红外寻迹传感器。本次使用到的传感器,在检测到检测方向障碍物信号时,电路持续输出低电平。
1、红外寻迹传感器配置
开发环境搭建、系统时钟配置、调试配置及串口配置,请参考:
- STM32F1与STM32CubeIDE快速入门-开发环境搭建
- STM32F1与STM32CubeIDE快速入门-GPIO概述与点亮LED
- STM32F1与STM32CubeIDE快速入门-USART/UART串口通信
传感器配置如下:
保存配置并生成代码。
2、红外寻迹传感器驱动实现
1)基本定义
/** infrared_tracking_sensor.h** Created on: Apr 23, 2022* Author: jenson*/#ifndef __INFRARED_TRACKING_SENSOR_H__
#define __INFRARED_TRACKING_SENSOR_H__#include <stdio.h>
#include <stm32f1xx_hal.h>
#include "main.h"typedef struct {uint16_t id;GPIO_TypeDef *GPIOx; // 总线端口uint16_t GPIO_Pin; // 总线引脚
} infrared_tracking_sensor_t;/***@brief 红外寻迹传感器状态*@param sensor 开关对象*@return 当开关闭合时,返回1;否则,返回0*/
uint8_t infrared_tracking_sensor_check(infrared_tracking_sensor_t* sensor);#endif /* __INFRARED_TRACKING_SENSOR_H__ */
2)传感器状态检测
/** infrared_tracking_sensor.c** Created on: Apr 23, 2022* Author: jenson*/#include "infrared_tracking_sensor.h"uint8_t infrared_tracking_sensor_check(infrared_tracking_sensor_t *sensor) {// 读取传感器输出电平if (HAL_GPIO_ReadPin(sensor->GPIOx, sensor->GPIO_Pin) == GPIO_PIN_SET) {return 1;}return 0;
}
3)主程序
在main.c
文件中添加如下代码:
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include "infrared_tracking_sensor/infrared_tracking_sensor.h"
/* USER CODE END Includes *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV */
infrared_tracking_sensor_t sensor;
/* USER CODE END PV */int main(void) {/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_USART1_UART_Init();/* USER CODE BEGIN 2 */sensor.id = 1;sensor.GPIO_Pin = INFRARED_TRACKING_SENSOR_Pin;sensor.GPIOx = INFRARED_TRACKING_SENSOR_GPIO_Port;printf("****STM32CubeIDE:Infrared Tracking Sensor****\r\n");/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1) {/* USER CODE END WHILE *//* USER CODE BEGIN 3 */if (infrared_tracking_sensor_check(&sensor)) {printf("sensor tracked black line\r\n");} else {printf("sensor tracked while line\r\n");}HAL_Delay(10);}/* USER CODE END 3 */
}
程序运行结果:
3、红外寻迹传感器中断方式配置
前面的驱动代码是基于循环轮询实现,比较耗CPU。可以通过外部中断方式来对传感器输出信号进行检测。关于外部中断使用,请参考:
- STM32F1与STM32CubeIDE快速入门-中断、NVIC与EXTI概述
- STM32F1与STM32CubeIDE快速入门-外部中断配置与功能实现
传感器的中断方式驱动配置如下:
保存配置并生成代码。
4、红外寻迹传感器中断方式驱动实现
1)基本定义
/** infrared_tracking_sensor.h** Created on: Apr 23, 2022* Author: jenson*/#ifndef __INFRARED_TRACKING_SENSOR_H__
#define __INFRARED_TRACKING_SENSOR_H__#include <stdio.h>
#include <stm32f1xx_hal.h>
#include "main.h"/**** @brief 红外寻迹感器中断回调函数* @param sensor_id 传感器编码*/
typedef void (*infrared_tracking_sensor_callback)(uint16_t sensor_id);typedef struct {uint16_t id;GPIO_TypeDef *GPIOx; // 总线端口uint16_t GPIO_Pin; // 总线引脚infrared_tracking_sensor_callback callback; // 中断服务回调
} infrared_tracking_sensor_t;#endif /* __INFRARED_TRACKING_SENSOR_H__ */
2)中断服务实现
在stm32f1xx_it.c
文件中添加如下代码:
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "infrared_tracking_sensor/infrared_tracking_sensor.h"
/* USER CODE END Includes *//* USER CODE BEGIN EV */
extern infrared_tracking_sensor_t sensor;
/* USER CODE END EV *//* USER CODE BEGIN 1 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {if(GPIO_Pin == sensor.GPIO_Pin){if(sensor.callback){sensor.callback(sensor.id);}}
}
/* USER CODE END 1 */
3)主程序
在main.c
文件中添加如下代码:
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include "infrared_tracking_sensor/infrared_tracking_sensor.h"
/* USER CODE END Includes *//* USER CODE BEGIN PV */
infrared_tracking_sensor_t sensor;
/* USER CODE END PV *//* USER CODE BEGIN PFP */
void on_infrared_tracking_sensor_callback(uint16_t sensor_id);
/* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void on_infrared_tracking_sensor_callback(uint16_t sensor_id){printf("sensor %d detected black line\r\n",sensor_id);
}
/* USER CODE END 0 */int main(void) {/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_USART1_UART_Init();/* USER CODE BEGIN 2 */sensor.id = 1;sensor.GPIO_Pin = INFRARED_TRACKING_SENSOR_Pin;sensor.GPIOx = INFRARED_TRACKING_SENSOR_GPIO_Port;sensor.callback = on_infrared_tracking_sensor_callback;printf("****STM32CubeIDE:Infrared Tracking Sensor****\r\n");/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1) {/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}
程序运行结果:
这篇关于STM32F1与STM32CubeIDE编程实例-红外寻迹传感器驱动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!