Hi guys i am doing my project upon MPPT i am using PIC16F877A microcontroller i have written the code in C language in MPLabs but i am not able to see any PWM waveform through my circuit .PLEASE HELP ME ,i am in dire need.
Certain specifications
crystal oscillator frequency = 20MHz
PWM frequency = 100KHz
below is the given code
#include <pic.h>
__CONFIG(0x1832);
unsigned int Power, Power_Old;
unsigned char Voltage, Current, Voltage_Old, Current_Old, PWM = 0x1F, PWM_MAX = 0xFA, PWM_MIN = 0x05, FLAG, i;
void DELAY(void)
{
unsigned char a;
for (a=0; a<1; a++);
}
void main(void)
{
CCP1CON = 0;
PR2 = 0b00001100; /* Set Time Period Value */
TRISCbits.TRISC2=0; /* Set RC2 as an output pin */
T2CON = 0x00; /* No Prescalers Timer 2 OFF */
CCP1CON = 0b00001100; /* PWM Mode */
CCPR1L = 0b00001011;
TMR2 = 0; /* Reset Timer 2 */
T2CONbits.TMR2ON = 1; /* Timer 2 ON */
FLAG = 1; /*shows last increment/decrement*/
for(i=1; i>0; i++){
Power_Old = Power; /* Set panel power from previous cycle/
TRISAbits.TRISA1 = 1; /* AN0 analog input for voltage*/
ADCON0 = 0x81; /* 10000001 */
ADCON1 = 0xC4; /* 01000100*/
DELAY();
ADCON0bits.ADON = 1; /* Begin A/D Conversion*/
while(ADON); /* Wait for the conversion to end*/
Voltage = ADRESL;
Voltage += (ADRESH<<8);
TRISAbits.TRISA1 = 1; /* AN1 analog input for current*/
ADCON0 = 0x89; /* 10001001 1st 2 bits fosc/64 along with ADCON1, next 3 select channel AN1, last digit turns ADC on */
ADCON1 = 0xC4; /* 01000100 Left justify, fosc/64, 3 analog inputs, Vref+ = VDD, Vref- = VSS */
DELAY(); /* Gives ADC time to sample*/
ADCON0bits.ADON = 1; /* Begin A/D Conversion*/
while(ADON); /* Wait for the conversion to end*/
Current = ADRESL;
Current += (ADRESH<<8);
Power = Voltage * Current;
if(FLAG == 1){ /* Compare Voltage and power to previous and increment/decrement DUTY cycle*/
if(Power - Power_Old > 0){
PWM++;
FLAG = 1;
}
else{
PWM--;
FLAG = 0;
}
}
if(FLAG == 0){
if(Power - Power_Old > 0){
PWM--;
FLAG = 0;
}
else{
PWM++;
FLAG = 1;
}
}
if(PWM > PWM_MAX){ /* Keeps DC between 0 and 95% */
PWM = PWM_MAX;
}
if(PWM < PWM_MIN){
PWM = PWM_MIN;
}
PIR1bits.TMR2IF = 0; /* clear timer 2 flag*/
while(PIR1bits.TMR2IF==0); /* wait for end of period*/
}
}
thanks very much indeed for your help!!!!
Certain specifications
crystal oscillator frequency = 20MHz
PWM frequency = 100KHz
below is the given code
#include <pic.h>
__CONFIG(0x1832);
unsigned int Power, Power_Old;
unsigned char Voltage, Current, Voltage_Old, Current_Old, PWM = 0x1F, PWM_MAX = 0xFA, PWM_MIN = 0x05, FLAG, i;
void DELAY(void)
{
unsigned char a;
for (a=0; a<1; a++);
}
void main(void)
{
CCP1CON = 0;
PR2 = 0b00001100; /* Set Time Period Value */
TRISCbits.TRISC2=0; /* Set RC2 as an output pin */
T2CON = 0x00; /* No Prescalers Timer 2 OFF */
CCP1CON = 0b00001100; /* PWM Mode */
CCPR1L = 0b00001011;
TMR2 = 0; /* Reset Timer 2 */
T2CONbits.TMR2ON = 1; /* Timer 2 ON */
FLAG = 1; /*shows last increment/decrement*/
for(i=1; i>0; i++){
Power_Old = Power; /* Set panel power from previous cycle/
TRISAbits.TRISA1 = 1; /* AN0 analog input for voltage*/
ADCON0 = 0x81; /* 10000001 */
ADCON1 = 0xC4; /* 01000100*/
DELAY();
ADCON0bits.ADON = 1; /* Begin A/D Conversion*/
while(ADON); /* Wait for the conversion to end*/
Voltage = ADRESL;
Voltage += (ADRESH<<8);
TRISAbits.TRISA1 = 1; /* AN1 analog input for current*/
ADCON0 = 0x89; /* 10001001 1st 2 bits fosc/64 along with ADCON1, next 3 select channel AN1, last digit turns ADC on */
ADCON1 = 0xC4; /* 01000100 Left justify, fosc/64, 3 analog inputs, Vref+ = VDD, Vref- = VSS */
DELAY(); /* Gives ADC time to sample*/
ADCON0bits.ADON = 1; /* Begin A/D Conversion*/
while(ADON); /* Wait for the conversion to end*/
Current = ADRESL;
Current += (ADRESH<<8);
Power = Voltage * Current;
if(FLAG == 1){ /* Compare Voltage and power to previous and increment/decrement DUTY cycle*/
if(Power - Power_Old > 0){
PWM++;
FLAG = 1;
}
else{
PWM--;
FLAG = 0;
}
}
if(FLAG == 0){
if(Power - Power_Old > 0){
PWM--;
FLAG = 0;
}
else{
PWM++;
FLAG = 1;
}
}
if(PWM > PWM_MAX){ /* Keeps DC between 0 and 95% */
PWM = PWM_MAX;
}
if(PWM < PWM_MIN){
PWM = PWM_MIN;
}
PIR1bits.TMR2IF = 0; /* clear timer 2 flag*/
while(PIR1bits.TMR2IF==0); /* wait for end of period*/
}
}
thanks very much indeed for your help!!!!