Wednesday, May 30, 2012

AVR 9V battery charger





Several devices use the 9V batteries and the rechargeable NiMh version is a good option. 
There are only a few commercial chargers for this type of 9V block available, and most of them are either expensive or of low quality and will damage your rechargeable battery.
Here comes my version of charger with the following features:
- input voltage from 12 to 42V
-constant current charge 
-micro-controller verifies the charge and switches charging off when a certain voltage of the battery is reached
- battery voltage displayed on LCD during charge
-low cost - about 10$ in total




Details:

The AVR micro controller measures the voltage of the battery over R4 and R3. Before measurement is taken, the charging voltage is switched off by the micro controller via Q1.
Q1 can be any P-channel FET, in my case I use an smd version in a SOT8 package which contains 1 N-channel and 1 P-channel FET. 
IC6 is just a 5V linear voltage regulator. In my case a LM1117 smd but any other 5V regulator would do the job. You can also use LF50, 7805, LP2950 etc.
The LCD is a standard HD47780 compatible LCD, in my case with 8x2. R7 for the LCD contrast, can be replaced by a fixed resistor connected to ground. Try values from 600 Ohm to 6k .
Charging current is limited to a maximum of 25mA by the resistor and LM117 combination at the output.


http://www.gutenberg.org


Here comes the bascom source code:

 $regfile = "attiny44.dat"
$crystal = 978149                                           '1001625                                                   500 in 8min56s = 536s /128x = 65536x timer =
$hwstack = 20
$swstack = 20
$framesize = 20


Dim A As Byte                                               'counter for 10x get ADC

Dim Voltage As Word                                         'calculated voltage from ADCwert
Dim Adcwert As Word                                         'For Battery Voltage
Dim Timecount As Word
Dim Displayvolt As String * 3                               'convert number to string for decimal point value
Dim Voltstring As String * 4
 Config Portb.0 = Output                                    'switches charging mosfet to 0
 Config Porta.6 = Input                                     ' set tri-state adcpin to prevent voltage


'----------timer1 for voltage check time--------------

  Config Timer1 = Timer , Prescale = 64                     'configure timer for 2 seconds
Enable Timer1                                               'enable timer
On Timer1 Isr_timer1                                        'after 2 seconds iterrupt routine is executed  Isr_von_Timer1
Enable Interrupts
 'Timer1 = 1337


 'lcd ----------------------------------------------
Config Lcdpin = Pin , Db4 = Porta.3 , Db5 = Porta.2 , Db6 = Porta.1 , _
   Db7 = Porta.0 , E = Porta.4 , Rs = Porta.5
Config Lcd = 16 * 2



't---------------------config ADC----------------------

   Config Adc = Single , Prescaler = Auto , Reference = Avcc
   Start Adc



'(

 if voltage =  0 then Lcd Insert Battery
 if voltage < 10v then start charge via Pb.0 = 0
if voltage > 10v then display "battery full" and finish



')



'----------main loop----------------------------
  Do


  Wait 2



  Loop







  Isr_timer1:



   '----------Measure battery voltage------------

    Portb.0 = 0
    Config Portb.0 = Input                                  'measuring of voltage every 2 seconds while not charging ,switch mosfet off, no charge
  Waitms 5


    Adcwert = 0
   For A = 1 To 10
    Adcwert = Adcwert + Getadc(6)                           'pin7
    Next A


  '---------display state ---------------
     If Adcwert > 3700 Then
    Cls
                                      'when voltage is over 370= 10V, battery full, end charge


    Lcd "full"
    End If

  If Adcwert = 0 Then
  Cls

 Locate 1 , 1
 Lcd "insert"
 Locate 2 , 1
 Lcd "battery"
 End If


 If Adcwert < 3701 And Adcwert > 0 Then                     '3701 =10V

  Config Portb.0 = Output
    Portb.0 = 1                                             'Switch Mosfet On , Charge
 Cls

  Voltage = Adcwert / 37
      Displayvolt = Str(voltage)
            Voltstring = Format(displayvolt , " 0.0")

            Locate 1 , 1
            Lcd "charging"
            Locate 2 , 1
    Lcd Voltstring ; " V"


    End If


    Return



    Finish:


   Portb.0 = 0                                              'switch mosfet off, no charge


    Cls

    Lcd "full"
    Wait 2

    Return