Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] [PIC] Need help for PIC12F675 Based 12V battery charger

Status
Not open for further replies.

uzzalpic

Junior Member level 2
Joined
Mar 24, 2014
Messages
24
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Activity points
189
Hi all, I am trying to write code for a 12V battery charger used PIC12F675. Its working for charging and full charged but not working for low battery. I giving my project code, schematic and fuse in attachment. Please help me about this.

https://imgbox.com/K6Q5Xa7c

https://imgbox.com/2zuvEODA

Code:
#define ON 1
#define OFF 0

sbit low_led at GP4_bit;
sbit charge_led at GP5_bit;
sbit RELAY at GP1_bit;
sbit MOSFET at GP2_bit;

unsigned int adc_value =0;
unsigned char led_blink = 0;
double mains_voltage = 0.0;

void delay_start()
{
   int i;
   for(i=0; i<6; i++){

   charge_led = ~charge_led;
   delay_ms(1000);
   }
}


void InitTimer1(){
  T1CON              = 0x21;
  TMR1IF_bit  = 0;
  TMR1H              = 0x0B;
  TMR1L              = 0xDC;
  TMR1IE_bit  = 1;
  INTCON      = 0xC0;
}

void Interrupt() {
  if (TMR1IE_bit && TMR1IF_bit) {
    TMR1L = 0xDC;
    TMR1H = 0x0B;
    if (led_blink) { // use this to switch on-off the blinking in main()
       charge_led = ~charge_led;
    }
    TMR1IF_bit = 0;
  }
}


void main() {
 asm clrwdt
 OPTION_REG = 0x8F;
 CMCON = 0x07;
 ANSEL = 0x51;
 ADCON0 = 0x80;

 TRISIO = 0x09;
 GPIO = 0x00;
 delay_ms(50);

 InitTimer1();
 delay_start();
 ADC_Init();

 do{
    adc_value = ADC_Read(0);

      delay_ms(5);

    mains_voltage = (double)adc_value * 5.0 / 1023.0;

    


    if(mains_voltage <= 2.15)
    {
       led_blink = 1;
       RELAY = ON;
       MOSFET = ON;
       low_led = OFF;
    }
    delay_ms(100);
    if(mains_voltage > 2.15)
    {

      charge_led = ON;
      led_blink = 0;
      RELAY = OFF;
      MOSFET = ON;
      low_led = OFF;

    }
    else if(mains_voltage < 1.58)
    {
      charge_led = OFF;
      led_blink = 0;
      RELAY = OFF;
      MOSFET = ON;
      low_led = ON;

    }
    delay_ms(50);


  } while(1);

}
Fuse.jpgDiagram.jpg
 

I'm not sure I understand this design.

1. the battery appears to be backwards.
2. there is no mains connection so what is 'mains_voltage' supposed to represent?
3. why is there a 'delay_ms(100)' between mathematical checks of the same value?
4. if the measured voltage is <1.58 there probably isn't enough to feed to the regulator anyway. (shouldn't the input pin go to C1?)
5. what does Q2 actually do? It never changes state.

Brian.
 

I understand that the circuit is supplied by a center tapped transformer connected to J1. Besides obvious drawing errors like the mentioned reversed battery symbol, the main problem is probably that 5V logic supply is derived from the battery voltage instead of rectified mains voltage. If the battery voltage is too low, the processor never comes up.
 

Now What should I do?
 

Hardware:

1. reverse the battery symbol on the schematic, check you have it wired the right way around in the real circuit.
2. remove D6, it doesn't do anything useful.
3. connect U2 'VI' and C5 to the junction of D2, D3 and C1.
4. remove R5 it doesn't do anything.
5. remove R9 it doesn't do anything.
6. add a small signal diode, cathode to 5V+, anode to 'ADC' to protect the PIC from charge on C2.

Software:
1. remove the line 'Delay_ms(100);', it has no effect on the result.
2. ADC0 measures the battery voltage, not the mains voltage (which is on GP3 which has no ADC channel)
3. consider that you don't need to convert the ADC value to a voltage, it would be easier to use the raw ADC value in the comparisons.
4. you don't need to reset the watchdog timer if it is disabled in the config settings.

Brian.
 

Now What should I do?

Though I have studied your schematic. Unfortunately I could not understand what is expected from this circuit. Do you want to control charging by the relay? Then what is the function of MOSFET?
 

I collect this circuit from my local market. I want to write code like this circuit so that is work like that. Now I will say how this circuit works.
This circuit works in two mode:

A- When Circuit connected with transformer

1. When the circuit is run, the charging LED will blink three times with 1000ms delay.
2. After three times blink It will start blink again for 250ms. Relay and Moster will on. That means charging is start and output is off.
3. If Battery voltage exceeds adc 2.12v then the charging LED Blink will stop but charging LED stay ON. Relay will OFF but Mosfet stay On. That means battery full charged complete.

A- When Circuit not connected with transformer

1. When I start a DC fan with battery and the battery voltage drops below adc 1.58v then this low LED will on and the mosfet will be on which the output is closed.


WhatsApp Image 2017-10-13 at 11.38.25 AM.jpeg
 

I see. Now it is clear. You should use mains sense at GP3 bit so that when transformer is connected the MOSFET will not deliver current to load. As soon as mains fails the MOSFET will deliver current to load provided voltage of battery is within permissible limit. But you have not used the GP3 bit in your code.

In your given code, for any value of 'mains_voltage', the MOSFET will not deliver any current to load as 'MOSFET= ON'. The flow chart will be in two state - when 'GP3_bit ==1' and when 'GP3_bit==0'.
 

