djc
Advanced Member level 1
Plz look at the revised code, umcommented
In the loop named LOOP---1, I am first dividing the 10 bit ADC value by 4 and adding two constants k and l to it to get the PWM width value. Such that if ADC value for battery is 462 which is a value for 13v, then 425/4 = 106+(Initial value of K i.e 14)=120, which will be minimum pulse width at 13 v battery voltage to maintain 9v across load. But here issue is constant K and l will not vary according to ADC value so constant value will not be added accordingly. Timer 0 is loaded in interrupt routine.. Hardwae is fixed can't change anything.
Code:
//#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT, PUT
#define ON 0
#define OFF 1
#define timer_val 0
#define BATTERY_CHARGE PORTC.B5
#define LOAD PORTC.B4
unsigned int Panel_Value, Battery_Value,FLAG_1,FLAG_2=0,FLAG_3,FLAG_4,FLAG_5,FLAG_6,FLAG_7,FLAG_8;
//int OnPulse = 217;
//int OffPulse = 38;
//char TOG = 0;
unsigned int i = 305,j=1, factor = 0,divide_val=0,k=14,l=3;
void interrupt(){
if(INTCON.T0IF){
T0IF_bit = 0;
TMR0 = timer_val;
}
if(FLAG_1 == 1){
LOAD = ON;
//FLAG_2 = 1;
}
}
void main() {
TRISA = 0xff; //port A as input
TRISC = 0x00; //port C as output
PORTC = 0X00; //INITIAL VALUE ON PORTC
ADCON0 = 0b00000000; //Left Justified
ADCON1 = 0b00110000; //Clock derived from internal RC oscilator
ANSEL = 0b00000011; //channel AN0 and AN1 as analog input
INTCON = 0b10100000;
INTCON.T0IF = 0;
//CMCON0 = 0x07; //Disable the comparators
T0CS_bit = 0;
PSA_bit = 0; //Prescaler is assigned to Timer0 module
PS0_bit = 0; //TMR0 rate 1:1
PS1_bit = 0;
PS2_bit = 0;
//TMR0 = timer_val;
while(1){
Panel_Value = ADC_Read(0);
Battery_Value = ADC_Read(1);
if(Panel_value>=122){ //AD Value for 3 volt
BATTERY_CHARGE = ON;
if(Battery_Value>=430){ //AD Value for 12v
BATTERY_CHARGE = OFF;
}
}
if(Battery_Value > 430){
BATTERY_CHARGE = OFF;
}
if(Panel_Value <= 30 ){ //Panel AD value for 1v <=40
BATTERY_CHARGE = OFF;
LOAD = ON;
}
if(Panel_Value > 321){ //Panel Voltage 9 v
LOAD = OFF;
}
if(Battery_Value <=425 && Battery_Value > 310){ /////////////LOOP ------(1)
FLAG_1 = 1;
FLAG_2 = 1;
divide_val = ((Battery_Value/4) + k);
if(divide_val>=200){
divide_val = divide_val + l;
l=l+3;
if(l==42){
l = 3;
}
}
if(TMR0 >= divide_val){
LOAD = OFF;
k++;
//FLAG_1=0;
}
if(k>=134){
k=14;
FLAG_1 = 0;
}
}
if(Battery_Value <= 310 ){ //Battery ADC value for 9v 310,309
while(Battery_Value<=400){
Battery_Value = ADC_Read(1);
LOAD = OFF;
}
LOAD = ON;
}
}
}
In the loop named LOOP---1, I am first dividing the 10 bit ADC value by 4 and adding two constants k and l to it to get the PWM width value. Such that if ADC value for battery is 462 which is a value for 13v, then 425/4 = 106+(Initial value of K i.e 14)=120, which will be minimum pulse width at 13 v battery voltage to maintain 9v across load. But here issue is constant K and l will not vary according to ADC value so constant value will not be added accordingly. Timer 0 is loaded in interrupt routine.. Hardwae is fixed can't change anything.