Private Function PIDcalc(SV As Integer,PV As Integer) As Double
Dim SVtemp As Double,PVtemp As Double
Kc = CDbl(Kcin / 10#)
Ti = CDbl(Tiin / 10#)
Td = CDbl(Tdin / 10#)
Ts = CDbl(Tsin / 10#)
Kp = Kc
Ki = Kc * Ts / Ti
Kd = Kc * Td / Ts
SVn_last = SVn_now
SVtemp = CDbl(SV / 32000&)
If SVtemp > 1# Then SVtemp = 1#
If SVtemp < 0# Then SVtemp = 0#
SVn_now = SVtemp
PVn_last = PVn_now
PVtemp = CDbl(PV / 32000&)
If PVtemp > 1# Then PVtemp = 1#
If PVtemp < 0# Then PVtemp = 0#
PVn_now = PVtemp
PVn_now = CDbl(PV / 32000&)
En_last = En_now
En_now = SVn_now - PVn_now
MPn = Kp * En_now
MIn = Ki * En_now + MXn_last
MXn_last = MIn
'MDn = Kd * (En_now - En_last)
MDn = Kd * (PVn_last - PVn_now)
Mn = MPn + MIn + MDn
If Mn > 1# Then MXn_last = 1# - (MPn + MDn)
If Mn < 0# Then MXn_last = 0# - (MPn + MDn)
If Mn > 1# Then Mn = 1#
If Mn < 0# Then Mn = 0#
PIDcalc = Mn
End Function