做AVR电源,两级升压,一级降压,采样输入电压后根据采样点落在不通的范围控制进入不通的状态,用一般的比较还要考虑当前状态的方式太繁琐而且程序量也不小容易出错,故作出以下算法,判断即IO口控制一起完成,感觉不错,希望二姨家高手们批评指正:
typedef struct
{
word entry_high;
word entry_low;
word leave_high;
word leave_low;
IOControl io_val;
} volt_threshold;
#define LINE_ABNOR_LOW 0
#define LINE_LOWEST 1
#define LINE_LOW 2
#define LINE_NORMAL 3
#define LINE_HIGH 4
#define LINE_ABNOR_HIGH 5
volt_threshold Status_Array[]=
{
{VOLT_160V,VOLT_160V,VOLT_170V,VOLT_170V,0x0},
{VOLT_180V,VOLT_170V,VOLT_190V,VOLT_160V,0x0d},
{VOLT_210V,VOLT_190V,VOLT_220V,VOLT_180V,0x0f},
{VOLT_230V,VOLT_220V,VOLT_240V,VOLT_210V,0x01},
{VOLT_240V,VOLT_240V,VOLT_250V,VOLT_230V,0x0b},
{VOLT_250V,VOLT_250V,VOLT_240V,VOLT_240V,0x0},
};
static byte SysStatus = LINE_NORMAL;
void Main_Process(void)
{
byte curState = SysStatus;
while(1)
{
if(linePeakV > Status_Array[SysStatus].leave_high)
{
if(SysStatus == LINE_ABNOR_HIGH)
break;
SysStatus++;
continue ;
}
if(linePeakV < Status_Array[SysStatus].leave_low)
{
if(SysStatus == LINE_ABNOR_LOW)
break;
SysStatus--;
continue ;
}
break;
}
if(curState != SysStatus)
{
PTAD_PTAD1 = Status_Array[SysStatus].io_val.Relay1;
PTAD_PTAD2 = Status_Array[SysStatus].io_val.Relay2;
PTAD_PTAD3 = Status_Array[SysStatus].io_val.Relay3;
PTAD_PTAD4 = Status_Array[SysStatus].io_val.LEDYellow;
}
} |