71 lines
1.8 KiB
C
71 lines
1.8 KiB
C
/**
|
|
******************************************************************************
|
|
* @file relay_control.c
|
|
* @brief 继电器控制模块实现
|
|
* @author Application Layer
|
|
* @version 2.0
|
|
******************************************************************************
|
|
* @attention
|
|
* 本模块实现单路继电器的安全控制
|
|
* 关键特性:
|
|
* 1. 最小切换间隔保护,防止频繁切换损坏继电器
|
|
* 2. 状态记录
|
|
* 3. 调试日志输出
|
|
*
|
|
* 修订历史:
|
|
* v2.0 - 精简为单路继电器控制,移除冗余功能
|
|
******************************************************************************
|
|
*/
|
|
|
|
#include "relay_control.h"
|
|
#include "uart2_print.h"
|
|
#include "main.h"
|
|
|
|
#define DEBUG_RELAY 1
|
|
|
|
#if DEBUG_RELAY
|
|
#define DEBUG_LOG(fmt, ...) UART2_Print_Printf("[RELAY] " fmt "\r\n", ##__VA_ARGS__)
|
|
#else
|
|
#define DEBUG_LOG(fmt, ...)
|
|
#endif
|
|
|
|
static bool current_state = false;
|
|
static uint32_t last_toggle_tick = 0;
|
|
|
|
void Relay_Init(void)
|
|
{
|
|
HAL_GPIO_WritePin(RL_Control_GPIO_Port, RL_Control_Pin, GPIO_PIN_RESET);
|
|
current_state = false;
|
|
last_toggle_tick = 0;
|
|
|
|
DEBUG_LOG("Init OK, state=OFF");
|
|
}
|
|
|
|
void Relay_SetState(bool state)
|
|
{
|
|
uint32_t current_tick = HAL_GetTick();
|
|
|
|
if (current_tick - last_toggle_tick < RELAY_MIN_INTERVAL) {
|
|
DEBUG_LOG("Toggle too fast, ignored");
|
|
return;
|
|
}
|
|
|
|
if (current_state == state) {
|
|
DEBUG_LOG("State unchanged: %s", state ? "ON" : "OFF");
|
|
return;
|
|
}
|
|
|
|
HAL_GPIO_WritePin(RL_Control_GPIO_Port, RL_Control_Pin,
|
|
state ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
|
|
|
current_state = state;
|
|
last_toggle_tick = current_tick;
|
|
|
|
DEBUG_LOG("Relay -> %s", state ? "ON" : "OFF");
|
|
}
|
|
|
|
bool Relay_GetState(void)
|
|
{
|
|
return current_state;
|
|
}
|