# LIIONcharge.c File Reference

## Detailed Description

Charge state function for Li-Ion batteries.

Contains the charge state function, in which the Li-Ion charging algorithm is, plus the associated functions.

Application note:
  AVR458: Charging Li-Ion Batteries with BC100

Documentation
  For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html

Author:
  Atmel Corporation: http://www.atmel.com
  Support email: avr@atmel.com

$Name$

Revision
  4254

$RCSfile$

URL
  http://revisor.norway.atmel.com/AppsAVR8/avr458_Charging_Li-Ion_Batteries_with_BC100/trunk/code/IAR/LIIONcharge.c

Date
  2008-07-25 12:32:45 +0200 (fr, 25 jul 2008)

Definition in file LIIONcharge.c.

`#include <ioavr.h>`
`#include "enums.h"`
`#include "structs.h"`
`#include "battery.h"`
`#include "charge.h"`
`#include "chargefunc.h"`
`#include "main.h"`
`#include "menu.h"`
`#include "LIIONspecs.h"`
`#include "PWM.h"`
`#include "time.h"`

Include dependency graph for LIIONcharge.c:

Go to the source code of this file.

## Functions
unsigned char | Charge (unsigned char inp)
| Controls the charging.

## Function Documentation

unsigned char Charge | ( | unsigned char | inp | ) |

Controls the charging.

Define for compliance with Japanese regulations for battery chargers.

This function contains the charging algorithm itself, divided into stages.
For each stage the PWM may be started/stopped, and the timer, halt-requirements and charge parameters may be set.
The charging functions return whatever state is next, and as long as no errors occur it will be the next charging stage.

Note:
  For the charger to follow Japanese regulations, define JAPANREGS in charge.h . In this case, the charger will use only the ST_MAXVOLTCURR state.

  If more stages are needed simply define more states in menu.h, include them in menu_state[] in menu.c, then add the cases to this function.

  This algorithm is strictly for Li-Ion batteries.

Definition at line 68 of file LIIONcharge.c.

