Application Note

Z8 Encore!™-Based Battery Charger
AN013702-0403

ZiLOG Worldwide Headquarters • 532 Race Street • SanJose, CA 95126 Telephone:408.558.8500 • Fax:408.558.8300 • www.ZiLOG.com

Z8

Encore!™-Based

Application Note Battery Charger

This publication is subject to replacement by a later edition. To determine whether a later edition exists, or to request copies of publications, contact: ZiLOG Worldwide Headquarters
532 Race Street San Jose, CA 95126 Telephone: 408.558.8500 Fax: 408.558.8300 www.zilog.com

ZiLOG is a registered trademark of ZiLOG Inc. in the United States and in other countries. All other products and/or service names mentioned herein may be trademarks of the companies with which they are associated.

Information Integrity
The information contained within this document has been verified according to the general principles of electrical and mechanical engineering. Any applicable source code illustrated in the document was either written by an authorized ZiLOG employee or licensed consultant. Permission to use these codes in any form, besides the intended application, must be approved through a license agreement between both parties. ZiLOG will not be responsible for any code(s) used beyond the intended application. Contact the local ZiLOG Sales Office to obtain necessary license agreements.

Document Disclaimer
©2003 by ZiLOG, Inc. All rights reserved. Information in this publication concerning the devices, applications, or technology described is intended to suggest possible uses and may be superseded. ZiLOG, INC. DOES NOT ASSUME LIABILITY FOR OR PROVIDE A REPRESENTATION OF ACCURACY OF THE INFORMATION, DEVICES, OR TECHNOLOGY DESCRIBED IN THIS DOCUMENT. ZiLOG ALSO DOES NOT ASSUME LIABILITY FOR INTELLECTUAL PROPERTY INFRINGEMENT RELATED IN ANY MANNER TO USE OF INFORMATION, DEVICES, OR TECHNOLOGY DESCRIBED HEREIN OR OTHERWISE. Except with the express written approval ZiLOG, use of information, devices, or technology as critical components of life support systems is not authorized. No licenses or other rights are conveyed, implicitly or otherwise, by this document under any intellectual property rights.

AN013702-0403

Z8

Encore!™-Based

Application Note Battery Charger iii

Table of Contents
Abstract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Z8 Encore!™ Flash MCU Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Z8 Encore!™ Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Theory of Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Z8 Encore!™-Based Battery Charger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Hardware Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Software Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Appendix A—Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Appendix B—Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Appendix C—Schematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Appendix D—Flowcharts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Appendix E—Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Appendix F—Battery Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

AN013702-0403

Z8

Encore!™-Based

Application Note Battery Charger iv

List of Figures
Figure 1. Figure 2. Figure 3. Figure 4. Figure 5. Figure 6. Figure 7. Figure 8. Figure 9. Figure 10. Figure 11. Feedback Control System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Block Diagram of Battery Charger Hardware . . . . . . . . . . . . . . . . . . 6 Battery Charger Test Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 SLA Charging Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 NiCad Charging Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 NiMH Charging Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Schematic for Z8 Encore!™ Interface . . . . . . . . . . . . . . . . . . . . . . . 15 DC–DC Step-Down Converter and LED Indicator Port . . . . . . . . . . 16 Feedback Section and Battery Type Selector Jumper Settings . . . 17 Flowchart for the Main Routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Flowchart for the ISR Return Routine . . . . . . . . . . . . . . . . . . . . . . . 19

List of Tables
Table 1. Table 2. Table 3. Table 4. LED Status Indicators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Z8 Encore!™ Battery Charger Test Equipment . . . . . . . . . . . . . . . . . 9 List of References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

AN013702-0403

Z8

Encore!™-Based

Application Note Battery Charger 1

Abstract
This Application Note demonstrates a Z8 Encore!™-based battery charger that charges various rechargeable batteries in a fast, efficient, and safe manner. All of the important rechargeable battery types, such as Sealed Lead Acid (SLA), Nickel Cadmium (NiCad), Nickel Metal Hydride (NiMH), and Lithium Ion (Li-Ion), are addressed in this Application Note. The Z8 Encore!™-based charger manages each battery type according to its individual charging profile.

Z8 Encore!™ Flash MCU Overview
The Z8F640x/Z8F480x/Z8F320x/Z8F240x/Z8F160x products, hereafter referred to collectively as Z8 Encore!™, are based on the new 8-bit eZ8 CPU, and introduce Flash memory to ZiLOG’s extensive line of 8-bit microcontrollers. The Flash in-circuit programming capability allows for faster development time and program changes in the field. The new eZ8 core maintains backward compatibility with ZiLOG’s popular Z8® MCU. Featuring ZiLOG’s high performance register-to-register based architecture (eZ8), the new Z8 Encore!™ microcontrollers combine a fast 20MHz core, up to 64KB of Flash memory, up to 4KB of linear register SRAM, and an extensive array of onchip peripherals. These peripherals make Z8 Encore!™ suitable for a variety of applications including motor control, security systems, home appliances, personal electronic devices, and sensors.

Z8 Encore!™ Features • • • • • • • • • • • •
AN013702-0403

New high-performance 20MHz eZ8 CPU Up to 64KB Flash memory with in-circuit programming capability Up to 4KB register SRAM 12-channel, 10-bit analog-to-digital converter (ADC) Two full-duplex UARTs Two Infrared Data Association (IrDA) compliant endecs SPI and I2C ports Four 16-bit timers with capture, compare, and PWM capability Watch-Dog Timer (WDT) with internal RC oscillator 3-channel DMA Up to 60 I/O pins 24 interrupts with configurable priority

Abstract

Z8

Encore!™-Based

Application Note Battery Charger 2

• • •

On-chip debugger Voltage Brown-Out protection (VBO) Power-On Reset (POR)

The Z8 Encore!™ CPU is capable of a nominal 10MIPs throughput at 20MHz. The 4KB SRAM extends the Z8 Encore!™’s reach to a wider range of applications. The 10-bit sigma/delta ADC provides high measurement resolution, and the SPI, UART, and I2C interfaces can be used concurrently. The versatile DMA controllers can be configured in many useful combinations to free the CPU from performing unnecessary data transfer overhead.

Discussion
A discussion on what goes into designing a battery charger is presented in this section. Readers are requested to see Appendix A on page 13 for further details.

Theory of Operation
When designing a battery charger, the following aspects are considered:

• • •

Power control techniques to suit different battery types and capacities Charging and charge termination techniques to avoid overcharging, thus facilitating fast charging Safety techniques to ensure safe operation throughout the charging process

These aspects are discussed in the following pages. Power Control Techniques At the core of a battery charger is the DC–DC converter that acts as a regulated power source. The charger hardware is capable of regulating its output in various modes, such as constant voltage, constant current, or constant voltage with a current limit. The charger can be viewed as a control system in itself. In Figure 1, an initial setpoint is a charger output value of the user’s choosing. In a battery charger, the type and capacity of the battery is the determinant of the mode of operation—namely, a constant current source or a constant voltage source. It also determines the required current and voltage setpoints. These setpoints can be expressed as ISET or VSET.

AN013702-0403

Discussion

Z8

Encore!™-Based

Application Note Battery Charger 3

Setpoint (VSET /I SET )

+ –

Σ

Error

Controller

Control Signal (PWM)

Actuator (Buck Converter)

Output (VOUT /I OUT)

Feedback Signal (VFB /IFB)

Feedback Circuits

Figure 1. Feedback Control System

