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 "LIIONspecs.h" 00040 #include "PWM.h" 00041 #include "time.h" 00042 00043 #ifndef LIION 00044 #error LIION not defined in main.h! 00045 #endif // LIION 00046 00047 00048 //****************************************************************************** 00049 // Functions 00050 //****************************************************************************** 00068 unsigned char Charge(unsigned char inp) 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 }