1 /* This file has been prepared for Doxygen automatic documentation generation.*/
\r
2 /*! \file *********************************************************************
\r
5 * Charge state function for Li-Ion batteries
\r
7 * Contains the charge state function, in which the Li-Ion charging
\r
8 * algorithm is, plus the associated functions.
\r
10 * \par Application note:
\r
11 * AVR458: Charging Li-Ion Batteries with BC100
\r
13 * \par Documentation
\r
14 * For comprehensive code documentation, supported compilers, compiler
\r
15 * settings and supported devices see readme.html
\r
18 * Atmel Corporation: http://www.atmel.com \n
\r
19 * Support email: avr@atmel.com
\r
25 * $URL: http://svn.norway.atmel.com/AppsAVR8/avr458_Charging_Li-Ion_Batteries_with_BC100/tag/20070904_release_1.0/code/IAR/LIIONcharge.c $
\r
26 * $Date: 2007-08-10 09:28:35 +0200 (fr, 10 aug 2007) $\n
\r
27 ******************************************************************************/
\r
32 #include "structs.h"
\r
34 #include "battery.h"
\r
36 #include "chargefunc.h"
\r
39 #include "LIIONspecs.h"
\r
44 #error LIION not defined in main.h!
\r
48 //******************************************************************************
\r
50 //******************************************************************************
\r
51 /*! \brief Controls the charging.
\r
53 * This function contains the charging algorithm itself, divided into stages.\n
\r
54 * For each stage the PWM may be started/stopped, and the timer,
\r
55 * halt-requirements and charge parameters may be set.\n
\r
56 * The charging functions return whatever state is next, and as long as no
\r
57 * errors occur this is the next charging stage.
\r
59 * \note If more stages are needed simply define more states in menu.h, include
\r
60 * them in \ref menu_state[] in menu.c, then add the cases to this function.
\r
62 * \note This algorithm is for Li-Ion batteries.
\r
64 unsigned char Charge(unsigned char inp)
\r
66 unsigned char NextState;
\r
68 switch (CurrentState) {
\r
69 case ST_PREQUAL: // First step is prequalification.
\r
71 // Charge with the defined prequalifiction-current, and if no errors
\r
72 // occur return ST_CCURRENT as the next state.
\r
73 ChargeParameters.Current = BAT_CURRENT_PREQUAL;
\r
74 ChargeParameters.NextState = ST_CCURRENT;
\r
76 // We want charging to halt if voltage reaches a limit or time runs out.
\r
77 // In case of timeout the battery will be flagged as exhausted, and an
\r
78 // error will be flagged.
\r
79 HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME |
\r
80 HALT_FLAG_EXHAUSTION);
\r
82 // Set the maximum temperature and charge voltage limit.
\r
83 HaltParameters.TemperatureMax = BAT_TEMPERATURE_MAX;
\r
84 HaltParameters.TemperatureMin = BAT_TEMPERATURE_MIN;
\r
85 HaltParameters.VoltageMax = BAT_VOLTAGE_PREQUAL;
\r
87 // Start PWM output and charging timer first.
\r
89 Time_Set(TIMER_CHG, BAT_TIME_PREQUAL, 0, 0);
\r
91 // Call the constant current charging-function.
\r
92 // If all goes well, we will get ST_CCURRENT in return.
\r
93 NextState = ConstantCurrent();
\r
97 case ST_CCURRENT: // Second step is constant current charging.
\r
99 // Set charging timer to the battery's maximum charge time.
\r
100 Time_Set(TIMER_CHG,BattData.MaxTime,0,0);
\r
102 // Charge at the battery's maximum current, go to ST_CVOLTAGE next.
\r
103 ChargeParameters.Current = BattData.MaxCurrent;
\r
104 ChargeParameters.NextState = ST_CVOLTAGE;
\r
107 // Charge until the defined BatChargeVoltage is reached.
\r
108 HaltParameters.VoltageMax = BAT_VOLTAGE_MAX;
\r
110 // Start charging using constant current.
\r
111 NextState = ConstantCurrent();
\r
115 case ST_CVOLTAGE: // Third step is constant voltage charging.
\r
117 // Charge with the defined charge-voltage, go to ST_ENDCHARGE next.
\r
118 ChargeParameters.Voltage = BAT_VOLTAGE_MAX;
\r
119 ChargeParameters.NextState = ST_ENDCHARGE;
\r
121 // We want charging to halt if temperature rises too high, if current
\r
122 // sinks below limit, or time runs out. Also, flag error if temperature
\r
123 // limit is reached. Timeout doesn't mean anything is wrong at this point.
\r
124 HaltParameters.HaltFlags = (HALT_CURRENT_MIN | HALT_TIME);
\r
126 HaltParameters.CurrentMin = BattData.MinCurrent;
\r
128 // Start charging using constant voltage. We will continue on the
\r
129 // timer started in ST_CCURRENT.
\r
130 NextState = ConstantVoltage();
\r
134 case ST_ENDCHARGE: // Charging is done!
\r
137 BattData.Charged = TRUE;
\r
139 // If the other battery is enabled go to ST_BATCON, otherwise
\r
141 if (BattControl[(BattActive+1)%2].Enabled) {
\r
142 NextState = ST_BATCON;
\r
144 NextState = ST_SLEEP;
\r
149 default: // Shouldn't end up here. Reinitialize for safety.
\r
150 NextState = ST_INIT;
\r
154 // Return the next state.
\r