Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

code for PID control using PIC16F877 in mikroC

Status
Not open for further replies.

Aman2009

Newbie level 3
Newbie level 3
Joined
Apr 10, 2010
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,304
I need an efficient code or program written in mikroC, to implement a PID control algorithm using PIC16F877 to control a temperature
 

I have written a program in mikroC that performs the task (PID control algorithm), but the result was not correct. The step response of the system was not as that theoritcally expected

Here is the code

float measured,desired;
float error,kp,ki,ek_1,uk,uk_1,T,ta,k,ts,a,b;
unsigned int uk_o;
unsigned char mV[6];
void main()
{
TRISA=0xFF;
TRISC=0;
TRISD=0;
ADCON1=0x80;
//// First order system parameters and calculation of PI parameters
T=1;
ts=5;
k=1;
ta=10;
kp=5*ta/(k*ts);
ki=5/(k*ts);
ek_1=0;
uk_1=0;

Lcd_init(&PORTB);
lcd_cmd(LCD_CURSOR_OFF);
Lcd_out(1,1,"Desired=");
Lcd_out(2,1,"Measured=");

while(1)
{
desired=Adc_read(0);
measured=Adc_read(1);

//WordTostr(desired,mV);
//Lcd_out(1,9,mV);
//WordTostr(Measured,mV);
//Lcd_out(2,10,mV);

error=(float)((desired-measured)*5000/1024);
uk=kp*error+ki*(error+ek_1)*T; // Positional form of a PI
if(uk>5) uk=5;
if(uk<0) uk=0;
//uk=(uk_1+(kp+ki*T)*error-kp*ek_1); // Velocity form
ek_1=error;
uk_1=uk;
uk_o=(int)(uk*1024/5000);
PORTC=uk_o;
PORTD=uk_o/4;
}
}

and this is a file that represents the circuit diagram


I want to know what is the error
 

Attachments

  • The circuit diagram.doc
    47 KB · Views: 261

Hi,

I´v done a float based PID some time ago. May be it helps...

// C-File
// Module..: PID.C
// Version.: 1.0
// Compiler: IAR ARM
// Date....: August 2007
// Chip....: STM32
//-----------------------------------------------------------------------------

#include "pid.h"
//-----------------------------------------------------------------------------

void pid_init(pPID pid)
{
// Preset coefficients and variables
pid->gain = 1.0;
pid->kp = 1.0;
pid->ki = 0.05;
pid->kd = 1.5;
pid->cv_min = 5.0; // Minimum Control Value
pid->cv_max = 255.0; // Maximum Control Value

pid->sp = 0.0; // Setpoint
pid->pv = 0.0; // Process Value
pid->pv_last = 0.0;
pid->i = 0.0;
pid->i_max = 10.0;
pid->cv = 0.0;
}
//-----------------------------------------------------------------------------

void pid_update(pPID pid, float dt)
{
float e, d;

// Error = setpoint - process value
e = pid->sp - pid->pv;

// Calculate integral term
pid->i += e * dt;

// Check integral term for upper limit
if (pid->i > pid->i_max)
{
pid->i = pid->i_max;
}

// Check integral term for lower limit
if (pid->i < -pid->i_max)
{
pid->i = -pid->i_max;
}

// Derivative part = (process value - last process value) / dt
if (dt > 0.00001) // Avoid division by zero
{
d = (pid->pv - pid->pv_last) / dt;
}
else
{
d = 0.0;
}

// Control value = gain * (kp * e + ki * i - kd * d)
pid->cv = pid->gain * (pid->kp * e + pid->ki * pid->i - pid->kd * d);

// Add min to control value
if (pid->cv >= 0.0 && pid->cv < pid->cv_min)
{
pid->cv += pid->cv_min;
}
else if (pid->cv < 0.0 && pid->cv > -pid->cv_min)
{
pid->cv -= pid->cv_min;
}

// Check control value upper limits
if (pid->cv > pid->cv_max)
{
pid->cv = pid->cv_max;
}

if (pid->cv < -pid->cv_max)
{
pid->cv = -pid->cv_max;
}

// Store reference value for next control cycle
pid->pv_last = pid->pv;
}



// Header File
#ifndef __PID_H
#define __PID_H
//-----------------------------------------------------------------------------

#include "includes.h"
//-----------------------------------------------------------------------------

typedef struct sPID
{
float gain; // Controller gain
float kp; // Coefficient for proportional term
float ki; // Coefficient for integral term
float kd; // Coefficient for derivative term
float sp; // Setpoint
float i; // Integral term
float i_max; // Integral part limit
float pv; // Process value
float pv_last; // Last process value
float cv; // Control value
float cv_min; // Minimum control value
float cv_max; // Control value limit
} tPID;

typedef tPID *pPID;
//-----------------------------------------------------------------------------

void pid_init(pPID pid);
void pid_update(pPID pid, float dt);
//-----------------------------------------------------------------------------

#endif
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top