1 /* This file has been prepared for Doxygen automatic documentation generation.*/
\r
2 /*! \file *********************************************************************
\r
5 * Charge state function for NiMH batteries
\r
7 * Contains the charge state function, in which the NiMH charging
\r
8 * algorithm is, plus the associated functions.
\r
10 * \par Application note:
\r
11 * AVR463: Charging NiMH Batteries with BC100 \n
\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/avr463_Charging_NiMH_Batteries_with_BC100/trunk/code/IAR/NIMHcharge.c $
\r
26 * $Date: 2007-08-09 14:47:58 +0200 (to, 09 aug 2007) $\n
\r
27 ******************************************************************************/
\r
32 #include "structs.h"
\r
34 #include "battery.h"
\r
36 #include "chargefunc.h"
\r
39 #include "NIMHspecs.h"
\r
44 #error NIMH 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 NiMH batteries.
\r
64 unsigned char Charge(unsigned char inp)
\r
66 unsigned char NextState;
\r
68 switch (CurrentState) {
\r
69 // First stage is a prequalification. Attempt to charge battery to 1 V,
\r
70 // using a 0.1 C current, within 2 minutes.
\r
71 // If this fails, the battery is likely damaged.
\r
72 // If it succeeds, start a fast charge.
\r
75 // Set up charge current and next state.
\r
76 ChargeParameters.Current = BattData.Capacity / 10;
\r
77 ChargeParameters.NextState = ST_FASTCHARGE;
\r
79 // Halt charge on voltage limit or timeout.
\r
80 // Timeout means battery exhaustion.
\r
81 HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME |
\r
82 HALT_FLAG_EXHAUSTION);
\r
84 // Set up voltage limit and temperature limits.
\r
85 HaltParameters.VoltageMax = BAT_VOLTAGE_PREQUAL;
\r
86 HaltParameters.TemperatureMin = BAT_TEMPERATURE_MIN;
\r
87 HaltParameters.TemperatureMax = 35;
\r
89 // Reset temperature measurement for HaltNow().
\r
90 HaltParameters.LastNTC = 0;
\r
92 // Start PWM and charge timer before calling the charge function.
\r
94 Time_Set(TIMER_CHG, BAT_TIME_PREQUAL, 0, 0);
\r
96 // Call charge function, get next state.
\r
97 NextState = ConstantCurrent();
\r
101 // Second stage is a fast charge. Charge at 1.0 C for at most 1.5 hours,
\r
102 // until either rate of temperature increase or voltage reaches limit, or
\r
103 // the voltage drops sufficiently.
\r
104 // Timeout doesn't mean battery exhaustion now.
\r
105 case ST_FASTCHARGE:
\r
107 // Set up charge current and next state.
\r
108 ChargeParameters.Current = BattData.Capacity;
\r
109 ChargeParameters.NextState = ST_LOWRATECHARGE;
\r
111 // Halt charge on voltage limit, timeout, voltage drop or rate of
\r
112 // temperature increase.
\r
113 HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME |
\r
114 HALT_VOLTAGE_DROP | HALT_TEMPERATURE_RISE);
\r
116 // Set up limits for voltage, voltage drop, temperature and rate of
\r
117 // temperature increase (1 degree C per minute).
\r
118 HaltParameters.VoltageMax = BAT_VOLTAGE_MAX;
\r
119 HaltParameters.VoltageDrop = BAT_VOLTAGE_DROP;
\r
120 HaltParameters.TemperatureMax = BAT_TEMPERATURE_MAX;
\r
121 HaltParameters.TemperatureRise = 1;
\r
123 // Reset maximum voltage measurement for HaltNow().
\r
124 HaltParameters.VBATMax = 0;
\r
126 // Start timer, PWM should still be running.
\r
127 Time_Set(TIMER_CHG, BattData.MaxTime, 0, 0);
\r
129 // Call charge function, get next state.
\r
130 NextState = ConstantCurrent();
\r
134 // Last stage is a trickle charge. Charge at 0.1 C for at most 30 minutes,
\r
135 // until either rate of temperature increase or voltage reaches limit.
\r
136 case ST_LOWRATECHARGE:
\r
138 // Set up charge current and next state.
\r
139 ChargeParameters.Current = BattData.Capacity / 10;
\r
140 ChargeParameters.NextState = ST_ENDCHARGE;
\r
142 // Halt charge on voltage limit, timeout or temperature rise.
\r
143 // Use the same requirements as during the last stage (ST_FASTCHARGE).
\r
144 HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME |
\r
145 HALT_TEMPERATURE_RISE);
\r
147 // Start timer, 30 minutes.
\r
148 Time_Set(TIMER_CHG, 30, 0, 0);
\r
150 // Call charge function, get next state.
\r
151 NextState = ConstantCurrent();
\r
155 // Charging is done!
\r
158 // Stop the PWM output and flag battery as charged.
\r
160 BattData.Charged = TRUE;
\r
162 // If the other battery is enabled go to ST_BATCON, otherwise
\r
164 if (eeprom_read_byte(&BattControl[(BattActive+1)%2]) & BIT_BATTERY_ENABLED) {
\r
165 NextState = ST_BATCON;
\r
167 NextState = ST_SLEEP;
\r
172 default: // Shouldn't end up here. Reinitialize for safety.
\r
173 NextState = ST_INIT;
\r
177 // Return the next state to main().
\r