Full code is:
// Descritpion: Program to read voltage of solar panel and adjust the PWM accordingley to produce 100V at the output.
// Author:
// Version: v.1
#include <pic.h>
const char look1 []= {
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04}; // look up table array for ccpr1l.
const char look2 []= {
0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C,
0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C,
0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C,
0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C, 0x2C,
0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C, 0x3C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C,
0x2C, 0x2C, 0x2C}; // look up table array for ccp1con.
void main(void);
void Initialize_ADC(void);
void a2d_conversion(void);
void interrupt isr (void);
unsigned char bodge;
void main()
{
unsigned char dig_input;
unsigned int george;
Initialize_ADC();
//Read_ADC_input_Vin
while(1)
{
//Set ADCON0
ADCON0 = 0x80; //clear ADCON0 A2D starts sampling again, selects A2D pin AN0.
a2d_conversion();
dig_input = ADRESH;
if (dig_input<128) //if solar panel (Vin) to boost converter is less than 10V don't boost.
{
CCPR1L = 0x64; //sets pwm to 100% this gets inverteted to 0% by driver i.e don't boost.
CCP1CON = 0x0C;
}
else
{
dig_input -= 128; // scaling dig_input to number between 0-127 (128 values)
george = dig_input + bodge;
if (george < 128)
{george = 0;}
else
{george -= 128;}
CCPR1L = look1[george];
CCP1CON = look2[george];
}
}
}
void Initialize_ADC()
{
TRISA = 0x01;
TRISC = 0x00;
PORTC = 0x00;
PR2 = 0x18;
T2CON = 0x05;
OPTION = 0x00;
INTCON = 0x00;
//Set ADCON1
ADCON1 = 0x00; // clear ADCON1 register.//
ADCON1 = 0x02; // set PCFG1 bit i.e AN0 is analogue input.
GIE = 1;
PEIE = 1;
TMR0IE = 1;
OPTION = 0x02;
TMR0 = 0x82;
//intialise Global Variables
bodge = 0x80;
}
void a2d_conversion()
{
unsigned char delay = 0x05;
//Read_ADC_input_Vout
ADON = 1; // turn A2D on
while (delay != 0)
{
delay -= 1;
}
ADGO = 1; //captures analogue voltage on pin.
while (ADGO)
{
//dig_output = 0x00;
}
};
void interrupt isr()
{
unsigned char dig_output;
// Output error voltage correction
ADCON0 = 0x88; //clear ADCON0 A2D starts sampling again, selects A2D pin AN0.
a2d_conversion();
dig_output = ADRESH;
if (dig_output<96) // output voltage is less than 100V
{
-- bodge; // increment duty cycle
}
else if (dig_output>102) // output voltage is greater then 100V
{
++ bodge; // decrement duty cycle
}
TMR0 = 0x82;
TMR0IF = 0;
}