STM32F1与STM32CubeIDE编程实例-线性霍尔效应传感器驱动

线性霍尔效应传感器驱动

当对材料通电时,电流以直线方式流过它。 也就是说,电子沿直线行进。 然后,当磁铁靠近该材料时,沿直线行进的电子再次沿弯曲路径行进。 这导致来自永磁体和电流的电位差或电压。 这种现象称为霍尔效应。

霍尔效应传感器(或简称霍尔传感器)是一种使用霍尔效应检测磁场的存在和大小的传感器。霍尔传感器的输出电压与场强成正比。它以美国物理学家埃德温·霍尔命名。

霍尔传感器用于接近感应、定位、速度检测和电流感应应用。通常,霍尔传感器与阈值检测相结合以充当二进制开关。常见于工业应用中,例如图中的气缸,它们也用于消费设备;例如,一些计算机打印机使用它们来检测缺纸和打开的盖子。一些 3D 打印机使用它们来测量灯丝厚度。

霍尔传感器通常用于对车轮和轴的速度进行计时,例如用于内燃机点火正时、转速计和防抱死制动系统。它们用于无刷直流电动机中以检测永磁体的位置。在图中具有两个等距磁铁的车轮中,传感器的电压每转两次峰值。这种安排通常用于调节磁盘驱动器的速度。

在这里插入图片描述

本次实例使用Key-024 线性磁性霍尔传感器模块包括一个称为 49E 的霍尔效应传感器。 此外,我们可以使用此传感器模块获得数字和模拟输出。 这里的电位器可以改变输出的灵敏度。 数字输出可用作靠近磁铁的开关,模拟输出可用于测量磁场的极性和相对强度。

1、线性霍尔效应传感器配置

线性霍尔效应传感器配置:

在这里插入图片描述

保存配置并生成代码。

2、线性霍尔效应传感器驱动实现

1)基本定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/\*
\* linear\_hall\_sensor.h
\*
\* Created on: Apr 22, 2022
\* Author: jenson
\*/

#ifndef \_\_LINEAR\_HALL\_SENSOR\_H\_\_
#define \_\_LINEAR\_HALL\_SENSOR\_H\_\_

#include <stdio.h>
#include <stm32f1xx\_hal.h>
#include "main.h"

typedef struct {
uint16\_t id;
GPIO_TypeDef \*GPIOx; // 总线端口
uint16\_t GPIO_Pin; // 总线引脚
} linear\_hall\_sensor\_t;

/\*\*
\*@brief 检查霍尔传感状态
\*@param sensor 开关对象
\*@return 当开关闭合时,返回1;否则,返回0
\*/
uint8\_t linear\_hall\_sensor\_check(linear\_hall\_sensor\_t\* sensor);

#endif /\* \_\_LINEAR\_HALL\_SENSOR\_H\_\_ \*/


2)霍尔传感器状态检测

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/\*
\* linear\_hall\_sensor.c
\*
\* Created on: Apr 22, 2022
\* Author: jenson
\*/

#include "linear\_hall\_sensor.h"

uint8\_t linear\_hall\_sensor\_check(linear\_hall\_sensor\_t \*sensor) {
// 读取传感器输出电平
if (HAL\_GPIO\_ReadPin(sensor->GPIOx, sensor->GPIO_Pin) == GPIO_PIN_SET) {
return 1;
}
return 0;
}

3)主程序

main.c文件中添加如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/\* Private includes ----------------------------------------------------------\*/
/\* USER CODE BEGIN Includes \*/
#include <stdio.h>
#include "linear\_hall\_sensor/linear\_hall\_sensor.h"
/\* USER CODE END Includes \*/

/\* USER CODE BEGIN PV \*/
linear\_hall\_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 = LINEAR_HALL_SENSOR_Pin;
sensor.GPIOx = LINEAR_HALL_SENSOR_GPIO_Port;
printf("\*\*\*\*STM32CubeIDE:Linear Hall Sensor\*\*\*\*\r\n");
/\* USER CODE END 2 \*/

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

/\* USER CODE BEGIN 3 \*/
if (linear\_hall\_sensor\_check(&sensor)) {
printf("Linear Hall Sensor toggled\r\n");
}
HAL\_Delay(10);
}
/\* USER CODE END 3 \*/
}

当磁铁靠近霍尔传感器时,将得到如下结果:

在这里插入图片描述

3、线性霍尔效应传感器中断方式配置

前面的驱动代码是基于循环轮询实现,比较耗CPU。可以通过外部中断方式来对传感器输出信号进行检测。关于外部中断使用,请参考:

传感器的中断方式驱动配置如下:
在这里插入图片描述

在这里插入图片描述

保存并生成代码。

4、线性霍尔效应传感器中断方式驱动实现

1)基本定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/\*
\* linear\_hall\_sensor.h
\*
\* Created on: Apr 22, 2022
\* Author: jenson
\*/

#ifndef \_\_LINEAR\_HALL\_SENSOR\_H\_\_
#define \_\_LINEAR\_HALL\_SENSOR\_H\_\_

#include <stdio.h>
#include <stm32f1xx\_hal.h>
#include "main.h"

/\*\*\*
\* @brief 霍尔传感器中断回调函数
\* @param sensor\_id 传感器编码
\*/
typedef void (\*linear_hall_sensor_callback)(uint16\_t sensor_id);

typedef struct {
uint16\_t id;
GPIO_TypeDef \*GPIOx; // 总线端口
uint16\_t GPIO_Pin; // 总线引脚
linear_hall_sensor_callback callback;//中断回调
} linear\_hall\_sensor\_t;


#endif /\* \_\_LINEAR\_HALL\_SENSOR\_H\_\_ \*/


2)中断服务驱动实现

stm32f1xx_it.c文件中,添加如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/\* Private includes ----------------------------------------------------------\*/
/\* USER CODE BEGIN Includes \*/
#include "linear\_hall\_sensor/linear\_hall\_sensor.h"
/\* USER CODE END Includes \*/

/\* Private function prototypes -----------------------------------------------\*/
/\* USER CODE BEGIN PFP \*/
extern linear\_hall\_sensor\_t sensor;
/\* USER CODE END PFP \*/

/\* 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文件中添加如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/\* Private includes ----------------------------------------------------------\*/
/\* USER CODE BEGIN Includes \*/
#include <stdio.h>
#include "linear\_hall\_sensor/linear\_hall\_sensor.h"
/\* USER CODE END Includes \*/

/\* USER CODE BEGIN PV \*/
linear\_hall\_sensor\_t sensor;
/\* USER CODE END PV \*/

/\* USER CODE BEGIN PFP \*/
void on\_linear\_hall\_sensor\_callback(uint16\_t sensor_id);
/\* USER CODE END PFP \*/

/\* Private user code ---------------------------------------------------------\*/
/\* USER CODE BEGIN 0 \*/
void on\_linear\_hall\_sensor\_callback(uint16\_t sensor_id){
printf("Linear Hall Sensor %d toggled\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 = LINEAR_HALL_SENSOR_Pin;
sensor.GPIOx = LINEAR_HALL_SENSOR_GPIO_Port;
sensor.callback = on_linear_hall_sensor_callback;
printf("\*\*\*\*STM32CubeIDE:Linear Hall 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 \*/
}

程序运行结果如下:

在这里插入图片描述

文章来源: https://iotsmart.blog.csdn.net/article/details/125385082