huyongji1.1-system/App/gungshi/guangshi.c

83 lines
1.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 定义供电类型
#include "guangshi.h"
#define POWER_NONE 0 // 无供电
#define POWER_AC 1 // 市电供电
#define POWER_SOLAR 2 // 太阳能供电
// 市电检测相关变量
uint8_t bizohi = 0;
#define AC_SAMPLE_TIME 100 // 采样时间窗口
#define AC_CHANGE_THRESHOLD 5 // 最小变化次数阈值
static uint8_t last_ac_status = 0;
static uint8_t change_count = 0;
static uint32_t sample_timer = 0;
// 市电检测函数
void Check_Power_Source(void)
{
uint8_t current_ac = Read_MainPower(); // 读取市电检测端口
// 检测电平变化
if(current_ac != last_ac_status)
{
change_count++;
last_ac_status = current_ac;
}
sample_timer++;
// 采样时间窗口到达后判断
if(sample_timer >= AC_SAMPLE_TIME)
{
// 如果在采样窗口内检测到足够的变化次数,说明交流电存在
if(change_count >= AC_CHANGE_THRESHOLD)
{
bizohi = 1; // 市电正常
}
else
{
bizohi = 2; // 市电断开
}
// 重置计数器
change_count = 0;
sample_timer = 0;
}
}
// 太阳能检测函数
uint8_t Check_Solar_Power(void)
{
// 通过ADC值判断太阳能状态
// data1[1]是太阳能电压ADC值data1[2]是市电电压ADC值
if(data1[1] > data1[2])
{
return 1; // 太阳能电压足够
}
return 0; // 太阳能电压不足
}
// 获取当前供电类型
uint8_t Get_Power_Type(void)
{
// 先检查市电状态
if(bizohi == 1) // 市电正常
{
printf("当前使用市电供电\r\n");
return POWER_AC;
}
// 检查太阳能状态
else if(Check_Solar_Power())
{
printf("当前使用太阳能供电\r\n");
return POWER_SOLAR;
}
else
{
printf("当前无供电\r\n");
return POWER_NONE;
}
}