43 lines
957 B
C
43 lines
957 B
C
|
#include "guangshi.h"
|
||
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|