Contains the charge state function, in which the NiMH charging algorithm is, plus the associated functions.
Definition in file NIMHcharge.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 "NIMHspecs.h"
#include "PWM.h"
#include "time.h"
Include dependency graph for NIMHcharge.c:

Go to the source code of this file.
Functions | |
| unsigned char | Charge (unsigned char inp) |
| Controls the charging. | |
| unsigned char Charge | ( | unsigned char | inp | ) |
Controls the charging.
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 this is the next charging stage.
This algorithm is for NiMH batteries.
Definition at line 64 of file NIMHcharge.c.
References BAT_TEMPERATURE_MAX, BAT_TEMPERATURE_MIN, BAT_TIME_PREQUAL, BAT_VOLTAGE_DROP, BAT_VOLTAGE_MAX, BAT_VOLTAGE_PREQUAL, BattActive, BattControl, BattData, Batteries_struct::Capacity, Batteries_struct::Charged, ChargeParameters, ConstantCurrent(), ChargeParameters_struct::Current, CurrentState, HALT_FLAG_EXHAUSTION, HALT_TEMPERATURE_RISE, HALT_TIME, HALT_VOLTAGE_DROP, HALT_VOLTAGE_MAX, HaltParameters_struct::HaltFlags, HaltParameters, HaltParameters_struct::LastNTC, Batteries_struct::MaxTime, ChargeParameters_struct::NextState, PWM_Start(), PWM_Stop(), ST_BATCON, ST_ENDCHARGE, ST_FASTCHARGE, ST_INIT, ST_LOWRATECHARGE, ST_PREQUAL, ST_SLEEP, HaltParameters_struct::TemperatureMax, HaltParameters_struct::TemperatureMin, HaltParameters_struct::TemperatureRise, Time_Set(), TIMER_CHG, TRUE, HaltParameters_struct::VBATMax, HaltParameters_struct::VoltageDrop, and HaltParameters_struct::VoltageMax.
00065 { 00066 unsigned char NextState; 00067 00068 switch (CurrentState) { 00069 // First stage is a prequalification. Attempt to charge battery to 1 V, 00070 // using a 0.1 C current, within 2 minutes. 00071 // If this fails, the battery is likely damaged. 00072 // If it succeeds, start a fast charge. 00073 case ST_PREQUAL: 00074 00075 // Set up charge current and next state. 00076 ChargeParameters.Current = BattData.Capacity / 10; 00077 ChargeParameters.NextState = ST_FASTCHARGE; 00078 00079 // Halt charge on voltage limit or timeout. 00080 // Timeout means battery exhaustion. 00081 HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME | 00082 HALT_FLAG_EXHAUSTION); 00083 00084 // Set up voltage limit and temperature limits. 00085 HaltParameters.VoltageMax = BAT_VOLTAGE_PREQUAL; 00086 HaltParameters.TemperatureMin = BAT_TEMPERATURE_MIN; 00087 HaltParameters.TemperatureMax = 35; 00088 00089 // Reset temperature measurement for HaltNow(). 00090 HaltParameters.LastNTC = 0; 00091 00092 // Start PWM and charge timer before calling the charge function. 00093 PWM_Start(); 00094 Time_Set(TIMER_CHG, BAT_TIME_PREQUAL, 0, 0); 00095 00096 // Call charge function, get next state. 00097 NextState = ConstantCurrent(); 00098 break; 00099 00100 00101 // Second stage is a fast charge. Charge at 1.0 C for at most 1.5 hours, 00102 // until either rate of temperature increase or voltage reaches limit, or 00103 // the voltage drops sufficiently. 00104 // Timeout doesn't mean battery exhaustion now. 00105 case ST_FASTCHARGE: 00106 00107 // Set up charge current and next state. 00108 ChargeParameters.Current = BattData.Capacity; 00109 ChargeParameters.NextState = ST_LOWRATECHARGE; 00110 00111 // Halt charge on voltage limit, timeout, voltage drop or rate of 00112 // temperature increase. 00113 HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME | 00114 HALT_VOLTAGE_DROP | HALT_TEMPERATURE_RISE); 00115 00116 // Set up limits for voltage, voltage drop, temperature and rate of 00117 // temperature increase (1 degree C per minute). 00118 HaltParameters.VoltageMax = BAT_VOLTAGE_MAX; 00119 HaltParameters.VoltageDrop = BAT_VOLTAGE_DROP; 00120 HaltParameters.TemperatureMax = BAT_TEMPERATURE_MAX; 00121 HaltParameters.TemperatureRise = 1; 00122 00123 // Reset maximum voltage measurement for HaltNow(). 00124 HaltParameters.VBATMax = 0; 00125 00126 // Start timer, PWM should still be running. 00127 Time_Set(TIMER_CHG, BattData.MaxTime, 0, 0); 00128 00129 // Call charge function, get next state. 00130 NextState = ConstantCurrent(); 00131 break; 00132 00133 00134 // Last stage is a trickle charge. Charge at 0.1 C for at most 30 minutes, 00135 // until either rate of temperature increase or voltage reaches limit. 00136 case ST_LOWRATECHARGE: 00137 00138 // Set up charge current and next state. 00139 ChargeParameters.Current = BattData.Capacity / 10; 00140 ChargeParameters.NextState = ST_ENDCHARGE; 00141 00142 // Halt charge on voltage limit, timeout or temperature rise. 00143 // Use the same requirements as during the last stage (ST_FASTCHARGE). 00144 HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME | 00145 HALT_TEMPERATURE_RISE); 00146 00147 // Start timer, 30 minutes. 00148 Time_Set(TIMER_CHG, 30, 0, 0); 00149 00150 // Call charge function, get next state. 00151 NextState = ConstantCurrent(); 00152 break; 00153 00154 00155 // Charging is done! 00156 case ST_ENDCHARGE: 00157 00158 // Stop the PWM output and flag battery as charged. 00159 PWM_Stop(); 00160 BattData.Charged = TRUE; 00161 00162 // If the other battery is enabled go to ST_BATCON, otherwise 00163 // go to ST_SLEEP. 00164 if (BattControl[(BattActive+1)%2].Enabled) { 00165 NextState = ST_BATCON; 00166 } else { 00167 NextState = ST_SLEEP; 00168 } 00169 break; 00170 00171 00172 default: // Shouldn't end up here. Reinitialize for safety. 00173 NextState = ST_INIT; 00174 break; 00175 } 00176 00177 // Return the next state to main(). 00178 return(NextState); 00179 }
Here is the call graph for this function:

1.5.2