chanchal.chauhan
Full Member level 4
- Joined
- Apr 23, 2009
- Messages
- 216
- Helped
- 5
- Reputation
- 10
- Reaction score
- 1
- Trophy points
- 1,298
- Location
- Hyderabad, India
- Activity points
- 2,539
measure charging voltage
Hi,
I am using PIC 18F1330 microcontroller for my battery charging application.
Here I am controlling Battery current in CC mode & Battery voltage in CV mode in closed loop.
But here in CV mode my battery current & Pulse is varying.
Below are the details of my code.
#include<htc.h>
void osc_init();
void delay(unsigned int k);
void adc_init();
void read_volt();
void read_current();
void pwm_init();
long Multiplication16bit(int Mutiplicand, int Multiplier);
const unsigned int ref_voltage=2867; //14.0v
const unsigned int ref_voltage1=2847;//13.9v
const unsigned int ref_current=1332; //6.5Amps
unsigned int duty_cycle=9280,duty_cycle1=960;
void main()
{
long gain,gain2;
unsigned int gain1,gain3;
TRISB=0b00000000;
TRISA=0b01010011;
osc_init(); // 16Mhz internal ocillator
adc_init(); // adc init
while(1)
{
read_volt(); //Vs
gain=Multiplication16bit(adc_voltage, 666);
gain=gain*5;
gain1=gain>>10; //Battery voltage
read_current();//Is
gain2=Multiplication16bit(adc_current,360);
gain2=gain2*5;
gain3=gain2>>10; //Battery current
if(gain1>=ref_voltage) //
{
flag=1;
if(gain1==ref_voltage)
{
duty_cycle;
duty_cycle1;
}
else
{
++duty_cycle; //Decrement
--duty_cycle1;
}
}
else //CC Mode
{
if(gain3>=ref_current)
{
flag=0;
if(gain3==ref_current)
{
duty_cycle;
duty_cycle1;
}
else
{
++duty_cycle; //Decrement
--duty_cycle1;
}
}
else
{
if(flag==1)
{
duty_cycle;
duty_cycle1;
if(gain1<ref_voltage1) //ref_voltage1
{
--duty_cycle;
++duty_cycle1;
}
}
else
{
--duty_cycle;
++duty_cycle1;
}
}
}
if(duty_cycle>=9728)
{
duty_cycle=9728;
duty_cycle1=512; //480
}
if(duty_cycle<=6400)
{
duty_cycle=6400; //6399
duty_cycle1=3840;
}
pwm_init(); //Pwm initilisation
}
}
void adc_init()
{
ADCON1=0b00001100;
ADCON2=0b10001010; //Tacq=1.2usec
ADCON0=0b00000000;
}
void read_volt()
{
i=0;
for(i=0;i<16;i++) //Averaging 16 times
{
ADCON1=0b00001100;
ADCON2=0b10001010; //Tacq=1.2usec
ADCON0=0b00000000; //AN0 voltage
ADON=1;
GODONE=1;
while(GODONE) continue;
// GODONE=0;
ADIF=0;
adc_voltage=0;
adc_voltage=(ADRESH<<Cool+ADRESL;
result_temp=adc_voltage+result_temp;
}
adc_voltage=result_temp/16;
result_temp=0;
}
void read_current()
{
i=0;
for(i=0;i<16;i++) // Averaging 16 times
{
ADCON1=0b00001100;
ADCON2=0b10001010; //Tacq=1.2usec
ADCON0=0b00000100; //AN1 current
ADON=1;
GODONE=1;
while(GODONE) continue;
ADIF=0;
adc_current=0;
adc_current=(ADRESH<<Cool+ADRESL;
result_temp1=adc_current+result_temp1;
}
adc_current=result_temp1/16;
result_temp1=0;
// flag=1;
}
Any help is greatly appreciative.
Regards
Chanchal
Hi,
I am using PIC 18F1330 microcontroller for my battery charging application.
Here I am controlling Battery current in CC mode & Battery voltage in CV mode in closed loop.
But here in CV mode my battery current & Pulse is varying.
Below are the details of my code.
#include<htc.h>
void osc_init();
void delay(unsigned int k);
void adc_init();
void read_volt();
void read_current();
void pwm_init();
long Multiplication16bit(int Mutiplicand, int Multiplier);
const unsigned int ref_voltage=2867; //14.0v
const unsigned int ref_voltage1=2847;//13.9v
const unsigned int ref_current=1332; //6.5Amps
unsigned int duty_cycle=9280,duty_cycle1=960;
void main()
{
long gain,gain2;
unsigned int gain1,gain3;
TRISB=0b00000000;
TRISA=0b01010011;
osc_init(); // 16Mhz internal ocillator
adc_init(); // adc init
while(1)
{
read_volt(); //Vs
gain=Multiplication16bit(adc_voltage, 666);
gain=gain*5;
gain1=gain>>10; //Battery voltage
read_current();//Is
gain2=Multiplication16bit(adc_current,360);
gain2=gain2*5;
gain3=gain2>>10; //Battery current
if(gain1>=ref_voltage) //
{
flag=1;
if(gain1==ref_voltage)
{
duty_cycle;
duty_cycle1;
}
else
{
++duty_cycle; //Decrement
--duty_cycle1;
}
}
else //CC Mode
{
if(gain3>=ref_current)
{
flag=0;
if(gain3==ref_current)
{
duty_cycle;
duty_cycle1;
}
else
{
++duty_cycle; //Decrement
--duty_cycle1;
}
}
else
{
if(flag==1)
{
duty_cycle;
duty_cycle1;
if(gain1<ref_voltage1) //ref_voltage1
{
--duty_cycle;
++duty_cycle1;
}
}
else
{
--duty_cycle;
++duty_cycle1;
}
}
}
if(duty_cycle>=9728)
{
duty_cycle=9728;
duty_cycle1=512; //480
}
if(duty_cycle<=6400)
{
duty_cycle=6400; //6399
duty_cycle1=3840;
}
pwm_init(); //Pwm initilisation
}
}
void adc_init()
{
ADCON1=0b00001100;
ADCON2=0b10001010; //Tacq=1.2usec
ADCON0=0b00000000;
}
void read_volt()
{
i=0;
for(i=0;i<16;i++) //Averaging 16 times
{
ADCON1=0b00001100;
ADCON2=0b10001010; //Tacq=1.2usec
ADCON0=0b00000000; //AN0 voltage
ADON=1;
GODONE=1;
while(GODONE) continue;
// GODONE=0;
ADIF=0;
adc_voltage=0;
adc_voltage=(ADRESH<<Cool+ADRESL;
result_temp=adc_voltage+result_temp;
}
adc_voltage=result_temp/16;
result_temp=0;
}
void read_current()
{
i=0;
for(i=0;i<16;i++) // Averaging 16 times
{
ADCON1=0b00001100;
ADCON2=0b10001010; //Tacq=1.2usec
ADCON0=0b00000100; //AN1 current
ADON=1;
GODONE=1;
while(GODONE) continue;
ADIF=0;
adc_current=0;
adc_current=(ADRESH<<Cool+ADRESL;
result_temp1=adc_current+result_temp1;
}
adc_current=result_temp1/16;
result_temp1=0;
// flag=1;
}
Any help is greatly appreciative.
Regards
Chanchal