Home › Forums › Microcontrollers › PIC Microcontroller › PI controller using PIC16F876A
- This topic has 5 replies, 2 voices, and was last updated 9 years, 10 months ago by
Ligo George.
-
AuthorPosts
-
September 6, 2014 at 12:45 pm #8887
biswaIITH
Participanthello ligo,,i am trying to implement PI controller for controlling the output voltage of a dc/dc converter using PIC16F876A .
This z d code what i have writtenfloat adc; float set_ref=60; float err_val=0,I_err=0; float current_duty_2=10; float Kp=.01,Ki=2; float P_term=0,I_term=0; float Sum_err=0; void main() { ADCON1=0x81; TRISA=0xFF; TRISC.F1=0; PWM2_Init(100000); PWM2_Start(); PWM2_Set_Duty(current_duty_2); while(1) { adc=ADC_Read(1)/4; err_val=adc-set_ref; P_term=Kp*err_val; I_err=I_err+err_val; if(I_err>10) I_err=10; else if(I_err255) Sum_err=255; else if(Sum_err<0) Sum_err=0; current_duty_2=Sum_err; PWM2_Set_Duty(current_duty_2); } }
MY DOUBT IS IN THE WHILE LOOP AFTER EXECUTING THE LAST INSTRUCTION i.e. PWM2_Set_Duty(current_duty_2), IT WILL GO BACK TO THE STARTING OF THE LOOP TO EXECUTE THE INSTRUCTION adc=ADC_Read(1)/4; AT THE SAME TIME IT SHOULD ACCEPT THE ADC VALUE. WHAT I MEAN IS, ADC CONVERSION TIME AND EXECUTION TIME FOR REST OF INSTRUCTIONS MUST BE SAME.I AM CONFUSED AS TO HOW TO DO THAT. WE MAY FIX ADC CONVERSION TIME BUT THE EXECUTION TIME FOR THE REST OF INSTRUCTIONS MAY VARY IN EACH ITERATION AS IT CONTAINS LOOPS AND CONDITIONS. I WANT BOTH EXECUTION AND INSTRUCTION TO HAPPEN SYNCHRONOUSLY.
September 6, 2014 at 6:47 pm #9025Ligo George
KeymasterThen you should write your own ADC Codes.
Try the following link, it uses PIC 16F877A and Hi-Tech C.
Using ADC with PIC Microcontroller with Hi-Tech CSeptember 28, 2014 at 1:14 am #9030biswaIITH
ParticipantCan we compile the above hitech c code in mikroc 6.4 …thanksss
September 28, 2014 at 2:43 pm #9031Ligo George
KeymasterYou need to make some changes as given below.
void myADC_Init() { ADCON0 = 0x41; //ADC Module Turned ON and Clock is selected ADCON1 = 0xC0; //All pins as Analog Input //With reference voltages VDD and VSS } unsigned int myADC_Read(unsigned char channel) { if(channel > 7) //If Invalid channel selected return 0; //Return 0 ADCON0 &= 0xC5; //Clearing the Channel Selection Bits ADCON0 |= channel<<3; //Setting the required Bits Delay_ms(2); //Acquisition time to charge hold capacitor ADCON0.F2 = 1; //Initializes A/D Conversion while(ADCON0.F2); //Wait for A/D Conversion to complete return ((ADRESH<<8)+ADRESL); //Returns Result }
October 12, 2014 at 4:14 pm #9555biswaIITH
ParticipantADCON0 &= 0xC5; //Clearing the Channel Selection Bits
ADCON0 |= channel< <3; //Setting the required Bitswhy are these two instructions required???I mean to say what is the need of clearing and setting the channel selection bits again n again..We can select once ryt!!!correct me if i am wrong
October 16, 2014 at 7:49 pm #9654Ligo George
KeymasterNote that required bits are SET using OR Operator, so we should clear previously set bit if any using AND Operator.
-
AuthorPosts
- You must be logged in to reply to this topic.