00001 /* This file has been prepared for Doxygen automatic documentation generation.*/ 00029 #include <ioavr.h> 00030 00031 #include "enums.h" 00032 #include "structs.h" 00033 00034 #include "battery.h" 00035 #include "charge.h" 00036 #include "chargefunc.h" 00037 #include "main.h" 00038 #include "menu.h" 00039 #include "NIMHspecs.h" 00040 #include "PWM.h" 00041 #include "time.h" 00042 00043 #ifndef NIMH 00044 #error NIMH not defined in main.h! 00045 #endif // NIMH 00046 00047 00048 //****************************************************************************** 00049 // Functions 00050 //****************************************************************************** 00064 unsigned char Charge(unsigned char inp) 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 }
1.5.2