The feedback circuits shown in Figure 1 measure actual output. The difference between the initial setpoint and the actual value (feedback signal) is called an error. The controller generates a control signal according to the magnitude and direction of the error. It minimizes the steady state error and also responds quickly to transient fluctuations during input or output. Controllers usually work at lower power levels and therefore require an external actuator to generate the appropriate output. In a battery charger, the actuator is a step-down DC–DC converter, also known as a buck converter. The buck converter converts a higher DC voltage to a lower one depending on the pulse-width modulated (PWM) control signal generated by the controller. The frequency of the PWM signal is maintained at a constant while the width of the pulse or the duty cycle of the signal varies. This variation is reflected as a change in voltage and/or current at the output. Controllers are differentiated according to the way they handle errors generated during regulation of the system output (in the case of a charger, these errors are either voltage or current errors). In a proportional controller, the actual value and the set value are compared, and the resulting error value is used. In such a system, there exists the possibility of a steady state error, which is a drawback for the proportional controller. Adding an integral component to the proportional controller eliminates this steady state error. The equation for a proportional plus integral (PI) controller is:

υ ( t ) = ( k1 × e ( t ) + k2 × ∫ e ( t ) dt )
To be useful for a microcontroller-based (discrete) system, the integral is approximated by a running sum of the error signal. Thus, an equation can be expressed as follows:

AN013702-0403

Discussion

Z8

Encore!™-Based

Application Note Battery Charger 4

  k–1 υ [ k ] =  C1 × e [ k ] + C2 × ∑ e [ j ] j=0  
where C1 and C2 are constants.

Equation 1

Equation 1 is known as the position algorithm. A better representation for Equation 1 is shown in Equation 2, as follows:

  – υ [ k – 1 ] =  C1 × e [ k – 1 ] + C2 × k∑2 e [ j ] j=0  

Equation 2

Subtracting Equation 2 from Equation 1 and rearranging the terms yields Equation 3, as follows:

υ [ k ] – υ [ k – 1 ] = ( Kp × e [ k ] + Ki × e [ k – 1 ] )

Equation 3

where Kp and Ki are the proportional and integral constants, respectively. Equation 3 is known as the velocity algorithm. It is a convenient expression, as only the incremental change in the manipulated variable is calculated. For a detailed discussion on controllers, please see reference 2c in Appendix A on page 13. Charging and Charge Termination Techniques Different battery types require different charging methods. The basic charging methods are the constant current and constant voltage charging. The NiCad and NiMH batteries are charged using the constant current method, whereas the SLA and Li-Ion batteries are charged via the constant voltage method. An on/off current limiter is required when performing constant voltage charging. These charging methods are based on the type of battery and the present state of charge for that battery. In a constant current method of charging, fast charging occurs when the charging current equals the rated battery capacity, C. Fast charging requires constant monitoring of battery parameters and precise termination techniques. It is therefore important to know when to terminate charging. The behavior of different batteries near full charge varies and demands different termination techniques. The most common termination techniques are the nega-

AN013702-0403

Discussion

Z8

Encore!™-Based

Application Note Battery Charger 5

tive ∆V, zero ∆V, and the absolute battery voltage, all of which are based on battery types, as discussed in Appendix F on page 44. Safety Techniques A battery charger must ensure the safety of batteries. Battery safety is implemented by monitoring the battery terminal voltage and current against the battery ratings provided by the manufacturer. When battery ratings are exceeded, the charging voltage or current is switched off.

Z8 Encore!™-Based Battery Charger
This section offers an overview of the functional architecture of the battery charger implementation using Z8 Encore!™.

Hardware Architecture
The Z8 Encore!™-based charger features the following hardware blocks. These blocks are illustrated in Figure 2.

• • • • •

Z8 Encore!™ MCU Step-down DC–DC (buck) converter Feedback section Battery selector (jumper settings) LED status indicators

AN013702-0403

Z8 Encore!™-Based Battery Charger

Z8

Encore!™-Based

Application Note Battery Charger 6

External Power Source +
PWM Output

Step-Down (Buck) Converter Battery

Z8 Encore!™ MCU

ADC Channels

Converter V/I, Battery Voltage Feedback Batter Selector (Jumper Settings) Status Indicator (LED Port)

GPIO as Inputs

GPIO as Outputs

Figure 2. Block Diagram of Battery Charger Hardware

In this application, the Z8 Encore!™ MCU’s ports E and H are used as GPIO; port B is used as an ADC input. Timer1 is used in PWM mode and the output is tapped at the pin PC1/Timer1 out. The step-down DC–DC (buck) converter provides appropriate voltage or current for the set battery type and parameters. The buck converter modulates a higher voltage (from the external source) with a varying pulse width (Pulse Width Modulation method) to generate a lower voltage. The pulse width is controlled by the control algorithm based on the values obtained from the feedback section. The output of the external source is preferably set to twice the value of the converter output voltage (VOUT). The feedback section consists of three differential amplifiers/attenuators. The corresponding parameters are the converter voltage (VOUT), battery voltage (VBATT), and battery current (IBATT). The battery current and the converter current are the same. The battery type is selected by setting one of the four jumpers provided. The jumper status is read initially, and the corresponding routine is selected for charging. The charger indicates the charger status via LEDs, which are used to indicate various states such as successful completion of charging, safety error, no battery

AN013702-0403

Z8 Encore!™-Based Battery Charger

Z8

Encore!™-Based

Application Note Battery Charger 7

selection, and the specific battery type undergoing the charging process. Table 5 lists the status indicators along with a brief description.
Table 5. LED Status Indicators LED D4 D5 D6 D7 D8 D9 D10 Status ON ON ON ON ON ON ON Description SLA battery is selected and charging is ON. NiCad battery is selected and charging is ON. NiMH battery is selected and charging is ON. Li-Ion battery is selected and charging is ON. No battery is selected. Safety error—charging is aborted. Charging is successfully completed.

Refer to Appendix C on page 15 for the battery charger schematics.

Software Implementation
All Z8 Encore!™ peripherals are initialized to the required mode of operation. The jumper settings are read and the battery type is validated. When the battery type is fixed, the battery parameters are loaded into the variables. At present, these battery parameters are defined in the header file. Initially, based on battery ratings, each module sets the safety and termination thresholds. Then type-dependent settings, such as converter voltage, current outputs, and current limit are calculated. When these one-time calculations are completed, the charger software enters an infinite loop, which is broken only by a successful charge completion or a safety error. Inside the loop, the ADC reads the actual values for the converter output voltage, the battery voltage, and the current. The ADC measures the output voltage and output current of the DC–DC converter as a feedback to the controller. It measures the voltage at the battery terminals as an input to determine the charge termination. When the actual values are known, they are checked for safety limit compliance. The safety routine is responsible for the overall safety features associated with the battery charger. The charger ensures safety by comparing the actual converter voltage and battery voltage with the calculated thresholds. Crossing these thresholds switches off the PWM output, which turns off the converter output and terminates charging functions. Such termination protects the batteries in case of a device failure. The LED status indicator reflects an unsuccessful termination.

AN013702-0403

Z8 Encore!™-Based Battery Charger

Z8

Encore!™-Based

Application Note Battery Charger 8

If everything is within limits, the battery is tested for full charge. Full charge is tested using different methods for different batteries (see Appendix F on page 44). If the battery is fully charged, charging terminates and the LED indicators are updated. If the battery requires further charging, the controller calculates the required duty cycle for maintaining the setpoint at the converter output. The controller implements proportional plus integral (PI) control to derive the PWM output based on the equations mentioned in the Theory of Operation section on page 2. The timer ISR is invoked every five milliseconds. The PWM value computed by the controller is loaded into the PWM generators to be sent out via the output pin. The 16-bit timer PWM mode offers a programmable switching frequency based on the reload value. This flexibility allows designers to trade off between accuracy and frequency of the PWM switching signal. The higher the frequency, the lesser the reload value and the lower the resolution in the pulse width variation; and vice versa. The timer ISR also updates the charge termination variables every 10 seconds.

Testing
This section contains a detailed test procedure to demonstrate the working of the Z8 Encore!™ battery charger as described in this Application Note. The test setup to demonstrate the battery charger using Z8 Encore!™ is illustrated in Figure 3.

External DC Power Supply

Oscilloscope

Z8 Encore!™ Evaluation Board

PWM

DC–DC Step-Down Converter

Battery

Feedback Attenuators PC–HyperTerminal
Charger Hardware/External Circuits

Figure 3. Battery Charger Test Setup

The test setup consists of an oscilloscope and a PC running the HyperTerminal application. For testing, the Z8 Encore!™ Evaluation Board is used with the DC–

AN013702-0403

Testing

Z8

Encore!™-Based

Application Note Battery Charger 9

DC converter and the feedback circuits. An external DC source supplies necessary voltage and current for the various circuits involved. The external DC power supply provides two different voltages to the charger circuits—the DC–DC step-down converter and the feedback attenuators. The operational amplifier based feedback attenuator circuits are fed with a 12V supply. The DC–DC converter works on a 8–12V DC input for the batteries tested. The control algorithm provides the necessary line regulation to sustain the voltage variation at the input. During testing, HyperTerminal is set at 57600 baud, 8-bit data, no parity, 1 stop bit, and no flow control. The equipment used to test the Z8 Encore!™-based battery charger is detailed in Table 6.
Table 6. Z8 Encore!™ Battery Charger Test Equipment Z8 Encore!™ Evaluation Board (Z8ENCORE000ZCO) External power supply Oscilloscope (Tektronix TDS 724D; 500MHz/1GSps) Multimeter PC (The HyperTerminal utility is used via the COM2 port of the PC.) Batteries Used BP–T40 BP–T16 CP2010H T–014 Make Sony Sony Panasonic Type Sealed Lead Acid Nickel Cadmium Nickel Metal Hydride Ratings 4 Volts, 500mAh 3.6 Volts, 270mAh 3.6 Volts, 150mAh

The circuits are connected as per the schematics in Appendix C on page 15. When the external power supply and the Evaluation Board power supply are switched on, the PWM waveforms are observed on the oscilloscope. The battery/ converter’s actual values are indicated in the HyperTerminal window. The LED status indicators, as depicted in Figure 2, reflect the charging status during the charging operation. Figures 4, 5 and 6 illustrate the test results obtained while charging various types of batteries. For SLA batteries, initially the current is effectively limited to 200mA; it continually falls while battery voltage increases. The charging profiles also demonstrate the constant voltage output (Vout) of the DC–DC converter at 4900mV. See Figure 4.

AN013702-0403

Testing

Z8

Encore!™-Based

Application Note Battery Charger 10

4900 4700
V batt / V out in mVolts

200

V out V batt

180 160 140 120 100 80

4300 4100 3900 3700 3500
121 151 181 211 241 271 301 331

Iout

60 40 20 0
361

31

61

91

1

Time in minutes
Figure 4. SLA Charging Profile

The NiCad charging profile shown in Figure 5 indicates a marked hump towards the full charge, before dropping down. The software effectively detects this drop and the charging is terminated.
6500 310

Vbatt / Vout in mVolts

6000

V out

300 290 280

5500

Iout
5000

270 260 250 240

V batt

4500

4000

230

Time in Minutes
Figure 5. NiCad Charging Profile

AN013702-0403

101

21

11

31

41

51

71

61

81

91

1

A

Iout mAmps

4500

Testing

Z8

Encore!™-Based

Application Note Battery Charger 11

The NiMH charging profile shown in Figure 6, on the other hand, lacks a significant drop and is thus terminated using the zero ∆V termination scheme.
6000 5800 5600 5400 5200 5000 4800 4600 4400 4200 4000 200 190 180 170

Vout

Vbatt / Vout in mVolts

Vbatt Iout

160 150 140 130 120 110 100

65

49

57

25

17

Time in minutes
Figure 6. NiMH Charging Profile

The charging profiles for NiCad and NiMH batteries demonstrate constant current outputs of 270mA and 150mA respectively. These are equal to their rated battery capacity measured in mAh. The charging times for NiCad and NiMH are 1 hour, 45 minutes and 1 hour, 25 minutes, respectively. Note: Because the SLA and Li-Ion batteries follow similar charging (constant voltage with limited current) and termination profiles (absolute voltage), only the SLA battery was charged. The results are provided in this document.

Summary
This Application Note demonstrates the use of Z8 Encore!™ in a battery charger implementation. Ordinary battery chargers can charge batteries of a particular type and of a particular voltage. The Z8 Encore!™-based hardware/software provides flexibility such that batteries of different types can be charged with the same charger. The Z8 Encore!TM 10-bit ADC ensures accurate charge termination, facilitating faster recharge. Such termination avoids overcharging and prolongs battery life. The flexibility of the PWM mode allows for accurate DC–DC buck/step-down converter implementation with excellent line/load regulation.

AN013702-0403

33

41

73

81

1

9

Iout in mAmps

Summary

Z8

Encore!™-Based

Application Note Battery Charger 12

The test results clearly demonstrate the charging and termination mechanisms used by the charger to successfully charge different battery types.

AN013702-0403

Summary

Z8

Encore!™-Based

Application Note Battery Charger 13

Appendix A—Reference
Further information regarding battery charging mechanisms can be found in the references listed in Table 7.
Table 7. List of References Topic Z8 Encore!™ MCU Document Name a. Z8 Encore!™ Microcontrollers with Flash Memory and 10-bit A/D Converter Product Specification b. Z8 Encore!™ Flash Microcontroller Developer Kit User Manual Electronics www.zilog.com/docs/z8encore/um0131.pdf Source www.zilog.com/docs/z8encore/ps0176.pdf

c. Power Electronics Design Handbook: ISBN: 0-7506-7073-8 Low Power Components and Publisher: Oxford: Newnes, 1998 Applications; author: Nihal Kularatna d. High Frequency Switching Power ISBN: 0-07-010949-4 Supplies: Theory and Design; author: Publisher: McGraw-Hill Book Company George Chryssis e. Digital Control Systems, Volume 1— ISBN: 0-387-50266-1 Fundamentals, Deterministic Control; Publisher: Springer Verlag author: Rolf Isermann

Battery Yuasa Technical Manual Technology Duracell Eveready/Energizer Panasonic Li-Ion battery documents Sanyo

http://www.yuasabatteries.com/literature.asp http://www.duracell.com/batteries http://data.energizer.com http://www.panasonic.com/industrial/battery/oem/ chem/lithion/index.html http://www.sanyo.com/industrial/batteries/ index.html

AN013702-0403

Reference

Z8

Encore!™-Based

Application Note Battery Charger 14

Appendix B—Glossary
Definitions for terms and expansions for abbreviations used in this application note that are not commonly known are listed in Table 8.
Table 8. Glossary Term/Abbreviation ADC ISR Li-Ion mAh NiCad NiMH PI PWM SLA Definition/Expansion Analog to Digital Converter Interrupt Service Routine Lithium Ion milli-Ampere-hour: a unit of battery capacity Nickel Cadmium Nickel Metal Hydride Proportional plus Integral Pulse Width Modulation Sealed Lead Acid

AN013702-0403

Glossary

Application Note Z8 Encore!™-Based Battery Charger

Appendix C—Schematics
This section provides the schematics for the Z8 Encore!™ battery charger implementation.
5 4 3 2 1

15

Z8 Encore! Interface
GND VDD

F1

U14 LM7805C/TO220/0.5A

1
D5 RXE160

IN

OUT

3

5V

VCC

VDD GND

D

2

PA4 PA5

S2G
PW R JACK

GND

D

80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65

U1

PA1/T0OUT PA2 PA3/CTS0 GND VDD PF7 PC5/MISO PD3 PD4/RXD1 PD5/TXD1 PC4/MOSI VDD GND PA4/RXD0 PA5/TXD0 PA6/SCL

1 3 2
P3

C17
C21 0.1

+ C15 100/10

+

C22 47uF

0.1

RESET VDD
C

PE4 PE3
PE2 PE1 PE0

GND

GND

1

VDD

EXTAL XTAL
B

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

