PI controller using PIC16F876A

Home Forums Microcontrollers PIC Microcontroller PI controller using PIC16F876A

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #8887
    biswaIITH
    Participant

    hello 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 written

    float 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.

    #9025
    Ligo George
    Keymaster

    Then 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 C

    #9030
    biswaIITH
    Participant

    Can we compile the above hitech c code in mikroc 6.4 …thanksss

    #9031
    Ligo George
    Keymaster

    You 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
    }
    
    #9555
    biswaIITH
    Participant

    ADCON0 &= 0xC5; //Clearing the Channel Selection Bits
    ADCON0 |= channel< <3; //Setting the required Bits

    why 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

    #9654
    Ligo George
    Keymaster

    Note that required bits are SET using OR Operator, so we should clear previously set bit if any using AND Operator.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
>