Charging Nickel-Metal Hydride
Charging Nickel-Metal Hydride
                                                                                   8-bit
Features                                                                           Microcontrollers
•   Fully Functional Design for Charging Nickel-Metal Hydride Batteries
•   High Accuracy Measurement with 10-bit A/D Converter
•   Modular “C” Source Code
•   Easily Adjustable Battery and Charge Parameters                                Application Note
•   Serial Interface for Communication with External Master
•   One-wire Interface for Communication with Battery EEPROM
•   Analogue Inputs for Reading Battery ID and Temperature
•   Internal Temperature Sensor for Enhanced Thermal Management
•   On-chip EEPROM for Storage of Battery and Run-Time Parameters
1 Introduction
This application note is based on the ATAVRBC100 Battery Charger reference
design (BC100) and focuses on how to use the reference design to charge Nickel-
Metal Hydride (NiMH) batteries. The firmware is written entirely in C language
(using IAR Systems Embedded Workbench) and is easy to port to other AVR®
microcontrollers.
This application is based on the ATtiny861 microcontroller but it is possible to
migrate the design to other AVR microcontrollers, such as pin-compatible devices
ATtiny261 and ATtiny461. Low pin count devices such as ATtiny25/45/85 can also
be used, but with reduced functionality.
                                                                                           Rev. 8098A-AVR-09/07
2 Theory of Operation
                         Battery charging is made possible by a reversible chemical reaction that restores
                         energy in a chemical system. Depending on the chemicals used, the battery will have
                         certain characteristics. A detailed knowledge of these characteristics is required in
                         order to avoid inflicting damage to the battery.
2.1.1 Safety
                         Nickel-based batteries are quite stable, but might vent gas or explode if severely
                         mishandled (short circuited, charged with the wrong charger, etc.) due to internal
                         pressure and heat build-up. Most batteries come with a safety vent, thermal
                         fuse/switch or pressure switch to help prevent this.
                         1
                             C represents the battery’s capacity per hour, i.e. mA.
 2             AVR463
                                                                                                   8098A-AVR-09/07
                                                                                                       AVR463
2.2.1 Safety
                               Towards the end of a charge cycle, the positive electrode of the battery will reach full
                               charge first, causing it to produce oxygen. To avoid pressure build-up in the cell, the
                               oxygen is recombined at the negative electrode to produce water. This process does
                               however generate heat, and it relies on the charge current to be limited so that the
                               oxygen generation rate is not greater than the recombination rate.
                               Should a pressure build-up occur, most batteries have a safety vent or even a
                               pressure switch that will cut off the charge current once the pressure gets too high.
                               The venting of gas deteriorates the battery as electrolyte is lost. More advanced
                               batteries have safety electronics that activate once pressure or temperature exceeds
                               a threshold.
                                                                                                                     3
 8098A-AVR-09/07
                                Table 2-1. Typical Charge Characteristics for Fast Charge
                                 Parameter                                                        Typical Value
                                 Charge time                                                       1.5 - 3 hours
                                 Charge current                                                     0.5 – 1 C
                                 Charge efficiency                                                  70 – 90 %
                                 Charge voltage                                                    1.4 – 1.6 V
                                 Temperature range                                               5 … 60 °C (max)
 4            AVR463
                                                                                                           8098A-AVR-09/07
                                                                                               AVR463
                        more information on the BC100, please refer to application note AVR451 - BC100
                        Hardware User's Guide [2].
2.3.1 Microcontroller
                        The BC100 hosts two microcontrollers: a master (ATmega644) and a slave (an
                        ATtiny85 or ATtiny861, by default). The master microcontroller is outside the scope of
                        this application but it may be noted that the microcontrollers are capable of
                        communicating with each other such that the master may request data from the slave
                        at any time.
                        The slave microcontroller is fully capable of handling all tasks related to battery
                        charging and it does not require a master microcontroller to be present. It constantly
                        scans the connectors for batteries and, if found, charges them when required. The
                        slave microcontroller also constantly monitors the hardware for any anomalies.