I shared my new code. Now low battery led working fine, charging also working fine but one problem now I faced, that is, when the battery charge is full, the relay chattering. But when the battery charge is fully charged, the relay will off and charge LED will On. So what's wrong in my code? Plz help me..



Code:
    if(GP3_bit == 1){
       if(adc_voltage <= 2.120){
         led_blink = 1;
         RELAY = ON;
         MOSFET = ON;
         low_led = OFF;
       }

    else  {
      charge_led = ON;
      led_blink = 0;
      RELAY = OFF;
      MOSFET = ON;
      low_led = OFF;
      }
    }



   if(GP3_bit == 0){
     if(adc_voltage <= 1.58100){

       charge_led = OFF;
       led_blink = 0;
       RELAY = OFF;
       MOSFET = ON;
       low_led = ON;
     }
    else
     led_blink = 0;
     charge_led = OFF;
     RELAY = OFF;
     MOSFET = OFF;
     low_led = OFF;
    }
   }
 

The chattering is probably because you have an absolute threshold voltage that operates the relay. When the voltage drops slightly the relay is turned on but doing so increases the voltage and turns it off again. You need to define two thresholds, a slightly higher one for turning the relay off then the one for turning it on again. Instead of chattering it will then cycle slowly between the two voltages, as the battery drops (say 0.25V) it gets recharged and as the voltage reaches maximum the charge is turned off again.

Brian.
 

The chattering is probably because you have an absolute threshold voltage that operates the relay. When the voltage drops slightly the relay is turned on but doing so increases the voltage and turns it off again. You need to define two thresholds, a slightly higher one for turning the relay off then the one for turning it on again. Instead of chattering it will then cycle slowly between the two voltages, as the battery drops (say 0.25V) it gets recharged and as the voltage reaches maximum the charge is turned off again.

Brian.

Hi Brian, Is there any way that will stop the relay after the full charged is complete?
 

Is there any way that will stop the relay after the full charged is complete?

Try this one.

if(GP3_bit == 1){
if(adc_voltage <= 2.000){
led_blink = 1;
RELAY = ON;
MOSFET = ON;
low_led = OFF;
}

else if (adc_voltage > 2.150 {
charge_led = ON;
led_blink = 0;
RELAY = OFF;
MOSFET = ON;
low_led = OFF;
}
else;
}



if(GP3_bit == 0){
if(adc_voltage <= 1.58100){

charge_led = OFF;
led_blink = 0;
RELAY = OFF;
MOSFET = ON;
low_led = ON;
}
else
led_blink = 0;
charge_led = OFF;
RELAY = OFF;
MOSFET = OFF;
low_led = OFF;
}
}
 

Hi swapan. I used this code. It's turning off the relay for some times but it is not a permanent solution. In my original circuit relay will be off when the battery is fully charged and remains off until the circuit restart. Any idea to make like my original circuit.
 

Hi Brian, Is there any way that will stop the relay after the full charged is complete?
Normally a fully charged battery has a slightly higher terminal voltage than it is rated at. For example a fully charged 12V battery may measure 13V before the load stabilizes it at 12V until it has discharged. What you have to do is decide what the fully charged voltage will be and disconnect the relay when it is reached. The relay shouldn't operate again until the voltage has dropped low enough that recharging is needed. As I posted earlier, you need two thresholds, one is the "I'm fully charged so turn off" voltage and the other is "I'm running low so charge me again" voltage.

Hint: Try disconnecting charge (relay off) before taking the voltage measurement so you read what is really across the battery rather than what the charger is pushing in to it.

Brian.
 

According to the circuit the power to 7805 is derived from Battery and when transformer is not connected (irrespective of relay state) then battery voltage should be minimum 7V otherwise 7805 output will be low and adc reference voltage changes and adc readings will be wrong.

GP3 pin is used to detect transformer connected or not that is mains presence.

The adc voltage when battery is connected and when not connected are different because when charging voltage is applied to battery it will be 14.6V (fully charged) and when transformer is not connected the adc input voltage will be different because the battery voltage will be 12.0V.

If you provide the correct adc voltages at which charging starts and at which it stops charging then I can fix the code. If relay is chattering then hysteresis has to be added.
 

Baileychic covers all the points made in the previous posts.

Additionally, you might consider a small extra modification: to deliberately introduce a load on the battery before measuring it's voltage. Basically, you reverse the relay connections so the common pin goes to the battery. Then you introduce a load resistor on the unused contact. When charging everything works as before but when measuring the voltage the charge is disconnected and a load introduced instead. It gives a better indication of the battery state of charge as the voltage may rise faster than it's storage capacity.

Whether it is a useful addition depends on the exact type of battery and whether it is normally left in the charger. Do be careful that the load isn't left connected if the charger is turned off!

Brian.
 

It's turning off the relay for some times but it is not a permanent solution.

Yes, it will be like that. I have given you the idea how to introduce software hysteresis in your code. If you like to switch on the charging relay at a level of 12V of the battery, then put the corresponding value in "if(adc_voltage <= 2.000)". You should know that when the battery is charged, its terminal voltage rises. As the charging relay is switched off at a specific voltage level, the terminal voltage of the battery gradually decreases. And when it reaches the threshold value of "2.000" relay is switched on again. Thus the chattering of the relay has become a periodic ON and OFF at specific interval.

In my original circuit relay will be off when the battery is fully charged and remains off until the circuit restart

What do you mean by "until the circuit restarts". Do you mean that the charging resumes when AC main is reset? Try to present your problem/requirement clearly.
 

If the battery voltage is less than 7.07V (considering the diode at 7805 input) then battery charger will not work. Relay can be turned on and hence charging whenever transformer is connected that is mains present.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top