--- /dev/null
+/* This file has been prepared for Doxygen automatic documentation generation.*/\r
+/*! \file *********************************************************************\r
+ *\r
+ * \brief\r
+ * Charge state function for Li-Ion batteries\r
+ *\r
+ * Contains the charge state function, in which the Li-Ion charging\r
+ * algorithm is, plus the associated functions.\r
+ *\r
+ * \par Application note:\r
+ * AVR458: Charging Li-Ion Batteries with BC100\r
+ *\r
+ * \par Documentation\r
+ * For comprehensive code documentation, supported compilers, compiler \r
+ * settings and supported devices see readme.html\r
+ *\r
+ * \author\r
+ * Atmel Corporation: http://www.atmel.com \n\r
+ * Support email: avr@atmel.com\r
+ *\r
+ * \r
+ * $Name$\r
+ * $Revision: 2261 $\r
+ * $RCSfile$\r
+ * $URL: http://svn.norway.atmel.com/AppsAVR8/avr458_Charging_Li-Ion_Batteries_with_BC100/tag/20070904_release_1.0/code/IAR/LIIONcharge.c $\r
+ * $Date: 2007-08-10 09:28:35 +0200 (fr, 10 aug 2007) $\n\r
+ ******************************************************************************/\r
+\r
+#include <avr/io.h>\r
+\r
+#include "enums.h"\r
+#include "structs.h"\r
+\r
+#include "battery.h"\r
+#include "charge.h"\r
+#include "chargefunc.h"\r
+#include "main.h"\r
+#include "menu.h"\r
+#include "LIIONspecs.h"\r
+#include "PWM.h"\r
+#include "time.h"\r
+\r
+#ifndef LIION\r
+#error LIION not defined in main.h!\r
+#endif // LIION\r
+\r
+\r
+//******************************************************************************\r
+// Functions\r
+//******************************************************************************\r
+/*! \brief Controls the charging.\r
+ *\r
+ * This function contains the charging algorithm itself, divided into stages.\n\r
+ * For each stage the PWM may be started/stopped, and the timer, \r
+ * halt-requirements and charge parameters may be set.\n\r
+ * The charging functions return whatever state is next, and as long as no\r
+ * errors occur this is the next charging stage.\r
+ *\r
+ * \note If more stages are needed simply define more states in menu.h, include\r
+ * them in \ref menu_state[] in menu.c, then add the cases to this function.\r
+ *\r
+ * \note This algorithm is for Li-Ion batteries.\r
+ */\r
+unsigned char Charge(unsigned char inp)\r
+{\r
+ unsigned char NextState;\r
+\r
+ switch (CurrentState) {\r
+ case ST_PREQUAL: // First step is prequalification.\r
+ \r
+ // Charge with the defined prequalifiction-current, and if no errors\r
+ // occur return ST_CCURRENT as the next state.\r
+ ChargeParameters.Current = BAT_CURRENT_PREQUAL;\r
+ ChargeParameters.NextState = ST_CCURRENT;\r
+ \r
+ // We want charging to halt if voltage reaches a limit or time runs out.\r
+ // In case of timeout the battery will be flagged as exhausted, and an\r
+ // error will be flagged.\r
+ HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME | \r
+ HALT_FLAG_EXHAUSTION);\r
+ \r
+ // Set the maximum temperature and charge voltage limit.\r
+ HaltParameters.TemperatureMax = BAT_TEMPERATURE_MAX;\r
+ HaltParameters.TemperatureMin = BAT_TEMPERATURE_MIN;\r
+ HaltParameters.VoltageMax = BAT_VOLTAGE_PREQUAL;\r
+\r
+ // Start PWM output and charging timer first.\r
+ PWM_Start();\r
+ Time_Set(TIMER_CHG, BAT_TIME_PREQUAL, 0, 0);\r
+\r
+ // Call the constant current charging-function.\r
+ // If all goes well, we will get ST_CCURRENT in return.\r
+ NextState = ConstantCurrent();\r
+ break;\r
+ \r
+\r
+ case ST_CCURRENT: // Second step is constant current charging.\r
+ \r
+ // Set charging timer to the battery's maximum charge time.\r
+ Time_Set(TIMER_CHG,BattData.MaxTime,0,0);\r
+\r
+ // Charge at the battery's maximum current, go to ST_CVOLTAGE next.\r
+ ChargeParameters.Current = BattData.MaxCurrent;\r
+ ChargeParameters.NextState = ST_CVOLTAGE;\r
+\r
+\r
+ // Charge until the defined BatChargeVoltage is reached.\r
+ HaltParameters.VoltageMax = BAT_VOLTAGE_MAX;\r
+ \r
+ // Start charging using constant current.\r
+ NextState = ConstantCurrent();\r
+ break;\r
+\r
+\r
+ case ST_CVOLTAGE: // Third step is constant voltage charging.\r
+ \r
+ // Charge with the defined charge-voltage, go to ST_ENDCHARGE next.\r
+ ChargeParameters.Voltage = BAT_VOLTAGE_MAX;\r
+ ChargeParameters.NextState = ST_ENDCHARGE;\r
+ \r
+ // We want charging to halt if temperature rises too high, if current\r
+ // sinks below limit, or time runs out. Also, flag error if temperature\r
+ // limit is reached. Timeout doesn't mean anything is wrong at this point.\r
+ HaltParameters.HaltFlags = (HALT_CURRENT_MIN | HALT_TIME);\r
+ \r
+ HaltParameters.CurrentMin = BattData.MinCurrent;\r
+ \r
+ // Start charging using constant voltage. We will continue on the \r
+ // timer started in ST_CCURRENT.\r
+ NextState = ConstantVoltage(); \r
+ break;\r
+\r
+\r
+ case ST_ENDCHARGE: // Charging is done!\r
+\r
+ PWM_Stop();\r
+ BattData.Charged = TRUE;\r
+ \r
+ // If the other battery is enabled go to ST_BATCON, otherwise\r
+ // go to ST_SLEEP.\r
+ if (eeprom_read_byte(&BattControl[(BattActive+1)%2]) & BIT_BATTERY_ENABLED) {\r
+ NextState = ST_BATCON;\r
+ } else {\r
+ NextState = ST_SLEEP;\r
+ } \r
+ break;\r
+\r
+\r
+ default: // Shouldn't end up here. Reinitialize for safety.\r
+ NextState = ST_INIT;\r
+ break;\r
+ }\r
+\r
+ // Return the next state.\r
+ return(NextState);\r
+}\r