My PID Code in C on PIC18F - Help

I'm making a PID controller using a PIC18F452 in C.

    
    signed int pid_error;
    short ProportionalGain;
    short IntegralGain;
    short DerivitiveGain;
    double AccumError;
    short LastError;
    short TotalGain;
    signed int pid_output;
    unsigned char reverse;
    unsigned int Kp;
    unsigned int Ki;
    unsigned int Kd;    
    Kp = 2;
    Ki = 0;
    Kd = 0;
    reverse = 0;

    setpoint = 50;
    T0CONbits.TMR0ON    = 0;        //Turn off tmr0 
    LastError = pid_error;

    if (reverse = 0)
    {
        pid_error = ADRESH - setpoint;    //Forward
    }
    else
    {
        pid_error = setpoint - ADRESH;    //Reverse
    }
    
    AccumError = AccumError + pid_error;
    
    ProportionalGain = pid_error*Kp;
    IntegralGain = Kp * AccumError / Ki;
    DerivitiveGain = Kp * Kd * (pid_error - LastError);
    TotalGain = ProportionalGain + IntegralGain + DerivitiveGain;
    pid_output = setpoint + TotalGain;
My input is coming from ADRESH, 8-bit value from 1 - 5V, and my output is going to an external 8-bit I2C DAC. How can I convert "pid_error" to an 8-bit number. Also if anyone has some general comments about my code, I'll take suggestions.

Note: this is my first version, I plan on adding integral limits, derivitive spike prevention, auto/manual control, ect.

Replies

You are reading an archived discussion.

Related Posts

i need the circuit of 1.2G wireless rf transmitter and receiver circuit.. please help me as soon as possible guys.. its kind of urgent.. I'm also trying for it...
The Quick Response code seems to be getting popular around our place too. I remember getting curious about this square with random little squares in it that was seen at...

hi

hay i am to this site pls guide me my name is :skylab studying b.tech 3rd branch cse 😁
Does anyone know from where to get High Torque ( > 30 Kgcm ) DC motors in India ? and its weight should be less then a Kg. [Edit: oops...
friends 😀. i had collected all the components which is needed to do the line follower robot.. since am weak in programming knowledge i require some guidance for programming .....