//排序输入的模拟量过程值,用于得出Zui大值和Zui小值,要考虑是否为坏值
FOR iIndex1:=1 TO 8 BY 1 DO
FOR iIndex2:=1 TO (8 - iIndex1) BY 1DO
IF aryPVs[iIndex2]>aryPVs[iIndex2+1] THEN
rPVsExchange:=aryPVs[iIndex2 + 1];
aryPVs[iIndex2+1]:= aryPVs[iIndex2];
aryPVs[iIndex2]:=rPVsExchange;
xBadsExchange:=aryBads[iIndex2 + 1];
aryBads[iIndex2+1]:= aryBads[iIndex2];
aryBads[iIndex2]:=xBadsExchange;
END_IF;
END_FOR;
END_FOR;
//得出Zui小值
FOR iIndex1:=1 TO 8 BY 1 DO
IF NOT aryBads[iIndex1] THEN
MinPV:=aryPVs[iIndex1];
EXIT;
END_IF;
END_FOR;
//得出Zui大值
FOR iIndex1:=8 TO 1 BY -1 DO
MaxPV:=aryPVs[iIndex1];
EXIT;
END_IF;
END_FOR;
//计算平均值
iBadCount:=0;
rPVsAdd:=0.0;
FOR iIndex1:=1 TO 8 BY 1 DO
rPVsAdd:=rPVsAdd +aryPVs[iIndex1];
iBadCount:= iBadCount +1;
END_IF;
END_FOR;
AvePV:= rPVsAdd / INT_TO_REAL(iBadCount);
//模式选择,三选一作为报警的过程值
IF Mode = 0 THEN
rPV:=AvePV;
ELSIF Mode =1 THEN
rPV:= MinPV;
ELSIF Mode = 2 THEN
rPV:= MaxPV;
END_IF;
//计算滞后
// Set PV_Hystteresis value
rPV_Hyst := PV_Hyst * (PV_ScaleHigh -PV_ScaleLow) / 100.0;
//启用仿真
IF SimOn THEN
PV_Out:=SimPV;
ELSE
PV_Out:=rPV;
END_IF;
//零点死区,切除小信号
// Deadband
IF ABS(PV_Out) <= Deadband THENPV_Out:=0.0 ;END_IF;
//ALARM/WARNING
PV_AL_C:= ((PV_Out >= PV_AH_Lim)AND PV_AH_EN ) OR
((PV_Out <= PV_AL_Lim) AND PV_AL_EN );
PV_WL_C := ((PV_Out >=PV_WH_Lim ) AND PV_WH_EN ) OR
((PV_Out <= PV_WL_Lim ) AND PV_WL_EN );
PV_AL_G:= ((PV_Out < (PV_AH_Lim -rPV_Hyst)) AND PV_AH_EN ) OR
((PV_Out > (PV_AL_Lim + rPV_Hyst)) AND PV_AL_EN);
PV_WL_G := ((PV_Out <(PV_WH_Lim - rPV_Hyst)) AND PV_WH_EN ) OR
((PV_Out > (PV_WL_Lim + rPV_Hyst)) ANDPV_WL_EN );
IF PV_AL_C AND NOT PV_AL_C_Old THEN rPV_A_DC:=0.0; END_IF;
IF PV_WL_C AND NOT PV_WL_C_OldTHEN rPV_W_DC:=0.0; END_IF;
IF PV_AL_G AND NOT PV_AL_G_Old THEN rPV_A_DG:=0.0; END_IF;
IF PV_WL_G AND NOT PV_WL_G_OldTHEN rPV_W_DG:=0.0; END_IF;
//高高报警
//PV_AH_Lim
IF PV_AH_EN THEN
IF (PV_Out>= PV_AH_Lim) AND NOT PV_AH_Act THEN
rPV_A_DC:=rPV_A_DC+SampleTime;
IFrPV_A_DC > PV_A_DC THEN
PV_AH_Act := TRUE;
rPV_A_DC:= 0.0;
END_IF;
ELSIF (PV_Out< (PV_AH_Lim - rPV_Hyst))AND PV_AH_Act THEN
rPV_A_DG:=rPV_A_DG+SampleTime;
IFrPV_A_DG > PV_A_DG THEN
PV_AH_Act := FALSE;
rPV_A_DG:= 0.0;
END_IF;
END_IF;
ELSE
PV_AH_Act:=FALSE;
END_IF;