PA0/T0IN PD2 PC2/SS PF6 RESET VDD PF5 PF4 PF3 PE4 PE3 GND PE2 PE1 PE0 GND PF2 PF1 PF0 VDD PD1/T3OUT PD0/T3IN EXTAL XTAL

Z8F

PA7/SDA PD6/CTS1 PC3/SCK PD7/RCOUT PG0 GND PG1 PG2 PE5 PE6 PE7 VDD PG3 PG4 PG5 PG6 VDD PG7 PC7/T2OUT PC6/T2IN DBG PC1/T1OUT PC0/T1IN GND

64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41

U16
GND
VCC

3 1

VIN VOUT GND

2

3.3V

VDD

VDD
C

VDD

PE5 PE6 PE7

LT1086-3.3/TO220

R14 680

0.1
VDD

100/6.3

2
D6 GREEN

C24

+ C23

GND

PC1

U19

R17

VCC

3
2
100K
RESET

B

GND AVDD PH0/ALG8 PH1/ALG9 PB0/ALG0 PB1/ALG1 PB4/ALG4 PB5/ALG5 PB6/ALG6 PB7/ALG7 PB3/ALG3 PB2/ALG2 PH2/ALG10 PH3/ALG11 VREF AGND

1

EXTAL

XTAL

GND RESET

Y1 18.432MHz
R3 1M

DS1233A-15
VDD

GND 25 VDD 26 27 PH0_ALG8 28 PH1_ALG9 29 PB0_ALG0 30 PB1_ALG1 31 32 33 34 35 PB3_ALG3 36 PB2_ALG2 37 PH2_ALG10 38 PH3_ALG11 39 40

C45

C46 0.1uF

C49 0.1uF

C50 0.1uF GND

C30
SW4

0.1uF

RESET
PH3 PH2 PB2 PB3 PB1 PB0 PH1 PH0

0.01

C1
A

C2 18pF

A

18pF

Title

Battery Charger using Z8 Encore!
Size A Date: Document Number <Doc> Rev Sheet

Tuesday, January 07, 2003
2

1
1

of

1

5

4

3

Figure 1. Schematic for Z8 Encore!™ Interface
AN013702-0403 Schematics

Application Note Z8 Encore!™-Based Battery Charger 16

5

4

3

2

1

Vp

DC-DC Step Down Converter
R3
D

1K

D

R37 18E
R1

V_out(+)
IRF9540 Q1

V_out(-)
D2

L1
Q2 2N2222

PC1/ T1OUT
3.3K

120uH
R2 2.2K
D3 MBR360

MBR360
C1 100uF
C2 100uF

R4 470E
R5 79E
BT1

(+)

V_batt(+)
BATTERY TO BE CHARGED

D1 LED

V_batt(-)
(-)
R6A 10E
C

C

R6B
C3

10E
0.1uF

Rsense

I_out(+)

3.3 Volts

LED Indicator Port
I_out(-)

B

R7 560E

R8 560E

R9 560E

R10 560E

R11 560E

R12 560E

R13 560E

B

D4 LED

D5 LED

D6 LED

D7 LED

D8 LED

D9 LED

D10 LED

PE1 PE2 PE3 PE4 PE5 PE6 PE7

A

A

Title
Using Z8 Encore! as a Battery Charger

Size A Date:
5
4
3

Document Number <Doc>
Tuesday, January 07, 2003
2

Rev 0.0 Sheet
1
1

of

2

Figure 2. DC–DC Step-Down Converter and LED Indicator Port
AN013702-0403 Schematics

Application Note Z8 Encore!™-Based Battery Charger 17

5

4

3

2

1

0.1uF C7
R25 1K

0.1uF C7
R17 1K

Feedback Circuits

12V
D

12V
LM324 U1C
10K

1K

11

I_out(-)
I_out(+)

8

R34

V_batt(-)

Battery Current

V_batt(+)

4

R24 1K

C8 10uF

R16 1K

4

1K

10K

C

C7

VCC

R21

10K

V_out(-)

V_out(+)

R20 1K
B

4

10K

3.3 Volts

Jumpers for Battery Selection
R30 10K

R31 10K

R32 10K

R33 10K

Note: 1. R14 - R30 all 1% MFR. 2. Signal, Digital, and Power Grounds are connected on the evaluation board.
A

A

PH0

1 2
J2

PH1

1 2
J3

PH2

1 2
J4

PH3

1 2
J5

Title
Using Z8 Encore! as a Battery Charger

Select NiCd
5

Select NiMH
4

Select SLA
3

Select Li-Ion

Size A Date:

Document Number <Doc>
Tuesday, January 07, 2003
2

Figure 3. Feedback Section and Battery Type Selector Jumper Settings
AN013702-0403 Schematics

+

R19

5

-

C11 100uF

C9 0.1uF

C10 0.1uF

R18

6

11

+

+

R23

10

PB2/ANA2

R15

3

-

-

R22

9

10K

R14

2

11

LM324 U1A

D

1

PB1/ANA1
Battery Voltage

0.1uF
C

1K

12V
LM324 U1B

7

PB3/ANA3
Converter Output Voltage

B

Rev 0.0 Sheet
2
1

of

2

Z8

Encore!™-Based

Application Note Battery Charger 18

Appendix D—Flowcharts
The main battery-charging routine is illustrated in Figure 4.

Start

Initialize peripherals

Read and verify battery type

Get the battery parameters

Calculate safety limits and thresholds for charging and termination

Read feedback values for battery voltage, current, and converter voltage

Within safety limits?
Yes

No

Is the battery charged?
No

Yes

Terminate

Calculate the duty cycle

Figure 4. Flowchart for the Main Routine

AN013702-0403

Flowcharts

Z8

Encore!™-Based

Application Note Battery Charger 19

The ISR return routine is illustrated in Figure 5.

Start ISR

Reload PWM Value

Update charge ending data every 10 seconds

Return from ISR Figure 5. Flowchart for the ISR Return Routine

AN013702-0403

Flowcharts

Z8

Encore!™-Based

Application Note Battery Charger 20

Appendix E—Source Code
/ ***************************************************************************** *** ** File: main.c ** Description: ** Main routine for battery charger using Z8 Encore! ** This routine reads the type of battery and switches the program flow ** accordingly. ** ** Copyright 2002 ZiLOG Inc. ALL RIGHTS RESERVED. ** ** This file contains unpublished confidential and proprietary information of ** ZiLOG, Inc. ** NO PART OF THIS WORK MAY BE DUPLICATED, STORED, PUBLISHED OR DISCLOSED IN ** ANY FORM WITHOUT THE PRIOR WRITTEN CONSENT OF ZiLOG, INC. ** ** This is not a license and no use of any kind of this work is authorized in ** the absence of a written license granted by ZiLOG, Inc. in ZiLOG's sole ** discretion ** ***************************************************************************** **/ ** Project: ** Z8 Encore! as a Battery charger ** ** Module: ** Main System ** ** Purpose: ** This file contains the main routine for the battery charger application ** note. ** ** Dependencies: ** Device number or family : Z8 Encore! ** Header files : charger.h, ez8.h, initialize.h ** ** History: ** Date Action ** -----------------------------------------------------** 2002-12-19 Created ** ** References: ** Document Number Title ** ----------------------------------------------------------------**

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 21

****************************************************************************** */ #include <stdio.h> #include <ez8.h> #include "charger.h" #include "initialize.h" void interrupt isr_timer0(void); int display_timer = 0, pwm_update_timer = 0, eoc_update_timer = 0; int new_pwh = 0, new_pwl = 0; int eoc_value = 0, last_eoc_value = 0; int battery_type = 0; extern int average_value[CHANNELS+1]; void main() { int print_data1, print_data2, print_data3; int n = 0; int i; initialize(); battery_type = read_battery_type(); if (battery_type == INVALID) { n = 1; PEOUT = NO_SELECTION; } else { select_parameters(battery_type); // Initialize the system