References BAT_CURRENT_PREQUAL, BAT_TEMPERATURE_MAX, BAT_TEMPERATURE_MIN, BAT_TIME_PREQUAL, BAT_VOLTAGE_MAX, BAT_VOLTAGE_PREQUAL, BattActive, BattControl, BattData, Batteries_struct::Charged, ChargeParameters, ConstantCurrent(), ConstantVoltage(), ChargeParameters_struct::Current, HaltParameters_struct::CurrentMin, CurrentState, HALT_CURRENT_MIN, HALT_FLAG_EXHAUSTION, HALT_TIME, HALT_VOLTAGE_MAX, HaltParameters_struct::HaltFlags, HaltParameters, Batteries_struct::MaxCurrent, Batteries_struct::MaxTime, MaxVoltageAndCurrent(), Batteries_struct::MinCurrent, ChargeParameters_struct::NextState, PWM_Start(), PWM_Stop(), ST_BATCON, ST_CCURRENT, ST_CVOLTAGE, ST_ENDCHARGE, ST_INIT, ST_MAXVOLTCURR, ST_PREQUAL, ST_SLEEP, HaltParameters_struct::TemperatureMax, HaltParameters_struct::TemperatureMin, Time_Set(), TIMER_CHG, TRUE, ChargeParameters_struct::Voltage, and HaltParameters_struct::VoltageMax.

    00069 {
    00070         unsigned char NextState;
    00071
    00072         switch (CurrentState)   {
    00073         case ST_PREQUAL:  // First step is prequalification.
    00074
    00075                 // Charge with the defined prequalifiction-current, and if no errors
    00076                 // occur return ST_CCURRENT as the next state.
    00077                 ChargeParameters.Current = BAT_CURRENT_PREQUAL;
    00078
    00079 // Set the next state depending on what charger behaviour is defined.
    00080 #ifdef JAPANREGS
    00081                 ChargeParameters.NextState = ST_MAXVOLTCURR;
    00082 #else
    00083                 ChargeParameters.NextState = ST_CCURRENT;
    00084 #endif // JAPANREGS
    00085
    00086                 // We want charging to halt if voltage reaches a limit or time runs out.
    00087                 // In case of timeout the battery will be flagged as exhausted, and an
    00088                 // error will be flagged.
    00089                 HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME |
    00090                                             HALT_FLAG_EXHAUSTION);
    00091
    00092                 // Set the maximum temperature and charge voltage limit.
    00093                 HaltParameters.TemperatureMax = BAT_TEMPERATURE_MAX;
    00094                 HaltParameters.TemperatureMin = BAT_TEMPERATURE_MIN;
    00095                 HaltParameters.VoltageMax = BAT_VOLTAGE_PREQUAL;
    00096
    00097                 // Start PWM output and charging timer first.
    00098                 PWM_Start();
    00099                 Time_Set(TIMER_CHG, BAT_TIME_PREQUAL, 0, 0);
    00100
    00101                 // Call the constant current charging-function.
    00102                 // If all goes well, we will get ST_CCURRENT in return.
    00103                 NextState = ConstantCurrent();
    00104         break;
    00105
    00106
    00107         case ST_CCURRENT:  // Second step is constant current charging.
    00108
    00109                 // Set charging timer to the battery's maximum charge time.
    00110                 Time_Set(TIMER_CHG,BattData.MaxTime,0,0);
    00111
    00112                 // Charge at the battery's maximum current, go to ST_CVOLTAGE next.
    00113                 ChargeParameters.Current = BattData.MaxCurrent;
    00114                 ChargeParameters.NextState = ST_CVOLTAGE;
    00115
    00116
    00117                 // Charge until the defined BatChargeVoltage is reached.
    00118                 HaltParameters.VoltageMax = BAT_VOLTAGE_MAX;
    00119
    00120                 // Start charging using constant current.
    00121                 NextState = ConstantCurrent();
    00122         break;
    00123
    00124
    00125         case ST_CVOLTAGE:  // Third step is constant voltage charging.
    00126
    00127                 // Charge with the defined charge-voltage, go to ST_ENDCHARGE next.
    00128                 ChargeParameters.Voltage = BAT_VOLTAGE_MAX;
    00129                 ChargeParameters.NextState = ST_ENDCHARGE;
    00130
    00131                 // We want charging to halt if temperature rises too high, if current
    00132                 // sinks below limit, or time runs out. Also, flag error if temperature
    00133                 // limit is reached. Timeout doesn't mean anything is wrong at this point.
    00134                 HaltParameters.HaltFlags = (HALT_CURRENT_MIN | HALT_TIME);
    00135
    00136                 HaltParameters.CurrentMin = BattData.MinCurrent;
    00137
    00138                 // Start charging using constant voltage. We will continue on the
    00139                 // timer started in ST_CCURRENT.
    00140                 NextState = ConstantVoltage();
    00141         break;
    00142
    00143
    00144         case ST_MAXVOLTCURR:
    00145                 // No need to specify charge voltage or current, go to ST_ENDCHARGE next.
    00146                 ChargeParameters.NextState = ST_ENDCHARGE;
    00147
    00148                 // We want charging to halt if temperature rises too high, if current
    00149                 // sinks below limit, or time runs out. Also, flag error if temperature
    00150                 // limit is reached. (With this setup, the charging is timed, but the
    00151       // battery is not flagged as exhausted if the timer runs out.)
    00152                 HaltParameters.HaltFlags = (HALT_CURRENT_MIN | HALT_TIME);
    00153                 HaltParameters.CurrentMin = BattData.MinCurrent;
    00154
    00155         // Start charge timer.
    00156                 Time_Set(TIMER_CHG,BattData.MaxTime,0,0);
    00157
    00158                 // Start charging using maximum voltage and current for the temperature range.
    00159         NextState = MaxVoltageAndCurrent();
    00160         break;
    00161
    00162
    00163         case ST_ENDCHARGE:  // Charging is done!
    00164
    00165                 PWM_Stop();
    00166                 BattData.Charged = TRUE;
    00167
    00168                 // If the other battery is enabled go to ST_BATCON, otherwise
    00169                 // go to ST_SLEEP.
    00170                 if (BattControl[(BattActive+1)%2].Enabled) {
    00171                         NextState = ST_BATCON;
    00172                 } else {
    00173                         NextState = ST_SLEEP;
    00174                 }
    00175         break;
    00176
    00177
    00178         default:  // Shouldn't end up here. Reinitialize for safety.
    00179                 NextState = ST_INIT;
    00180                 break;
    00181         }
    00182
    00183         // Return the next state.
    00184         return(NextState);
    00185 }

Here is the call graph for this function: