void main() {
PORTE = 0;
TRISE = 0;
TRISA = 1;
PORTA = 0;
k_op = 204;
k_min = 179;
k_max = 230;
v_op = 819;
v_min = 717;
v_max = 922;
CCP4CON = 10;
CCPR4H = 0x9C;
CCPR4L = 0x40;
CCP4IE_bit = 1;
CCP4IF_bit = 0;
GIE_bit = 1;
PEIE_bit = 1;
T3CON = 0x41; //Prescaler 1:1, start Timer 1
ADCON1 = 0x0E; // Configure AN0 pins as analog
ADCON0 = 1; // Configure ADC to work with channel 0 (AN0-RA0)
delay_ms(1); // Required for ADC to work properly
ADCON2 = 0x06;
CMCON |= 7; // Disable comparators
GO_bit = 1; // Start convertion
CCP1CON = 0x0C; // CCP1 configured as PWM (RC2 pin)
k = 127;
PWM1_init (1000);
PWM1_set_duty (k); // initialize PWM with 80% duty cycle
PWM1_start();
while(1){
//Do whatever else is required
v = ADC_Read (0); // read boost's output voltage
/////* duty cycle control */////
if ( v >= 819 ) { // if output voltage >= reference value:
if ( v >= 922 ) { // if output voltage exceeds max voltge, then:
k = k_max; // output max duty cycle then,
k--; // decrease duty cycle
}
}
if ( v <= 819 ) { // if output voltage <= reference value:
if ( v <= 717 ) { // if output voltage is below min voltage, then:
k = k_min; // output min duty cycle then,
k++; // increase duty cycle
}
}
} // end while loop
} // end main loop