add missing avr458 Makefile
[avr_bc100.git] / BaseTinyFirmware / GCC / nimhcharge.c
1 /* This file has been prepared for Doxygen automatic documentation generation.*/\r
2 /*! \file *********************************************************************\r
3  *\r
4  * \brief\r
5  *      Charge state function for NiMH batteries\r
6  *\r
7  *      Contains the charge state function, in which the NiMH charging\r
8  *      algorithm is, plus the associated functions.\r
9  *\r
10  * \par Application note:\r
11  *      AVR463: Charging NiMH Batteries with BC100 \n\r
12  *\r
13  * \par Documentation\r
14  *      For comprehensive code documentation, supported compilers, compiler \r
15  *      settings and supported devices see readme.html\r
16  *\r
17  * \author\r
18  *      Atmel Corporation: http://www.atmel.com \n\r
19  *      Support email: avr@atmel.com\r
20  *\r
21  * \r
22  * $Name$\r
23  * $Revision: 2255 $\r
24  * $RCSfile$\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
28 \r
29 #include <avr/io.h>\r
30 \r
31 #include "enums.h"\r
32 #include "structs.h"\r
33 \r
34 #include "battery.h"\r
35 #include "charge.h"\r
36 #include "chargefunc.h"\r
37 #include "main.h"\r
38 #include "menu.h"\r
39 #include "NIMHspecs.h"\r
40 #include "PWM.h"\r
41 #include "time.h"\r
42 \r
43 #ifndef NIMH\r
44 #error NIMH not defined in main.h!\r
45 #endif // NIMH\r
46 \r
47 \r
48 //******************************************************************************\r
49 // Functions\r
50 //******************************************************************************\r
51 /*! \brief Controls the charging.\r
52  *\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
58  *\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
61  *\r
62  * \note This algorithm is for NiMH batteries.\r
63  */\r
64 unsigned char Charge(unsigned char inp)\r
65 {\r
66         unsigned char NextState;\r
67 \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
73         case ST_PREQUAL:\r
74                 \r
75                 // Set up charge current and next state.\r
76                 ChargeParameters.Current = BattData.Capacity / 10;\r
77                 ChargeParameters.NextState = ST_FASTCHARGE;\r
78                 \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
83                 \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
88                 \r
89                 // Reset temperature measurement for HaltNow().\r
90                 HaltParameters.LastNTC = 0;\r
91                 \r
92                 // Start PWM and charge timer before calling the charge function.\r
93                 PWM_Start();\r
94                 Time_Set(TIMER_CHG, BAT_TIME_PREQUAL, 0, 0);\r
95                 \r
96                 // Call charge function, get next state.\r
97                 NextState = ConstantCurrent();\r
98         break;\r
99 \r
100 \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
106 \r
107                 // Set up charge current and next state.\r
108                 ChargeParameters.Current = BattData.Capacity;\r
109                 ChargeParameters.NextState = ST_LOWRATECHARGE;\r
110                 \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
115                 \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
122                 \r
123                 // Reset maximum voltage measurement for HaltNow().\r
124                 HaltParameters.VBATMax = 0;\r
125 \r
126                 // Start timer, PWM should still be running.\r
127                 Time_Set(TIMER_CHG, BattData.MaxTime, 0, 0);\r
128 \r
129                 // Call charge function, get next state.\r
130                 NextState = ConstantCurrent();\r
131         break;\r
132 \r
133 \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
137                 \r
138                 // Set up charge current and next state.\r
139                 ChargeParameters.Current = BattData.Capacity / 10;\r
140                 ChargeParameters.NextState = ST_ENDCHARGE;\r
141                 \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
146 \r
147                 // Start timer, 30 minutes.\r
148                 Time_Set(TIMER_CHG, 30, 0, 0);\r
149                 \r
150                 // Call charge function, get next state.\r
151                 NextState = ConstantCurrent();\r
152         break;\r
153 \r
154 \r
155         // Charging is done!\r
156         case ST_ENDCHARGE:\r
157 \r
158                 // Stop the PWM output and flag battery as charged.\r
159                 PWM_Stop();\r
160                 BattData.Charged = TRUE;\r
161                 \r
162                 // If the other battery is enabled go to ST_BATCON, otherwise\r
163                 // go to ST_SLEEP.\r
164                 if (eeprom_read_byte(&BattControl[(BattActive+1)%2]) & BIT_BATTERY_ENABLED) {\r
165                         NextState = ST_BATCON;\r
166                 } else {\r
167                         NextState = ST_SLEEP;\r
168                 }               \r
169         break;\r
170 \r
171 \r
172         default:  // Shouldn't end up here. Reinitialize for safety.\r
173                 NextState = ST_INIT;\r
174                 break;\r
175         }\r
176 \r
177         // Return the next state to main().\r
178         return(NextState);\r
179 }\r