实现了对运行指示灯,错误指示灯和水泵的控制函数封装
This commit is contained in:
32
Core/Bsp/BSP_Device/device_ctrl/device_ctrl.c
Normal file
32
Core/Bsp/BSP_Device/device_ctrl/device_ctrl.c
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "device_ctrl.h"
|
||||
#include "main.h"
|
||||
|
||||
// 注意:以下引脚宏需与 STM32CubeMX 生成的 main.h 中定义一致
|
||||
// 假设:
|
||||
// LED1 (运行灯) -> LED1_GPIO_Port / LED1_Pin
|
||||
// 用户需确认错误灯和继电器的引脚命名,这里假设为:
|
||||
// 错误灯 -> LED2_GPIO_Port / LED2_Pin
|
||||
// 继电器 -> RELAY_GPIO_Port / RELAY_Pin
|
||||
|
||||
void Device_Control(Device_Type_t device, uint8_t state)
|
||||
{
|
||||
GPIO_PinState pin_state = (state ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
||||
|
||||
switch (device) {
|
||||
case DEVICE_LED_RUN:
|
||||
HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, pin_state);
|
||||
break;
|
||||
|
||||
case DEVICE_LED_ERROR:
|
||||
// 假设错误灯使用 LED2
|
||||
HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, pin_state);
|
||||
break;
|
||||
|
||||
case DEVICE_RELAY:
|
||||
HAL_GPIO_WritePin(MOTOR_GPIO_Port, MOTOR_Pin, pin_state);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
19
Core/Bsp/BSP_Device/device_ctrl/device_ctrl.h
Normal file
19
Core/Bsp/BSP_Device/device_ctrl/device_ctrl.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef __DEVICE_CTRL_H
|
||||
#define __DEVICE_CTRL_H
|
||||
|
||||
#include "main.h" // 包含 GPIO 定义
|
||||
|
||||
typedef enum {
|
||||
DEVICE_LED_RUN, // 运行指示灯
|
||||
DEVICE_LED_ERROR, // 错误指示灯
|
||||
DEVICE_RELAY // 继电器
|
||||
} Device_Type_t;
|
||||
|
||||
/**
|
||||
* @brief 控制指定设备的开关状态
|
||||
* @param device: 设备类型
|
||||
* @param state: 0 = 关闭, 非0 = 打开
|
||||
*/
|
||||
void Device_Control(Device_Type_t device, uint8_t state);
|
||||
|
||||
#endif /* __DEVICE_CTRL_H */
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "dx_wf_24.h"
|
||||
#include "elog.h"
|
||||
#include "string.h"
|
||||
#include "device_ctrl.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@@ -132,7 +133,9 @@ void StartDefaultTask(void *argument)
|
||||
/* Infinite loop */
|
||||
for (;;) {
|
||||
|
||||
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
|
||||
Device_Control(DEVICE_LED_RUN, 1); // 打开运行灯
|
||||
osDelay(1000);
|
||||
Device_Control(DEVICE_LED_RUN, 0); // 关闭运行灯
|
||||
osDelay(1000);
|
||||
}
|
||||
/* USER CODE END StartDefaultTask */
|
||||
|
||||
Reference in New Issue
Block a user