3.1 Configuration
                        The ATAVRBC100 Battery Charger reference design must be configured as detailed
                        below.
3.1.1 Microcontroller
                        The hardware should be populated as follows:
                        • Make sure socket SC300 is empty
                        • Populate socket SC301 with an ATtiny861
                        It is possible to use other AVR microcontrollers but this application has been
                        optimised for using ATtiny861. Pin compatible replacements such as ATtiny261 and
                        ATtiny461 [3] may be used if the compiled code size is decreased. This can be done
                        by increasing the optimisation of the compiler and by removing unwanted features
                        from the firmware.
                        Other microcontroller options include ATtiny25, ATtiny45 and ATtiny85 [4]. These (as
                        well as other 8-pin AVR microcontrollers) use the SC300 socket on BC100. It should
                                                                                                             5
 8098A-AVR-09/07
                              be noted that due to reduced pin count the 8-pin microcontrollers provide less
                              features than the default 20-pin.
3.1.3 Jumpers
                              The jumpers should be configured as follows:
                              • J405 & J406: Set jumpers to 1/4 (max measurable voltage 10V)
                              • For 300 mAh battery: J401, J404 and J408 should be set to enable Buck converter
                                C (20V / 1A)
                              • For 1300 mAh battery: J400, J401, J403, J408 and J407 should be set to enable
                                Buck converter B (30V / 2.5A)
                              Other configurations are possible, but may require firmware changes. See variable
                              VBAT_RANGE in file ADC.h.
3.1.4 Battery
                              For testing of this application, two generic 3-cell NiMH batteries from Minamoto were
                              used. The only data available were the specific capacity and nominal voltage. Both
                              batteries were rated at 3.6 V, with the respective capacities 300 mAh and 1300 mAh.
                              These batteries did not include Resistor ID, EPROM or NTC.
                              If a Resistor ID is not connected, or its value causes ADC saturation, default battery
                              data may be used. See Configuration on page 22.
 6              AVR463
                                                                                                       8098A-AVR-09/07
                                                                                              AVR463
3.1.6 NTC
                       Temperature measurement during fast charge of NiMH batteries is vital, and some
                       manufacturers therefore often include a thermistor in their batteries. Usually, these
                       have a negative temperature coefficient (NTC).
                       For this application, an RH16-3H103FB NTC from Mitsubishi was employed, and the
                       NTC lookup-table populated accordingly. The NTC should be connected as follows:
                       Table 3-2. Connecting NTC thermistor to charger
                        Battery Pin                           Charger Connector
                        NTC                                   NTC/RID
                        GND                                   BATTERY-
                       In this application, if an NTC is not connected, the ADC will be saturated and
                       temperature registered as -1 °C. This will halt the charging, unless a sub zero
                       minimum battery temperature has been set.
                       If an EPROM is not connected to the battery charger the application will simply
                       disregard its absence.
                                                                                                            7
 8098A-AVR-09/07
                   The table below lists the files that are relevant to the compiler project.
                   Table 4-1. Project files
                    File                 Type                        Note
                    ADC.c                C source code
                                                                     Functions related to A/D conversion
                    ADC.h                Header file
                    battery.c            C source code               Functions related to battery control &
                                                                     data acquisition, and definition of default
                    battery.h            Header file                 battery data
                    chargefunc.c         C source code
                                                                     Functions related to charging
                    chargefunc.h         Header file
                                                                     Enumerations for time.c and USI.c,
                    enums.h              Header file                 which are used in both Slave and Master
                                                                     (SPI communication)
                    main.c               C source code
                                                                     Main program / Program entry point
                    main.h               Header file
                    menu.c               C source code
                                                                     State machine definitions
                    menu.h               Header file
                    NIMHcharge.c         C source code               The charge state function for charging of
                    charge.h             Header file                 NiMH batteries
                                                                     Definitions of NiMH cell & battery
                    NIMHspecs.h          Header file
                                                                     specifications
                    OWI.c                C source code
                                                                     Functions related to one-wire interface
                    OWI.h                Header file
                    PWM.c                C source code               Functions related to generating pulse-
                    PWM.h                Header file                 width modulated output
