Maximum power point tracker code

Discussion in 'The Projects Forum' started by rednwhite, Mar 1, 2011.

Thread Status:
Not open for further replies.
  1. rednwhite

    Thread Starter New Member

    Feb 24, 2011
    1
    0
    Hi guys,

    Trying to implement a maximum power point tracker (MPPT) in PIC C code. I have attached my code below. The inputs are AN0 to measure voltage of the cell and AN1 to measure the current of the cell, both measure between 0-5V. It is driving the mosfet in a boost converter which is fully working however the problem is that the duty cycle stays at the same value (the initial set value) suggesting the code only runs through once. I am new to programming so any help would be appreciated. I am using a 20MHz oscillator. Also forgot to say im using a thevenin equivalent to replicate the cell at the minute using a 30ohm resistor.

    Thanks

    Code ( (Unknown Language)):
    1.  
    2. #include <p18F258.h>
    3. #include <stdio.h>
    4.  
    5. unsigned int Panel_Power, Panel_Power_Old;
    6. unsigned char Panel_Voltage, Panel_Current, Panel_Voltage_Old, Panel_Current_Old, DUTY = 0x1F, DUTY_MAX = 0xFA, DUTY_MIN = 0x05, FLAG, i;
    7.  
    8. #pragma config WDT = OFF
    9.  
    10. void DELAY(void)
    11. {
    12.     unsigned char a;
    13.     for (a=0; a<1; a++);
    14. }
    15.  
    16.  
    17. void main(void)
    18. {
    19.     CCP1CON = 0;
    20.     PR2 = 0x3F;                 /* Set Time Period Value */
    21.     TRISCbits.TRISC2=0;        /* Set RC2 as an output pin */
    22.     T2CON = 0x00;             /* No Prescalers Timer 2 OFF */
    23.     CCP1CON = 0x0C;         /* PWM Mode */
    24.     CCPR1L = DUTY;                            
    25.     TMR2 = 0;                    /* Reset Timer 2 */
    26.     T2CONbits.TMR2ON = 1;        /* Timer 2 ON */        
    27.     FLAG = 1; /*shows last increment/decrement*/
    28.  
    29. for(i=1; i>0; i++){
    30. Panel_Power_Old = Panel_Power;    /* Set panel power from previous cycle
    31.  
    32. TRISAbits.TRISA0 = 1;             /* AN0 analog input for voltage*/
    33. ADCON0 = 0x81;                 /* 10000001 */
    34. ADCON1 = 0x44;                    /* 01000100*/
    35. DELAY();                    /* Gives ADC time to sample*/                    ADCON0bits.GO = 1;                        /* Begin A/D Conversion*/
    36.     while(ADCON0bits.DONE == 0);            /* Wait for the conversion to end*/
    37.     Panel_Voltage = ADRESH;                    /* Save voltage in ADRESH as Voltage_Step_Number*/
    38.  
    39.     TRISAbits.TRISA1 = 1;                     /* AN1 analog input for current*/
    40.     ADCON0 = 0x89;                             /* 10001001 1st 2 bits fosc/64 along with ADCON1, next 3 select channel AN1, last digit turns ADC on */
    41.     ADCON1 = 0x44;                            /* 01000100 Left justify, fosc/64, 3 analog inputs, Vref+ = VDD, Vref- = VSS */
    42.     DELAY();                                /* Gives ADC time to sample*/                    
    43.     ADCON0bits.GO = 1;                        /* Begin A/D Conversion*/
    44.     while(ADCON0bits.DONE == 0);            /* Wait for the conversion to end*/
    45.     Panel_Current = ADRESH;                    /* Save current in ADRESH as Panel_Current*/
    46.  
    47.     Panel_Power = Panel_Voltage * Panel_Current;
    48.  
    49.     if(FLAG == 1){                                        /* Compare Voltage and power to previous and increment/decrement DUTY cycle*/
    50.         if(Panel_Power - Panel_Power_Old > 0){    
    51.         DUTY++;
    52.         FLAG = 1;
    53.         }
    54.         else{
    55.         DUTY--;
    56.         FLAG = 0;
    57.             }
    58.     }
    59.  
    60.     if(FLAG == 0){
    61.         if(Panel_Power - Panel_Power_Old > 0){
    62.         DUTY--;
    63.         FLAG = 0;
    64.         }
    65.         else{
    66.         DUTY++;
    67.         FLAG = 1;
    68.             }
    69.     }
    70.  
    71.  
    72.     if(DUTY > DUTY_MAX){                /* Keeps DC between 0 and 95% */
    73.         DUTY = DUTY_MAX;    
    74.     }
    75.     if(DUTY < DUTY_MIN){
    76.         DUTY = DUTY_MIN;
    77.     }    
    78.  
    79.  
    80.     PIR1bits.TMR2IF = 0;                    /* clear timer 2 flag*/
    81.     while(PIR1bits.TMR2IF==0);                /* wait for end of period*/
    82.  
    83. }    
    84. }
    85.  
     
    Last edited by a moderator: Mar 1, 2011
  2. Tawfique

    New Member

    Mar 4, 2012
    1
    0
    rednwhite,
    i am also implementing mppt but with buck-boost converter. It will be very helpful to me if you upload your implemented mppt details.
    Regards
     
Loading...
Thread Status:
Not open for further replies.