// Flag for aborting the program. // Indicate error on LED port.

// Assign appropriate battery

parameters. do_common_calculations(); do_specific_calculations(battery_type):

// // // //

Battery type calculations Battery type calculations

independent here. specific here.

EI(); } while (n != 1) { read_feedback_values(); check_for_safety();

// Find actual values. // Ensure safe operation.

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 22

n = check_for_termination(battery_type); calculate_pwm_value(battery_type);

if (display_timer == 12000) { display_timer = 0; print_data1 = average_value[1] ; print_data2 = average_value[2] / R_SENSE; print_data3 = average_value[3] ;

// // // // // // //

Find whether further charging is required. Find appropriate pwm value based on feedback received. Output data to UART0 every 1 minute

printf("%5d %5d %5d\n", print_data1, print_data2, print_data3); } } } /* ****************************************************************************** ** ** Routine : isr_timer0 ** Parameters : None ** Return : int ** ** Purpose: ** This is the ISR for the battery charger. ** It updates the duty cycle every 5 milliseconds. ** It updates the fullcharge test values every 10 seconds. ** ****************************************************************************** */ #pragma interrupt void isr_timer0() { eoc_update_timer = eoc_update_timer + 1; // condition for updating // termination values display_timer = display_timer + 1; // condition for sending // data to UART T1CTL &= 0x7F; // Disable timer for // modifications // PWM High // PWM Low // Enable timer, rest same.

T1PWMH T1PWML T1CTL

= new_pwh; = new_pwl; |= 0x80;

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 23

if (eoc_update_timer == 2000) { last_eoc_value = eoc_value; eoc_value = average_value[1]; eoc_update_timer = 0; } }

// Store end_of_charging // values every 10 seconds // update last value // update present value // Reset every 10 seconds

/* ****************************************************************************** ** End of File ****************************************************************************** */

/ ***************************************************************************** *** ** File: initialize.c ** Description: ** This routine intializes all the GPIOs, peripherals, and sets interrupt ** service routines. ** ** Copyright 2002 ZiLOG Inc. ALL RIGHTS RESERVED. ** ** This file contains unpublished confidential and proprietary information of ** ZiLOG, Inc. ** NO PART OF THIS WORK MAY BE DUPLICATED, STORED, PUBLISHED OR DISCLOSED IN ** ANY FORM WITHOUT THE PRIOR WRITTEN CONSENT OF ZiLOG, INC. ** ** This is not a license and no use of any kind of this work is authorized in ** the absence of a written license granted by ZiLOG, Inc. in ZiLOG's sole ** discretion ** ***************************************************************************** **/ ** Project: ** Z8 Encore! as Battery charger ** ** Module: ** Initialization of the System ** ** Purpose: ** This file contains code for the initialization of the ** Z8 Encore! as used in the battery charger application. **

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 24

** Dependencies: ** Device number or family : Z8 Encore! ** Header files : charger.h, ez8.h, initialize.h ** ** History: ** Date Action ** --------------------------------------------------------** 2002-12-20 Created ** ** References: ** Document Number Title ** ---------------------------------------------------------------** ***************************************************************************** *** */ #include <stdio.h> #include <ez8.h> #include "charger.h" #include "initialize.h" extern extern extern extern extern extern void interrupt isr_timer0(void); int average_value[CHANNELS + 1]; int adc_value[CHANNELS + 1]; int old[CHANNELS + 1]; int max; int Vset, Iset, Ilim;

int print_data1, print_data2, print_data3; void initialize(void) { unsigned int RAM_Location; int i; // // // // // // // // // // // // // Initialize GPIO ports ---------------------Port A alternate function - Enabled UART0 Rx,Tx and Timer0. Port A data direction - UART0_Rx is input. Rest of Port A defined as Output. Timer0 output is PUSH-PULL. Same as RESET. Port B alternate function - defined as all ADC inputs. Port B data direction is all

PAADDR = ALT_FUN; PACTL = 0x32; PAADDR = DATA_DIR; PACTL = 0x10;

PBADDR = ALT_FUN; PBCTL = 0xFF;

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 25

PCADDR PCCTL PCADDR PCCTL

= = = =

ALT_FUN; 0x02; DATA_DIR; 0xFD;

PEADDR PECTL PEADDR PECTL

= = = =

DATA_DIR; 0x00; OUT_CTL; 0xFF;

PHADDR = DATA_DIR; PHCTL = 0xFF; PEOUT |= ALL_LEDS_OFF;

SET_VECTOR(TIMER0, isr_timer0); T0H= T0L T0RH T0RL 0x00; = 0x00; = 0x5A; = 0x00;

T0CTL = 0x11; mode

// // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //

inputs. Same as RESET. Port B output control. Not applicable. Port C alternate function - Enabled Timer1(PWM output) Port C data direction - Timer1_out is output. Rest all inputs. Port E are all GPIOs. Same as RESET. Port E data direction - defined as all outputs. Port E output control - defined as all open drain. Port H are all GPIOs. Same as RESET. Port H data direction - defined as all inputs. -----------------------------Initialize the LED indicator port -----------------------------Initialize timer0 Set interrupt service routine ISR for timer0 Timer High Timer Low Reload Compare High Reload Compare Low = 5 mS Timeout Timer Control#00010001b |_|_______ continuous

// of operation // |_|_____________/4 Prescale // // // // // // // // // // // // Set Timer0 as Level2 (Nominal priority interrupt IRQ0 Enable High for Timer0 IRQ0 Enable Low Enable timer0. -----------------------------Initialize Timer 1 in continuous PWM mode with prescale = 1 PWM mode, Prescale 1, not enabled. PWM High

IRQ0ENH |= 0x20; IRQ0ENL &= 0x00; T0CTL |= 0x80;

T1CTL T1PWMH

= 0x03; = 0x00;

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 26

T1PWML = 0x00; T1RH = 0x01; T1RL = 0x70;

RAM_Location = 0xde;

DMAA_ADDR = RAM_Location;

U0BRH = 0x00; U0BRL = 0x14; U0CTL0 = 0xc0;

// // // // // // // // // // // // // // // // // // // // //

PWM Low RELOAD High RELOAD Low 0x0170 = 50KHz at 18.432MHz sysclk Define destination address for ADC-DMA Define data base address for ADC outputs as 0xDE0 arbitrary. RAM_Location cannot be between 0xF00 - 0xFFF -----------------------------Initialize UART for data collection, test only. Set Baudrate; test only. Set Baudrate = 57600; test only. Transmit enable, Receive Enable, No Parity, 1 Stop. -----------------------------// // // // // // // // // // // // // // // // // // // // // Non-recursive top rows. Test only. Non-recursive top rows. Test only. Non-recursive top rows. Test only. Non-recursive top rows. Test only. Non-recursive top rows. Test only. Non-recursive top rows. Test only. Non-recursive top rows. Test only.

printf("-------------------------------------- \n");

printf(" Z8 Encore! as a Battery Charger \n");

printf("-------------------------------------- \n");

printf("Vbatt \t");

printf("Iout \t");

printf("Vout \n");

printf("-------------------------------------- \n");

} /* ******************************************************************************

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 27

** End of File ****************************************************************************** */

/ ***************************************************************************** *** ** File: charger.c ** Description: ** This file contains code for battery charging. ** ** Copyright 2002 ZiLOG Inc. ALL RIGHTS RESERVED. ** ** This file contains unpublished confidential and proprietary information of ** ZiLOG, Inc. ** NO PART OF THIS WORK MAY BE DUPLICATED, STORED, PUBLISHED OR DISCLOSED IN ** ANY FORM WITHOUT THE PRIOR WRITTEN CONSENT OF ZiLOG, INC. ** ** This is not a license and no use of any kind of this work is authorized in ** the absence of a written license granted by ZiLOG, Inc. in ZiLOG's sole ** discretion ** ***************************************************************************** **/ ** ** Project: ** Z8 Encore! as a Battery charger ** ** Module: ** Charger routines ** ** Purpose: ** This file contains all the routines required for the functioning of the ** battery charger. ** ** Dependencies: ** Device number or family : Z8 Encore! ** Header files : charger.h, ez8.h ** ** History: ** Date Action ** -------------------------------------------------------------** 2002-12-19 Created ** ** References: ** Document Number Title ** ----------------------------------------------------------------

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 28

