Originally Posted by

**MohitM**
Basically PID is a control scheme that calculates:

1. The present error

2. The total of all previous errors

3. The difference between the present and previous error.

The measurement and the control action loop - meaning

1. System 'sensors' measure the process value (could be temperature, RPM, pressure),

2. System calculates the difference between the process value and set value,

3. System takes a control action (like turn on the heater, increase the motor PWM's duty cycle, open a pressure valve),

4. Go back to step 1.

The PID response is analog, but has been adapted to the digital world rather nicely. Check out the Microchip application note pointed above. It shows how this is done. In control systems I've designed I've used exclusively the scheme I'm indicating below (in pseudo-code) and although the tuning (more on this later) takes some effort, yet the control is great.

To begin with you can leave out the differential component. It complicates things greatly. Begin with a simple proportional control scheme and then move to a PI scheme.

Tuning basically means having to find values of Kp, Ki, Kd. This is best done by experiment (trial/error) although there are tuning schemes also like ZN.

Go through this and let me know if you have any questions.

<code>

PID_Calculate

;------------

;---------------------------------------------------------------------------------

; Ppp

; Produces proportional component by multiplying Error_Hi:Error_Lo by Kp

;

; ProH:Pro = Error_Hi:Error_Lo * PH:PL

;

; This term forces the output close to the desired output quickly,

; but will never completely eliminate the error.

;

; Input Variables:

; Error_Hi:Error_Lo = Error found at top of loop

; n4:n3 = Kp [Proportional gain factor (constant)]

;

; Output Variables:

; ProH:Pro Proportional component

;

; At maximum, Error=0x1000 and Kp=0xA, then ProH2:L2=0xA000

;

;---------------------------------------------------------------------------------

;---------------------------------------------------------------------------------

; IntCom Computes integral component

;

; Multiplies present error by Ki,

; adds result to IntH:Int

;

; IntH:Int = IntH:Int + Error_Hi:Error_Lo * IH:IL

;

; This term will eliminate all error, but not quickly

;

; Input Variables:

; Error_Hi:Error_Lo Present loop error

; IntH:Int Running total of errors

; Ki Integral gain factor (constant)

;

; Output Variables:

; IntH:Int Integral component

;---------------------------------------------------------------------------------

;---------------------------------------------------------------------------------

; DifCom Computes differential component

;

; Finds difference between this loop error and previous loop error

; DifH : Dif = (Error_P_Hi:Error_P_Lo - Error_Hi:Error_Lo) * DH:DL

;

; This term tends to slow controller response.

;

; Input Variables:

; Error_P_Hi:Error_P_Lo : Previous loop error

; Error_Hi:Error_Lo : Present loop error

; DH:DL : Differential gain factor (constant)

; XD : Differential division factor (constant)

;

; Output Variables:

; DifH:Dif Differential component

;---------------------------------------------------------------------------------

;---------------------------------------------------------------------------------

; Total Sums proportional, integral, and differential terms

;---------------------------------------------------------------------------------

</code>