4.1 Overview
                   The firmware integrates all functions required to charge two NiMH batteries. Batteries
                   are connected to separate ports such that one may be charged while the other is idle.
                   The firmware is fully automated and capable of stand-alone battery monitoring and
                   charging but it may also be used together with a master microcontroller, such as the
                   one implemented in BC100.
                   By default, the firmware fits into an ATtiny861 (build option: debug) or an ATtiny461
                   (build option: release). Memory requirements of the firmware are summarised in the
                   table below.
 8        AVR463
                                                                                                     8098A-AVR-09/07
                                                                                                             AVR463
                    Table 4-2. Memory requirements of firmware
                     Build option                           Memory                                Approximate value
                                                            CODE (Flash)                               5800 bytes
                     Debug                                  DATA (SRAM)                                 270 bytes
                                                            XDATA (EEPROM)                              130 bytes
                                                            CODE (Flash)                               3900 bytes
                     Release                                DATA (SRAM)                                 270 bytes
                                                            XDATA (EEPROM)                              130 bytes
                    Figure 4-1. Flow chart of main function, including the state machine
                                         main()
                    Upon return, the state machine expects the function to indicate the next state as a
                    return argument. The recognised return codes are described in the table below.
                    Table 4-3. State machine codes (see source code, menu.h)
                     Label (1)                         Related Function (2)         Description
                     INIT                              Initialize()                 Entry state
                     BATCON                            BatteryControl()             Check hardware and batteries
                     PREQUAL                           Charge()                     Raise battery voltage, safety check.
                     SLEEP                             Sleep()                      Low power consumption mode
                     FASTCHARGE                        Charge()                     Charge with constant current at 1.0 C.
                     TRICKLECHARGE                     Charge()                     Charge with constant current at 0.1 C.
                     ENDCHARGE                         Charge()                     End of successful charge
                     DISCHARGE                         Discharge()
                     ERROR                             Error()                      Resolve error, if possible
                     Notes:          1. Name of label, excluding leading “ST_”
                                     2. Function name, as declared in source code
                                                                                                                             9
 8098A-AVR-09/07
                         State functions are described in the following sections.
4.2.1 Initialize()
                         The initialisation function is the first state function that will be executed after device
                         reset. The flow chart of the function is shown in the figure below.
Return(BATCON)
                         The initialisation function always exits with the same return code, pointing to the state
                         function for battery control.
4.2.2 BatteryControl()
                         The battery control function verifies that jumpers are set correctly and then checks to
                         see if there are any enabled batteries present that require charging. The program flow
                         is illustrated in the figure below.
  10            AVR463
                                                                                                     8098A-AVR-09/07
                                                                                              AVR463
                   Figure 4-3. Flow chart of battery control function
                             BatteryControl()
JumperCheck()
                             JumperCheck()
                                                       NO
                                 OK?
                                                               Return(ERROR)
                                  YES
                              Any batteries
                                                       NO
                               enabled?
                                  YES
                                                               Return(ERROR)
                              Any battery:
                           Status OK and not           NO
                                charged
Return(PREQUAL)
4.2.3 Charge()
                   The charge function contains the charging algorithm divided into stages. For this
                   application, it has four stages:
                       •      Prequalification – If the battery voltage is between 0.8 V and 1.0 V, charge at
                              a low rate like 0.1 C for 2 minutes. If the voltage doesn’t rise to 1.0 V within
                              the time limit, the battery is likely damaged. Limit max. temperature to 35 °C.
                       •      Fast charge – Charge at 1.0 C for 1.5 hours at maximum, and terminate
                              when voltage drops 10 – 15 mV per cell or the rate of temperature increase
                              reaches 1 °C per minute. Limit maximum temperature to 50 °C.
                       •      Top-up charge – Top the battery up with a 0.1 C charge current for 30
                              minutes. Limit maximum temperature to 50 °C.
                       •      End charge – Decide whether to go into the sleep state or to attempt a
                              charge of the other battery.
                   ChargeParameters and HaltParameters are central variables in this function. After
                   each stage, the function returns the next desired state to main(). The program flow of
                   this state function is illustrated in the figure below.
                                                                                                           11
 8098A-AVR-09/07
Figure 4-4. Flow chart of the charge state function
                                                                   Charge()
  Set charge current to 0.1 C.       Set charge current to 1.0 C              Set charge current to 0.1 C.
                                                                                                               Stop PWM output.
    (BattData.Capacty / 10)             (BattData.Capacity)                    (BattData.Capacity / 10)
 Flag that charging should halt     Flag that charging should halt
                                                                                   Reset VBATMax.
 once voltage reaches limit or      on maximum voltage, voltage
    time runs out, and that           drop, timeout or maximum                                                 Is the other battery
                                                                                                                                               NO
 timeout means that battery is             temperature rise.                                                        enabled?
           exhausted.
                                                                                Start charge timer with
                                                                                   30 minute limit.
                                      Set maximum voltage and
                                                                                                                      YES
   Set voltage limit to defined        voltage drop limit to the
              limit.                       defined values.
 (BAT_VOLTAGE_PREQUAL)                (BAT_VOLTAGE_MAX &                        Callt ConstantVoltage()       Set ST_BATCON as          Set ST_SLEEP as next
                                      BAT_VOLTAGE_DROP)                          to continue charging,             next state.                 state.
                                                                                get next state in return.
4.2.4 Sleep()
                                              The application enters sleep mode when all batteries have been fully charged. It
                                              wakes up at regular intervals to check the current status of the batteries. Sleep mode
                                              is terminated as soon as any battery requires charging.
 12                 AVR463
                                                                                                                                             8098A-AVR-09/07
                                                                                       AVR463
                   Sleep mode is illustrated in the flow chart below.
                                   Actual battery
                                                             YES
                                     charged?
                                                            Return(BATCON)
                                            NO
                                    First battery
                                       actual?
                                                               NO
                                           YES
4.2.5 Error()
                   Program flow is diverted here when an error has occurred. The error handler contains
                   some simple algorithms that try to resolve the most common problems. Program
                   execution will exit the error handler when all sources of error have been cleared.
                   The program flow is illustrated in the figure below.
                                                                                                    13
 8098A-AVR-09/07
              Figure 4-6. Flow chart of error handler
                                       Error()
                                      Jumper
                                     mismatch                  YES
                                       error?
                                                               Clear bit in error flag
NO Check jumpers
                                       NO
                                     batteries                 YES
                                      error?
                                                                   Any batteries
                                                                                           YES
                                                                    enabled?
                                        NO
                                                                                         Clear bit in error flag
                                                                         NO
                                       PWM
                                      control                YES
                                      error?
NO
                                      Battery
                                    temperature              YES
                                       error?
                                                           Clear bit in error flag
                                     Battery
                                    exhausted                YES
                                      error?
NO
                                     Any error
                         Yes                                  NO
                                     flags set?
Return(INIT)
14   AVR463
                                                                                                                   8098A-AVR-09/07
                                                                                                                  AVR463
4.3 Charging Functions
                                 These functions are called by Charge() after all parameters have been set.
ConstantCurrent()
                                              Wait for
                                                ADC
                                            conversions
                                            to complete.
                                                YES
                                                                                                 Start timers
                                          Flag that Master                                         again.
                                    NO                                       NO
                                          MCU stopped the
                                             charging.
Stop timers. NO
                                                                                                 Decrement
                                                                         Current above
                                                                                         YES     PWM duty
                                                                          hysteresis?
                                                                                                   cycle.
NO
HaltNow()?
YES
                                                                                                                       15
 8098A-AVR-09/07
4.3.2 Charge Halt Determination
                              Charge halt is determined by HaltNow(). This function is called by ConstantCurrent()
                              and ConstantVoltage() every time they loop, to decide if a stage of charging is done.
                              With the variable HaltParameters the user can specify at what terms the charging
                              should be halted, and if an error should be flagged if e.g. the time limit expires. An
                              error flag will also result in ST_ERROR being set as the next state, thereby aborting
                              the charge. If no errors are flagged, the next desired state, set earlier in Charge(), will
                              apply.
                              Lastly, the function checks if temperature is within limits, if the battery is OK and if
                              mains voltage is above minimum. Should any of these tests fail, the next state is set
                              to an appropriate error handler (ST_ERROR, ST_INIT or ST_SLEEP) and charging is
                              aborted.
                                                               Output voltage
                                   Halt on voltage                                           Voltage drop above
                                                       YES   higher than stored   NO
                                   drop selected?                                             or equal to limit?
                                                                 maximum?
YES YES
NO
                                                              Output voltage
                                  Halt on maximum
                                                       YES   above or equal to    YES            Set Halt flag.
                                  voltage selected?
                                                                  limit?
                                                      NO
                                        NO
                                                              Output current
                                  Halt on minimum
                                                       YES   below or equal to    YES            Set Halt flag.
                                  current selected?
                                                                  limit?
                                                      NO
                                        NO
 16          AVR463
                                                                                                                   8098A-AVR-09/07
                                                                                                            AVR463
                  Figure 4-9. Flow chart for HaltNow() part 2
                                                       Halt on
                                                   temperature rise?
YES
                                                    Measured NTC
                                                  above stored NTC?
                                                         NO
                                YES
                                                                                                                           17
8098A-AVR-09/07
              Figure 4-10. Flow chart for HaltNow() part 3
Halt on timeout?
YES
YES
                                 NO                     Flag battery
                                                      exhaustion error
                                                          and set
                                                      ST_ERROR as
                                                         next state.
18   AVR463
                                                                           8098A-AVR-09/07
                                                                                                           AVR463
                       Figure 4-11. Flow chart for HaltNow() part 4
                             HaltNow()
                                                       Output voltage
                           Halt on voltage                                           Voltage drop above
                                               YES   higher than stored   NO
                           drop selected?                                             or equal to limit?
                                                         maximum?
YES YES
NO
                                                      Output voltage
                          Halt on maximum
                                               YES   above or equal to    YES            Set Halt flag.
                          voltage selected?
                                                          limit?
                                              NO
                                NO
                                                      Output current
                          Halt on minimum
                                               YES   below or equal to    YES            Set Halt flag.
                          current selected?
                                                          limit?
                                              NO
                                NO
                                                                                                                19
 8098A-AVR-09/07
                             Figure 4-12. Flow chart of ADC interrupt service routine
ADC_ISR()
Disable ADC
0x02 0x05
0x03
Update MUX
                                                                                        Signed
                                                           Set unsigned                                          Set signed
                                                                             NO       conversion        YES
                                                            conversion                                           conversion
                                                                                         next?
YES
                                                                                     Return from
                                                                                      interrupt
 20         AVR463
                                                                                                                                         8098A-AVR-09/07
                                                                                                       AVR463
                  Figure 4-13. Flow chart of USI overflow interrupt service routine.
                                                USI_OVF_ISR()
Update flags
                                                                                   (not implemented)
                                                  Set address
                    Set SRAM/EEPROM flag
Decrease counter
NO Counter Zero?
YES
                                                                                                                      21
8098A-AVR-09/07
5 Quick Start Guide
                      This section describes how to configure, create and download the software.
5.1.1 Configuration
                      The most important compile-time constants are listed in the table below.
 22          AVR463
                                                                                                      8098A-AVR-09/07
                                                                                                                AVR463
                                      Label                         Description
                                      RID[].Capacity                Battery capacity for given RID.
                                      RID[].Icharge                 Charge current for given RID.
                                      RID[].tCutOff                 Maximum charge time for given RID.
                                      RID[].IcutOff                 Charge termination current for given RID.
                                      NTC[]                         Temperature look-up table.
5.1.2 Compilation
                                     Before compiling the code the following configurations should be made.
Table 5-2. Compiler configuration.
 Section             Tab               Field                                              Value
                                                                                                    (1)
                                       Processor                                       ATtiny861
                     Target            configuration
                                       Memory model                                       Small
                                       Data stack                                         0x40
 General Options
                                       Return address                                       24
                     System            stack
                                       Enable bit                                        Selected
                                       definitions …
                                       Require                                           Selected
 C/C++ Compiler      Language
                                       prototypes
                     Output            Format                                         Other: ubrof8
                                                                                     -y(CODE)
 Linker
                     Extra Options     Command Line        -Ointel-extended,(DATA)=$EXE_DIR$\$PROJ_FNAME$_data.hex
                                                         -Ointel-extended,(XDATA)=$EXE_DIR$\$PROJ_FNAME$_eeprom.hex
 Notes:                1. Other options possible. See section 3.1.1 on page 5 for more information.
5.1.3 Programming
                                     The compiled code is conveniently downloaded to the target device using AVR Studio
                                     and a debugger or programming tool of choice, such as the JTAGICE mkII.
                                     Note that the compiled code contains EEPROM data that must be loaded to the target
                                     for the software to work. Answer OK when AVR Studio asks if EEPROM contents
                                     should be loaded. This is illustrated in the figure below.
                                                                                                                     23
 8098A-AVR-09/07
                         The program expects the use of the internal oscillator and that the clock signal is not
                         prescaled. Some fuse bits must be programmed to ensure proper program execution.
                         The fuse bit settings that deviate from the default are listed in the table below.
                         Table 5-3. Non-default fuse bit settings
                          Fuse Bit              Setting                     Description
                          CKDIV8                1 (unprogrammed)            Do not divide clock by eight
                          CKSEL3…0              0010                        Use internal oscillator
6 Known Limitations
                         Here are listed known limitations of the design.
 24        AVR463
                                                                                                       8098A-AVR-09/07
                                                                                           AVR463
                    When using Varta PoLiFlex batteries this means the largest battery size that can be
                    reliably sensed is 1000 mAh. For larger sense resistors / battery sizes the pull-up
                    resistor on BC100 must be changed. In addition, the software must be updated to
                    reflect the new pull-up resistor value.
7 References
                    1. “What’s the best battery?”. Retrieved August 1, 2007, from Battery University:
                       http://www.batteryuniversity.com/partone-3.htm
                    2. “AVR451 - BC100 Hardware User's Guide”. Available from Atmel web site:
                       http://www.atmel.com/products/avr/
                                                                                                         25
 8098A-AVR-09/07
Headquarters                          International
Atmel Corporation                     Atmel Asia                           Atmel Europe                          Atmel Japan
2325 Orchard Parkway                  Room 1219                            Le Krebs                              9F, Tonetsu Shinkawa Bldg.
San Jose, CA 95131                    Chinachem Golden Plaza               8, Rue Jean-Pierre Timbaud            1-24-8 Shinkawa
USA                                   77 Mody Road Tsimshatsui             BP 309                                Chuo-ku, Tokyo 104-0033
Tel: 1(408) 441-0311                  East Kowloon                         78054 Saint-Quentin-en-               Japan
Fax: 1(408) 487-2600                  Hong Kong                            Yvelines Cedex                        Tel: (81) 3-3523-3551
                                      Tel: (852) 2721-9778                 France                                Fax: (81) 3-3523-7581
                                      Fax: (852) 2722-1369                 Tel: (33) 1-30-60-70-00
                                                                           Fax: (33) 1-30-60-71-11
Product Contact
                                      Literature Request
                                      www.atmel.com/literature
Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any
intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN ATMEL’S TERMS AND
CONDITIONS OF SALE LOCATED ON ATMEL’S WEB SITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED
OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS,
BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the
contents of this document and reserves the right to make changes to specifications and product descriptions at any time without notice. Atmel does not make any
commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in,
automotive applications. Atmel’s products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life.
© 2007 Atmel Corporation. All rights reserved. Atmel®, logo and combinations thereof, AVR® and others, are the registered trademarks or
trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others.
8098A-AVR-09/07