** ****************************************************************************** */ #include <ez8.h> #include "charger.h" int number_of_cells; int Vbatt, Ibatt; int gain[4] = {1, 5, 1, 10};

// // // //

Tbatt-gain0 = 1; Vbatt-gain1 = 5; Iout-gain2 =1; Vout-gain3 = 10

int Vbatt_max, Vout_max, Vbatt_low_th, flag_test, neg_th, zero_th, abs_th; int type, Vset, Iset, Ilim; int int int int average_value[CHANNELS + 1] = 0; adc_value[CHANNELS + 1] = 0; old[CHANNELS + 1] = 0; status;

extern int eoc_value, last_eoc_value; int error[2] = {0, 0}; extern int new_pwl, new_pwh; extern int battery_type; int Vpwm = 0; /* ****************************************************************************** ** ** Routine : read_battery_type ** Parameters : None ** Return : int ** ** Purpose: ** This function reads and validates the battery type. ** ****************************************************************************** */ int read_battery_type() { int type = 0; int selection; type = PHIN & 0x0F; // Mask higher nibble as port H

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 29

// is a 4-bit port. selection = INVALID; if (type == 0x0E) { selection = NiCad; PEOUT = NiCad_BATTERY; } if (type == 0x0D) { selection = NIMH; PEOUT = NIMH_BATTERY; } if (type == 0x0B) { selection = SLA; PEOUT = SLA_BATTERY; } if (type == 0x07) { selection = LI_ION; PEOUT = LI_ION_BATTERY; } return(selection); } // No proper jumper settings

// NiCad battery selected // LED indication

// NiMH battery selected // LED indication

// SLA battery selected // LED indication

// Li-Ion battery selected // LED indication

/* ****************************************************************************** ** ** Routine : select_parameters ** Parameters : None ** Return : Nothing ** ** Purpose: ** This routine selects battery voltage and capacity parameters for the ** charger. In a commercial product these can be entered from a keyboard. The ** values are defined in charger.h and the user will have to change them if ** the battery selected is of different voltage and capacity. ** ****************************************************************************** */ void select_parameters(int type) { if (type == NiCad) { Vbatt = VBATT_NiCad; // Voltage milivolts Ibatt = IBATT_NiCad; // Capacity miliamperehours

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 30

number_of_cells = Vbatt / 1200; } else if (type == NIMH) { Vbatt = VBATT_NIMH; Ibatt = IBATT_NIMH; number_of_cells = Vbatt / 1200; } else if (type == SLA) { Vbatt = VBATT_SLA; Ibatt = IBATT_SLA; number_of_cells = Vbatt / 2000; Vset = number_of_cells * 2450; } else { Vbatt = VBATT_LI_ION; Ibatt = IBATT_LI_ION; number_of_cells = Vbatt / 3600; Vset = number_of_cells * 4200; }

// One NiCad cell is 1200mV.

// Voltage milivolts // Capacity miliamperehours // One NiMH cell is 1200mV.

// // // // //

Voltage milivolts Capacity miliamperehours One SLA cell is 2000mV. SLAs are charged at 2450mV per cell.

// // // // // // // // // // // // // // // // // // // //

Voltage milivolts Capacity miliamperehours One Li-Ion cell is 3600mV. Li-Ion are charged at 4200mV per cell. ----------------------------Digitize the set points First, the set points are reduced to fit the ADC Vin range of 0 - 2000 millivolts by dividing them by the respective gains in the feedback channel. Next, they are scaled from these vlaues to the ADC word range of 0x000 - 0x3FF (10 bit). Converted to 0-2000 range Converted to 0-2000 range Converted to 0-2000 range

Vbatt = Vbatt / gain[1]; Ibatt = Ibatt / gain[2]; Vset = Vset / gain[3]; Vbatt = ((long)Vbatt << 10) / 2000; Ibatt = ((long)Ibatt << 10) / 2000; Vset = ((long)Vset << 10) / 2000; }

// scaled to 10 bit ADC // scaled to 10 bit ADC // scaled to 10 bit ADC

/*

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 31

****************************************************************************** ** ** Routine : do_common_calculations ** Parameters : None ** Return : Nothing ** ** Purpose: ** This routine combines all the calculations that are independent of battery ** type. ** ****************************************************************************** */ void do_common_calculations() { // Define safety thresholds. // --------------------------Vbatt_max = 160 * (Vbatt / 100); // The max out of 4 types is 60% // over the rated voltage. Vout_max = 160 * (Vset / 100); // Defined for dc-dc converter // output. // --------------------------// Define charging thresholds Vbatt_low_th = Vbatt >> 2; // Initial constant current // charge up to 1/4 th Vbatt // in SLA and Li-Ion. flag_test = (Vbatt * 4) / 5; // Used to avoid false, early // termination in NiCad and NiMH. // --------------------------// Define termination thresholds neg_th = number_of_cells * 15; // Number of cells multipled by // 15mV drop per cell. zero_th = (Vbatt / 100) * 2; // 2% of Battery rating for NiMH // termination. abs_th = (Vbatt / 100) * 5; // 5% of Battery rating for SLA // and Li-Ion termination. // --------------------------// For a battery with built-in // thermistor, // the temperature safety // thresholds can be // defined here as // temperature_min = 0; // temperature_max = 65535; }

/* ******************************************************************************

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 32

** ** Routine : do_specific_calculations ** Parameters : None ** Return : Nothing ** ** Purpose: ** Battery type specific parameters are calculated here. ** ****************************************************************************** */ void do_specific_calculations(int type) { if ((type == NiCad) ||(type == NIMH)) { Iset = Ibatt; // Fast charge at the rate of // full capacity Iset = Iset * R_SENSE; // Incorporates Rsense of 5-Ohms } else if (type == SLA) { Ilim = Ibatt / 2; // Desired current limit. Say, // half of Ibatt. Ilim = Ilim * R_SENSE; // Incorporates Rsense of 5-Ohms Iset = Ibatt / 10; Iset = Iset * R_SENSE; } else { Ilim = Ibatt / 2; Ilim = Ilim * R_SENSE; Iset = Ibatt / 10; Iset = Iset * R_SENSE; } } // // // // Desired start current for SLA or Li-Ion Converted to corresponding voltage

// Desired current limit. Say, // Half the Ibatt. // Incorporates Rsense of 5-Ohms // // // // Desired start current for SLA or Li-Ion Converted to corresponding voltage

/* ****************************************************************************** ** ** Routine : read_feedback_values ** Parameters : None

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 33

** Return : Nothing ** ** Purpose: ** In the read-feedback_values routine: ** Four Analog channels are converted to Digital data. ** The ADC data is stored arbitrarily from RAM location 0xDE0. ** The RAM location must exclude 0xF00-0xFFF address space. ** The ADC data is passed through a digital low pass filter. ** The final data is stored in the array average_value. ** ****************************************************************************** */ void read_feedback_values() { int i; DMAACTL |= (CHANNELS + 1); DMAACTL |= DMA_ADC_EN; do { status = DMAASTAT & 0xF0; status = status >> 4; // Wait till valid data for all // channels. DMAACTL &= ~DMA_ADC_EN; // Disable conversion //-----------------------------// Data conversion/Xfer over for // all channels. // Now load the data into the // arrays. // Low pass filter implementation // - Xfer function: out[k] = 0.6 // in[k]+ 0.4 in[k-1]

// Enable conversion

} while (status < (CHANNELS + 1));

for (i=0; i <= CHANNELS; i++) { adc_value[i] = getvalue(i); average_value[i] = ((6 * adc_value[i])+(4 * old[i]))/10; // Low pass filter old[i] = adc_value[i]; } } // Preparing for low // pass filter

/*

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 34

****************************************************************************** ** ** Routine : getvalue ** Parameters : None ** Returns : int ** ** Purpose: ** This routine is supplementry to the read_feedback_values routine above. ** ****************************************************************************** */ int getvalue (int temp) { int ADCvalue, valueH, valueL; unsigned int RAM_Location; RAM_Location = 0xde; DAADDR = RAM_Location; // // // // Select base address in RAM space. for writing data on completion of conversion.

RAM_Location = (RAM_Location << 4) & 0xFE0;// Convert to required format. valueH = IO_ADDR(RAM_Location + 2*temp);// Read MSbyte from (base_address // + channel_offset) valueH &= 0x00FF; // valueH is an int valueH = valueH << 3; // Make space for the two least // significant bits. valueL = IO_ADDR(RAM_Location + 2*temp + 1); // Read LSbyte from // (base_address // + channel_offset + 1) valueL = valueL >> 5; // Position least significant // bits at D0-D1 positions. valueL &= 0x03; // Mask all undefined bits. ADCvalue = valueH | valueL; // Form the 10-bit ADC value. return (ADCvalue); }

/* ****************************************************************************** ** ** Routine : check_for_safety ** Parameters : None ** Return : Nothing ** ** Purpose:

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 35

** This routine checks whether the battery voltage and converter ** output voltage are within the safety limits. ** ****************************************************************************** */ void check_for_safety() { if (average_value[1] > Vbatt_max) // Compare actual batery voltage // with max limit for Vbatt. { T1CTL &= 0x7F; // Actual battery voltage higher // than threshold - disable PWM // output. PEOUT &= SAFETY_ERROR; // Indicate safety threshold // violation on LED Port. DI(); // Disable further action. } if (average_value[3] > Vout_max) // Compare converter output // voltage with max limit of // Vout. // // // // // Actual output voltage higher than threshold - disable PWM output. Indicate safety threshold violation on LED Port. Disable further action

{ T1CTL &= 0x7F; PEOUT &= SAFETY_ERROR; DI(); } // Code for termal safety can be // added here. }

/* ****************************************************************************** ** ** Routine : check_for_termination ** Parameters : None ** Return : int ** ** Purpose: ** This routine contains code for terminating different ** types of batteries. The algorithms available are ** Negative delta V algorithm for NiCad batteries, ** Zero delta V algorithm for NiMH batteries, and ** Absolute V algorithm for SLA and Li-Ion batteries. ** ******************************************************************************

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 36

*/ int check_for_termination(int type) { static unsigned char count = 0; static int max = 0; int diff = 0; int difference; int status = 0;

// // // // // // //

for NiMH zero delta V termination for NiCad neg delta V threshold for NiCad neg delta V threshold for SLA and Li-Ion termination Initialize default for uncharged batteries

if (type == NiCad) { if (average_value[1] > flag_test) { if (eoc_value > max) { max = eoc_value; diff = 0; } else { diff = max - eoc_value; } if (diff > neg_th ) { T1CTL &= 0x7F; PEOUT &= CHARGING_OVER; DI(); status = CHARGING_COMPLETE; } } } else if (type == NIMH) { if (average_value[1] > flag_test)

// // // // // // //

Is present battery voltage the highest value yet? (This is done for peak identification in NiCad) If yes, save it as the highest. Reset difference to zero.

// If no, find the difference // from the highest value. // // // // // // // // // Now check if the difference from max is more than the threshold set for NiCad: neg_th = 15mV per cell. Terminate - disable PWM op Show successful charging on LED Port. Disable further action. Termination flag set, used in main().

// // { // // if ((eoc_value - last_eoc_value) < zero_th) // //

Check if Battery voltage is near full charge. This avoids false early termination. zero_th is 2% of battery rating.

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 37

{ count = count + 1; if (count > 2) { T1CTL &= 0x7F; PEOUT &= CHARGING_OVER; // Mark the occurances. // Avoiding spurious // equalities. // // // // // // // terminate - disable PWM output. Show successful charging on LED port. Disbale further action. Termination flag set, used in main()

DI(); status = CHARGING_COMPLETE;

} else { count = 0; } } } else { difference = Vset - average_value[1]; if (difference < abs_th) { T1CTL &= 0x7F; PEOUT &= CHARGING_OVER; // // // // // // // Terminate - disable PWM output. Show successful charging on LED Port. Disbale further action. Termination flag set, used in main() // SLA or NiMH battery types // // // // average_value[1] is used instead of eoc_value as faster sampling is better here. // If not three consecutive // times, start again.

DI(); status = CHARGING_COMPLETE; } } return (status); }

/* ****************************************************************************** ** ** Routine : calculate_pwm_value ** Parameters : None ** Return : Nothing **

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 38

** Purpose: ** The calculate_pwm_value routine monitors the battery status ** and decides the charging type. It further calculates the ** pwm values required for next cycle in control loop. ** ****************************************************************************** */ void calculate_pwm_value(int type) { if (type < 2) // NiCad or NiMH selected { cc_control(); // Constant current control } else // SLA or Li-Ion selected { if (average_value[1] < Vbatt_low_th) { cc_control(); // Start-up with constant current // control } else // Normal operation for SLA // or Li-Ion { cv_control(); // constant voltage control } } }

/* ****************************************************************************** ** ** Routine : cc_control ** Parameters: None ** Return : Nothing ** ** Purpose: ** This function implements the constant current controller for the battery ** charger. The constant current controller forces the dc-dc converter to work ** as a constant current source. It uses voltage developed across Rsense by ** the current as the feedback quantity. ** ****************************************************************************** */ void cc_control(void) { int new_pw;

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 39

error[0] = Iset - average_value[2]; // Find present error. Vpwm = Vpwm - (error[0] / 5) + (error[1] / 4); // PI controller algorithm. error[1] = error[0]; // store present error as // last error. // -----------------------// Scale Vout to Pulse width. // Reload corresponds to Vin // 100% duty cycle. new_pw = Vpwm * RELOAD / VIN; // So, calculate new_pw to // correspond to Vout. // -----------------------------new_pwl = new_pw & 0x00FF; // convert the new_pw in PWM // register format - L new_pwh = new_pw >> 8; // convert the new_pw in PWM // register format - H }

/* ****************************************************************************** ** ** Routine : cv_control ** Parameters: None ** Return : Nothing ** ** Purpose: ** This function implements the constant current controller for the battery ** charger.The constant voltage controller forces the dc-dc converter to work ** as a constant voltage source. It uses the voltage developed by the ** converter as the feedback quantity. ** ****************************************************************************** */ void cv_control(void) { int new_pw; error[0] = Vset - average_value[3]; // Find present error. Vpwm = Vpwm - (error[0]/ 5) + (error[1]/4);// PI controller algorithm. error[1] = error[0]; // store present error as last // error. // -----------------------------// Scale Vout to Pulse width. // Reload corresponds to // Vin - 100% duty cycle. new_pw = Vpwm * RELOAD / VIN; // So, calculate new_pw to

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 40

new_pwl = new_pw & 0x00FF; new_pwh = new_pw >> 8;

if (average_value[2] > Ilim) { DI(); T1CTL &= 0x7F; } else { T1CTL |= 0x80; EI(); } }

// // // // // // // // // // // // // //

correspond to Vout. -----------------------------convert the PWM value in PWM register format - L convert the PWM value in PWM register format - H -----------------------------Current Limit: ON-OFF control Compare actual current with the set limit. Prevent ISR from enabling the PWM output. Current Limit exceeded Switch OFF the PWM pules.

// Safe current - Switch ON the // PWM

/* ****************************************************************************** ** End of File ****************************************************************************** */

Header files / ***************************************************************************** *** * File : initialize.h * * Description: * This header file defines all the function prototypes used in the * initialize.c program * * Copyright 2002 ZiLOG Inc. ALL RIGHTS RESERVED. * * This file contains unpublished confidential and proprietary information of * ZiLOG, Inc. *

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 41

* NO PART OF THIS WORK MAY BE DUPLICATED, STORED, PUBLISHED OR DISCLOSED IN ANY * FORM WITHOUT THE PRIOR WRITTEN CONSENT OF ZiLOG, INC. * * This is not a license and no use of any kind of this work is authorized in * the absence of a written license granted by ZiLOG, Inc. in ZiLOG's sole * discretion * ***************************************************************************** *** */ void initialize(void);

/* ****************************************************************************** ** End of File ****************************************************************************** */

/ ***************************************************************************** **** File : charger.h * * Description: * It defines all the function prototypes and constants used in the * software listing. * * Copyright 2002 ZiLOG Inc. ALL RIGHTS RESERVED. * * This file contains unpublished confidential and proprietary information of * ZiLOG, Inc. * * NO PART OF THIS WORK MAY BE DUPLICATED, STORED, PUBLISHED OR DISCLOSED IN ANY * FORM WITHOUT THE PRIOR WRITTEN CONSENT OF ZiLOG, INC. * * This is not a license and no use of any kind of this work is authorized in * the absence of a written license granted by ZiLOG, Inc. in ZiLOG's sole * discretion * ***************************************************************************** *** /******************************** * Function prototypes for main.c

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 42

*********************************/ void select_parameters(int); void do_common_calculations(void); void do_specific_calculations(int); int read_battery_type(void); int check_for_termination(int); void calculate_pwm_value(int); /*************************** * Miscellaneous Definitions ****************************/ #define CHANNELS 3 #define VIN 350 #define R_SENSE 5 #define DATA_DIR 0x01 #define ALT_FUN 0x02 #define #define #define #define #define OUT_CTL 0x03 RELOAD 0x0170 CC 0 CV 1 CHARGING_COMPLETE 0x01

// // // // // // // // // // //

Four ADC channels 0-3 Conversion factor for PWM Current sensing resistor. Assign PxCTL for Data direction Assign PxCTL for Alternate function Assign PxCTL for Output control PWM reload value for 50KHz constant current control constant voltage control charge termination flag

/************ * LED codes *************/ #define ALL_LEDS_OFF #define CHARGING_OVER #define SAFETY_ERROR #define NO_SELECTION #define LI_ION_BATTERY #define NIMH_BATTERY #define NiCad_BATTERY #define SLA_BATTERY

0xFF 0x7F 0xBF 0xDF 0xEF 0xF7 0xFB 0xFD

/************************** * Battery type definitions ***************************/ #define NiCad 0x00 #define NIMH 0x01 #define SLA 0x02 #define LI_ION 0x03 #define INVALID 0x04 /********************************** * Battery type-specific parameters ***********************************/

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 43

#define VBATT_NiCad 3600 // Voltage rating for the NiCad battery in mV #define IBATT_NiCad 270 // Capacity rating for the NiCad battery in mAh #define VBATT_NIMH 3600 // Voltage rating for the NiMH battery in mV #define IBATT_NIMH 150 // Capacity rating for the NiMH battery in mAh #define VBATT_SLA 4000 #define IBATT_SLA 500 // Voltage rating for the SLA battery in mV // Capacity rating for the SLA battery in mAh

#define VBATT_LI_ION 3600 // Voltage rating for the Li-Ion battery in mV #define IBATT_LI_ION 600 // Capacity rating for the Li-Ion battery in mAh /******************************** * ADC Routines and definitions *********************************/ #define DMA_ADC_EN0x80 typedef unsigned char volatile far *IORegInt8; #define IO_ADDR(addr) *((IORegInt8) addr) void read_feedback_values(void); int getvalue(int); /********************* * Controller routines **********************/ void cv_control(void); void cc_control(void); /***************** * Safety Routines ******************/ void check_for_safety(void); /* ****************************************************************************** ** End of File ****************************************************************************** */

// constant voltage control // constant current control

AN013702-0403

Source Code

Z8

Encore!™-Based

Application Note Battery Charger 44

Appendix F—Battery Technology
The four mainstream battery chemistries discussed in this Application Note feature different charging and discharging characteristics. Long-term battery life and performance are critically dependent upon how batteries are charged. Therefore, it is of utmost importance to charge batteries with a mechanism specific to their requirement. It is also important to know when to terminate charging, because overcharging of a battery invariably results in poor performance and can damage the battery in extreme cases. Again, different battery types behave differently near full charge condition and thus require specific charge termination techniques. During charging, all batteries exhibit a marked rise in voltage above the rated battery voltage. The four major rechargeable battery types—SLA, NiCad, NiMH, and Li-Ion, are briefly discussed below. See Appendix A on page 13 for further details. Sealed Lead Acid (SLA) Sealed Lead Acid batteries are most commonly seen in automobiles. The single cell voltage for SLA is 2V. According to their use, several such cells are connected in series to get higher voltages such as 12V/24V. SLA batteries are usually charged with a constant voltage supply of 2.45V per cell. For this Application Note, 4.90V is used as the charging voltage for the 4V SLA battery. At the start of charging, depending on their state of charge, SLA batteries require huge amounts of current. If this current uptake is not controlled, the battery electrolyte may boil, producing gasses inside the battery. It is therefore necessary to limit the charging current. As soon as the battery achieves some charge, the current is limited and constant voltage charging is enforced. The charge termination mechanism is simple and is achieved as battery voltage reaches the charging voltage. At the same time, there is a corresponding drop in charging current. Nickel Cadmium (NiCad) NiCad batteries are used in camcorders, Walkmans, and other similar consumer portable equipment. The single-cell voltage for NiCad batteries is 1.2V. These batteries are charged using the constant current charging method. While charging, as the voltage crosses the full charge point, it starts dropping. This drop is approximately 15mV per cell in the battery. This drop is recognized as a full charge condition, and charging is terminated. This termination mechanism is named as –∆V termination. During charging, battery voltage rises to 1.65V per cell.

AN013702-0403

Battery Technology

Z8

Encore!™-Based

Application Note Battery Charger 45

The disadvantage of the NiCad battery is that the battery must be periodically discharged to protect performance. In battery parlance, this phenomenon is known as memory effect. Nickel Metal Hydride (NiMH) NiMH batteries exhibit higher power density compared to their NiCad counterparts. The per-cell voltage of the NiMH battery type is 1.2 V, similar to NiCad batteries. NiMH batteries are charged via the constant current charging method. While charging, as the voltage crosses the full charge point, the voltage drop is not as low as for the NiCad batteries. As a consequence, –∆V charge termination is usually not recommended for these batteries. Instead of the fall in cell voltage, the battery tends to plateau after a small drop. This flat region is the preferred indication for full battery charging, rather than the drop. Consequently, this termination mechanism is named zero ∆V termination. NiMH batteries do not suffer from memory effect as do NiCad batteries. As a result, they replace NiCad battery types in applications such as cell phones because the increase in price is justified by the reduction in weight and absence of memory effect. Lithium Ion (Li-Ion) Li-Ion batteries are lighter in weight than NiCad and NiMH batteries. Available with a high voltage rating of 3.7V, one Li-Ion battery can replace three NiCad/NiMH battery types. These two features make Li-Ion high-energy density batteries. They exhibit flat discharge characteristics and are free from memory effect. If the starting voltage of these batteries is initially too low, a small constant current is applied until the battery reaches a certain threshold specified by the manufacturer. The battery is charged with constant voltage when this threshold is crossed. Charging is terminated when battery voltage reaches the rated voltage.

AN013702-0403

Battery Technology

Sign up to vote on this title
UsefulNot useful