biswaIITH
Full Member level 4
- Joined
- Aug 23, 2014
- Messages
- 205
- Helped
- 27
- Reputation
- 54
- Reaction score
- 28
- Trophy points
- 1,308
- Location
- Hyderabad
- Activity points
- 2,923
Hello folks ...i am trying to implement PI controller using PIC16F876A...Following is the code,i am using..I am applying approx 4.55V to VDD...Applying 4v to channel 1 of ADC as a reference
DOUBTS
1)Is the above code ok as far as implementation of PI controller is concerned???I consulted one senior with this code..He told me the above code will have varying sampling frequency..How to fix that????
2)The line Sum_err=(P_term+I_term); will have 16 bit result in it...But, i am planning to use lower 8 bit of it(as the library functions in mikroC PRO i am using can handle upto 8 bit) ..How to do that???
Code:
int err_val=0,I_err=0;
int current_duty_2=10;
int Kp=1,Ki=1;
int P_term=0,I_term=0;
int Sum_err=0;
unsigned int ADCRead(unsigned int channel)
{
unsigned int l_byte, h_byte;
unsigned int ADR;
ADCON0 = 0x81 | (channel << 3);
delay_us(30);
ADCON0.F2 = 1;
while(ADCON0.F2);
l_byte = ADRESL;
h_byte = ADRESH;
ADR = h_byte;
return (ADR);
}
void main(void)
{
unsigned int Ch0,Ch1;
TRISA = 0xFF;
ADCON0 = 0x81;
ADCON1 = 0x40;
TRISC.F1=0;
PWM2_Init(25000);
PWM2_Start();
PWM2_Set_Duty(current_duty_2);
while(1)
{
Ch0= ADCRead(0);
Ch1= ADCRead(1);
err_val=(Ch1-Ch0);
P_term=(Kp*err_val);
I_err=I_err+err_val;
I_term=(Ki*I_err);
if(I_term>30000)
I_term=30000;
else if(I_term<-30000)
I_term=-30000;
Sum_err=(P_term+I_term);
********************************************
if(Sum_err>245)
Sum_err=245;
else if(Sum_err<0)
Sum_err=0;
current_duty_2=Sum_err;
PWM2_Set_Duty(current_duty_2);
}
}
DOUBTS
1)Is the above code ok as far as implementation of PI controller is concerned???I consulted one senior with this code..He told me the above code will have varying sampling frequency..How to fix that????
2)The line Sum_err=(P_term+I_term); will have 16 bit result in it...But, i am planning to use lower 8 bit of it(as the library functions in mikroC PRO i am using can handle upto 8 bit) ..How to do that???
Last edited: