You are on page 1of 4

#include <reg51.

h>
#include <stdio.h>
#include <math.h>
//#define PWMPIN P2_0
#define required P1
#define feedback P0
//Global variables and definition
unsigned char pwm_width;//********replace*****
unsigned int x = 0, error = 0, prev_error = 0, sum=0, sum2=0;
sbit PWMPIN = P2^0; // define output pin ********
/* uncommented
//--------------------------read port1-----------------------//sfr P1 = 0xFF; // input for the required temperature (from the A/D c)
sbit P1_1 = P1^0;
sbit P1_2 = P1^1;
sbit P1_3 = P1^2;
sbit P1_4 = P1^3;
sbit P1_5 = P1^4;
sbit P1_6 = P1^5;
sbit P1_7 = P1^6;
sbit P1_8 = P1^7;
//-----------------------------read port0---------------------// reading from port 0 (feedback)
//sfr P0 = 0xFF; // input for the sensor feedback (from the A/D c)
sbit P0_0 = P0^0;
sbit P0_1 = P0^1;
sbit P0_2 = P0^2;
sbit P0_3 = P0^3;
sbit P0_4 = P0^4;
sbit P0_5 = P0^5;
sbit P0_6 = P0^6;
sbit P0_7 = P0^7;
//-----------------------------*/
//-----------------------------------Functions prototypes-------------------------void wait(); // delay
//int required(); //input to port 1 P1
//int feedback(); //input to port 0 P0
int pid(int error,int prev_error); // PID
void pwm_setup(int out); //PWM
void timer0(); //Timer
//End functions prototypes
//--------------------------------------******* Main
******---------------------------------void main(void) //Main function
{
unsigned int sensor = 0, desired=0, out = 0;

TCON = 0x50;
TMOD = 0x20;
TH1 = 0xFA;
TR1 = 1;
TI = 1;
PCON = 0x80;
while(1)
{
wait();
//desired = required(); //call required function to read from the A/D c
sum = required; //P1_1 + P1_2*2 + P1_3*4 + P1_4*8 + P1_5*16 + P1_6*32 +
P1_7*64 + P1_8*128;
wait();
//sensor = feedback(); //call feedback function to read from the A/D c
sum2 = feedback; //P0_1 + P0_2*2 + P0_3*4 + P0_4*8 + P0_5*16 + P0_6*32 +
P0_7*64 + P0_8*128;
wait();
error = sensor - desired; // calculate the error
out = pid(error, prev_error); // call PID function
prev_error = error; // saving the old value of error
}
} //End Main ****
//-------------------------------- ** Functions **-------------------------//---------------------------delay function--------------------------void wait()
{
for(x=0; x < 255; x++);
for(x=0; x < 255; x++);
}//End delay
/*
//----------------------------rquired function----------------------int required()
{
int sum = 0;
//reading from port1 (required temperature)
sfr P1 = 0xFF; // input for the required temperature (from the A/D c)
sbit P1_1 = P1^0;
sbit P1_2 = P1^1;
sbit P1_3 = P1^2;
sbit P1_4 = P1^3;
sbit P1_5 = P1^4;
sbit P1_6 = P1^5;
sbit P1_7 = P1^6;
sbit P1_8 = P1^7;
sum = P1_1 + P1_2*2 + P1_3*4 + P1_4*8 + P1_5*16 + P1_6*32 + P1_7*64 +
P1_8*128;
return sum;
}//End required
//--------------------------------sensor function---------------------

int feedback()
{
int sum2 = 0;
// reading from port 0 (feedback)
sfr P0 = 0xFF; // input for the sensor feedback (from the A/D c)
sbit P0_0 = P0^0;
sbit P0_1 = P0^1;
sbit P0_2 = P0^2;
sbit P0_3 = P0^3;
sbit P0_4 = P0^4;
sbit P0_5 = P0^5;
sbit P0_6 = P0^6;
sbit P0_7 = P0^7;
sum2 = P0_1 + P0_2*2 + P0_3*4 + P0_4*8 + P0_5*16 + P0_6*32 + P0_7*64 +
P0_8*128;
return sum2;
}//End Feedback */
//----------------------------------------PID function-----------int PID_Func(int error, int prev_error){ //PID calculation function
unsigned short int Kp = 1, Ki=1, Kd=1, P=0, I=0, D=0;
int pid_out= 0;
if(error > 0 && error < 255){
P= Kp* error;
I= Ki* error* 0.001 ;
D= Kd* (error- prev_error)/0.001;
pid_out= P+I+D;
}
else if(error < 0)
{
pid_out = 0;
}
else if(error >= 255)
{
pid_out = 1;
}
return pid_out;
}//End PID
//------------------------------------------PWM--------------------void pwm_setup(int out){
TMOD = 0;
pwm_width = out;// it was 160!
EA = 1;
ET0 = 1;
TR0 = 1;
} //End PWM
//-----------------------------------------timer-----------------------void timer0() interrupt 1 {
if(!F0) { //Start of High level
F0 = 1; //Set flag
PWMPIN = 1; //Set PWM o/p pin
TH0 = pwm_width; //Load timer

TF0 = 0; //Clear interrupt flag


return; //Return
}
else { //Start of Low level
F0 = 0; //Clear flag
PWMPIN = 0; //Clear PWM o/p pin
TH0 = 255 - pwm_width; //Load timer
TF0 = 0; //Clear Interrupt flag
return; //return
}
}//End Timer
//--------------------------------------*** End Program ***----------------------------Thanks again

You might also like