337 lines
7.9 KiB
C
337 lines
7.9 KiB
C
#include "Driver.h"
|
||
uint8_t device_status[13] = {0}; // 初始化所有状态为0
|
||
static uint8_t led1_status = 0; // LED1 状态标志位,0 表示灭,1 表示亮
|
||
uint8_t biaozhi=0;
|
||
|
||
//水泵的水量
|
||
uint32_t pump_flow = 0; // 5分钟内的累计流量
|
||
|
||
uint8_t pump_running = 0; // 水泵运行状态
|
||
uint32_t last_upload_time = 0; // 上次上传时间
|
||
uint32_t flow_to_upload = 0; // 需要上传的流量值
|
||
uint8_t flow_need_upload = 0; // 新增:流量上传标志位
|
||
uint8_t biaozhi111 =0;
|
||
//状态上传
|
||
Device_Fault_t device_fault = {0}; // 初始化故障状态
|
||
void LED1_Control(uint8_t state)
|
||
{
|
||
// 设置 LED 状态
|
||
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_13, state ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
||
|
||
// 更新标志位
|
||
led1_status = state; // 更新标志位为当前状态
|
||
|
||
// 可选:打印当前状态
|
||
// printf("LED1: %s\n", state ? "ON" : "OFF");
|
||
}
|
||
//水泵运行控制
|
||
// 已知水泵流量是每小时1200升,那么:
|
||
// 每分钟流量 = 1200升/60分钟 = 20升/分钟
|
||
|
||
// 水泵控制函数
|
||
// 添加全局变量
|
||
|
||
|
||
// 添加全局变量来保存累计流量
|
||
//static uint32_t total_flow = 0; // 添加这个变量来保存总流量
|
||
|
||
void Control_WaterPump(uint8_t state)
|
||
{
|
||
|
||
|
||
|
||
if(state) // 水泵启动
|
||
{
|
||
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_1, GPIO_PIN_SET);
|
||
|
||
// 添加调试信息
|
||
// printf("水泵启动 - GPIOG_PIN1设置为高电平\r\n");
|
||
}
|
||
else // 水泵停止
|
||
{
|
||
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_1, GPIO_PIN_RESET);
|
||
// 添加调试信息
|
||
// printf("水泵停止 - GPIOG_PIN1设置为低电平\r\n");
|
||
}
|
||
|
||
}
|
||
|
||
//风机1控制
|
||
void Control_Fan1(uint8_t state)
|
||
{
|
||
if(state)
|
||
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_2, GPIO_PIN_SET);
|
||
else
|
||
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_2, GPIO_PIN_RESET);
|
||
|
||
}
|
||
//风机2控制
|
||
void Control_Fan2(uint8_t state)
|
||
{
|
||
if(state)
|
||
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_3, GPIO_PIN_SET);
|
||
else
|
||
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_3, GPIO_PIN_RESET);
|
||
|
||
}
|
||
|
||
|
||
// 控制臭氧开关
|
||
void Ozone_Control(uint8_t state)
|
||
{
|
||
if(state)
|
||
{
|
||
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_SET); // 开启臭氧
|
||
// printf("臭氧已开启\n");
|
||
}
|
||
else
|
||
{
|
||
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_RESET); // 关闭臭氧
|
||
// printf("臭氧已关闭\n");
|
||
}
|
||
|
||
}
|
||
|
||
void Control_Heater(uint8_t state)
|
||
{
|
||
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_9, state ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
||
// printf("加热膜: %s\n", state ? "开启" : "关闭");
|
||
}
|
||
// 在 Driver.c 中添加除磷控制函数
|
||
|
||
// 极板1控制(PE2)
|
||
void Control_Plate1(uint8_t state)
|
||
{
|
||
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_2, state ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
||
device_status[9] = state; // 更新极板1状态
|
||
}
|
||
|
||
// 极板2控制(PE3)
|
||
void Control_Plate2(uint8_t state)
|
||
{
|
||
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, state ? GPIO_PIN_SET : GPIO_PIN_RESET);
|
||
device_status[10] = state; // 更新极板2状态
|
||
}
|
||
void Control_Phosphorus(uint8_t state)
|
||
{
|
||
if(state)
|
||
{
|
||
// 只开启一个极板
|
||
if(polarity_state)
|
||
{
|
||
Control_Plate1(1); // 极板1通电
|
||
Control_Plate2(0); // 极板2断电
|
||
}
|
||
else
|
||
{
|
||
Control_Plate1(0); // 极板1断电
|
||
Control_Plate2(1); // 极板2通电
|
||
}
|
||
}
|
||
else
|
||
{
|
||
// 关闭所有极板
|
||
Control_Plate1(0);
|
||
Control_Plate2(0);
|
||
}
|
||
}
|
||
void Control_Dredge(uint8_t state)
|
||
{
|
||
if(state) // 排泥启动
|
||
{
|
||
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_4, GPIO_PIN_SET);
|
||
|
||
// printf("排泥启动 - GPIOG_PIN2设置为高电平\r\n");
|
||
}
|
||
else // 排泥停止
|
||
{
|
||
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_4, GPIO_PIN_RESET); // 关闭排泥
|
||
|
||
// printf("排泥停止 - GPIOG_PIN2设置为低电平\r\n");
|
||
}
|
||
}
|
||
// 读取除磷状态(任一极板运行即返回1)
|
||
uint8_t Read_Phosphorus_Status(void)
|
||
{
|
||
return (HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_2) || HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_3));
|
||
}
|
||
|
||
// 添加获取状态的函数
|
||
uint8_t Read_LED1_Status(void)
|
||
{
|
||
return HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_13);
|
||
}
|
||
|
||
uint8_t Read_WaterPump_Status(void)
|
||
{
|
||
return HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_1);
|
||
}
|
||
|
||
uint8_t Read_Fan1_Status(void)
|
||
{
|
||
return HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_2);
|
||
}
|
||
|
||
uint8_t Read_Fan2_Status(void)
|
||
{
|
||
return HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_3);
|
||
}
|
||
|
||
|
||
|
||
// Read_FloatBall函数保持不变
|
||
uint8_t Read_FloatBall(void)
|
||
{
|
||
return (uint8_t)HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_5);
|
||
}
|
||
// 获取臭氧状态
|
||
uint8_t Ozone_GetState(void)
|
||
{
|
||
return HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_6);
|
||
}
|
||
|
||
uint8_t Read_Heater_Status(void)
|
||
{
|
||
return HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_9);
|
||
}
|
||
uint8_t Read_MainPower(void)
|
||
{
|
||
return HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_2); // 返回 0 或 1
|
||
}
|
||
//
|
||
uint8_t Read_Dredge_Status(void)
|
||
{
|
||
return HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_4);
|
||
}
|
||
// 添加全局变量来保存累计流量
|
||
|
||
// 总开关控制函数
|
||
void Control_All_Devices(CmdState_t cmd)
|
||
{
|
||
if (cmd == CMD_ON) // 开机
|
||
{
|
||
// 清除强制关闭标志,允许设备运行
|
||
force_pump_off = 0;
|
||
force_fan_off = 0;
|
||
biaozhi111=1;
|
||
// 不强制切换模式,保持当前模式
|
||
// 如果需要切换模式,应该通过其他命令来实现
|
||
}
|
||
else if (cmd == CMD_OFF) // 关机
|
||
{
|
||
// 保存当前模式
|
||
biaozhi111=0;
|
||
WorkMode current_mode_backup = current_mode;
|
||
|
||
// 关闭所有外设
|
||
Control_WaterPump(0);
|
||
Control_Fan1(0);
|
||
Control_Fan2(0);
|
||
Control_Heater(0);
|
||
Ozone_Control(0);
|
||
|
||
// 设置强制关闭标志
|
||
force_pump_off = 1;
|
||
force_fan_off = 1;
|
||
|
||
// 保持当前模式不变
|
||
current_mode = current_mode_backup;
|
||
}
|
||
}
|
||
void Update_All_Status(void)
|
||
{
|
||
device_status[0] = led1_status; // LED1状态
|
||
device_status[1] = Read_WaterPump_Status(); // 水泵状态
|
||
device_status[2] = Read_Fan1_Status(); // 风机1状态
|
||
device_status[3] = Read_Fan2_Status(); // 风机2状态
|
||
device_status[4] = Ozone_GetState(); // 臭氧状态
|
||
device_status[5] = Read_FloatBall(); // 浮球状态
|
||
device_status[7] = Read_Heater_Status(); // 浮球状态
|
||
device_status[8] = Read_MainPower(); // 市电状态
|
||
device_status[9] = Read_Phosphorus_Status(); //除磷状态
|
||
device_status[10] =Read_Dredge_Status(); //除磷状态
|
||
device_status[11] = biaozhi111; //上传状态
|
||
}
|
||
|
||
// 水泵故障检测函数
|
||
// 正确的检测函数
|
||
void Check_Pump_Fault(void)
|
||
{
|
||
static uint32_t check_timer = 0;
|
||
uint8_t actual_status = Read_WaterPump_Status(); // 读取实际状态
|
||
|
||
// 比较控制命令和实际状态
|
||
if(1 != actual_status) // 如果实际状态与期望状态(1)不一致
|
||
{
|
||
check_timer++;
|
||
if(check_timer >= 10) // 10秒后仍不一致则判定为故障
|
||
{
|
||
device_fault.pump_fault = 1; // 1代表故障
|
||
// printf("水泵故障!\r\n");
|
||
}
|
||
}
|
||
else // 状态一致,表示正常运行
|
||
{
|
||
check_timer = 0; // 重置计时器
|
||
device_fault.pump_fault = 0; // 0代表正常
|
||
// printf("水泵正常运行\r\n");
|
||
}
|
||
}
|
||
|
||
|
||
// 风机1故障检测
|
||
void Check_Fan1_Fault(void)
|
||
{
|
||
static uint32_t check_timer = 0;
|
||
static uint8_t last_status = 0;
|
||
|
||
uint8_t current_status = Read_Fan1_Status(); // 读取风机1当前状态
|
||
|
||
if(current_status != last_status)
|
||
{
|
||
check_timer++;
|
||
if(check_timer >= 10) // 10秒后仍异常则判定为故障
|
||
{
|
||
device_fault.fan1_fault = 1;
|
||
// printf("风机1故障!\r\n");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
check_timer = 0;
|
||
device_fault.fan1_fault = 0;
|
||
}
|
||
|
||
last_status = current_status;
|
||
}
|
||
|
||
// 风机2故障检测
|
||
void Check_Fan2_Fault(void)
|
||
{
|
||
static uint32_t check_timer = 0;
|
||
static uint8_t last_status = 0;
|
||
|
||
uint8_t current_status = Read_Fan2_Status(); // 读取风机2当前状态
|
||
|
||
if(current_status != last_status)
|
||
{
|
||
check_timer++;
|
||
if(check_timer >= 10) // 10秒后仍异常则判定为故障
|
||
{
|
||
device_fault.fan2_fault = 1;
|
||
// printf("风机2故障!\r\n");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
check_timer = 0;
|
||
device_fault.fan2_fault = 0;
|
||
}
|
||
|
||
last_status = current_status;
|
||
}
|
||
|
||
|
||
|
||
|