PID Digitsl controller

This conference allows discussion on any subject not already covered in another PICmicro conference. If a topic becomes popular enough, a new conference will be created for it.

PID Digitsl controller

Postby ABlincon » Fri Nov 08, 2019 1:08 pm

Hello friends, I am creating a digital PID controller. The values ​​of Kp and Ki had already been calculated mathematically. It does the PID but, the system should stabilize in approximately 2.04s, and this does it in 3 approx. (attached Proteus image) .If I modify the value of Kp or Ki, the system does the same, there is no change and I do not understand why. If anyone could help me with this please. I also get a warning message on proteus. Attached code and images. I would appreciate your help.

#include <18F4550.h>
#device ADC=10
#use DELAY(internal=4MHZ)
#fuses XT,NOWDT

int16 PID_output;
float Ap=0, Ai=0; //Variables, floating of 32 bits
float max=255,min=0.0; //anti-windup
float Rs, Ys,e; //Variables of PID
float value;
float Us; //Float variables
int8 range; // Magnitude of control action.
float e0;

//PID control
void PID_control() {
float Ki;
float Kp=2.066;
float Ti=0.1597;
Ki= Kp/Ti;
Ap = e*Kp; //Kp, Ki calculation according to algoritm
Ai = e*Ki+e0;
Us = Ap+Ai; //Control action calculation m(kT)

if( Us>max ) { //Anti windup.
else {
PID_output = ((int16)(Us));

if(PID_output>255.0) PID_output = 255.0; //Limiting control action to 255

range = (int)PID_output; //Changing float to an 8 bits int

void main() {

setup_port_a(ALL_ANALOG); //All port A pines analog
//set_tris_a(0b00111111); //Port A as input
setup_adc(ADC_CLOCK_INTERNAL); //Allow A/D converter with internal clock
setup_timer_2(T2_DIV_BY_16, 255,1);
setup_ccp1(CCP_PWM); //Configure CCP1 module with PWM

while(TRUE) {

value=READ_ADC(); //read value of reference

value=READ_ADC(); ////read value variable measured

e=Ys-Rs; //Error calculation
PID_control(); //Calling PID function
set_pwm1_duty(range);//PWM according to the magnitude of the calculated control action

Warning proteus
2.png (104.05 KiB) Viewed 487 times
Proteus response
1.png (117.78 KiB) Viewed 487 times
Posts: 1
Joined: Fri Nov 08, 2019 6:08 am

Re: PID Digitsl controller

Postby Roche » Sat Nov 09, 2019 1:33 pm

I may be being stupid, but it looks to me like a 1000uF cap charging through a 650ish R resistor.
Posts: 51
Joined: Fri Jul 11, 2014 12:35 pm
PIC experience: Professional 5+ years with MCHP products

Return to Other PICmicro topics

Who is online

Users browsing this forum: No registered users and 2 guests