1 ///////////////////////////////////////////////////////////////////////////////
\r
3 // IAR Atmel AVR C/C++ Compiler V4.30F/W32 12/Mar/2008 23:01:38 /
\r
4 // Copyright 1996-2007 IAR Systems. All rights reserved. /
\r
6 // Source file = C:\home\kevin\pub\src\bc100_cal\IAR\NIMHcharge.c /
\r
7 // Command line = C:\home\kevin\pub\src\bc100_cal\IAR\NIMHcharge.c /
\r
8 // --cpu=tiny861 -ms -o C:\home\kevin\pub\src\bc100_cal\IA /
\r
9 // R\Debug\Obj\ -lC C:\home\kevin\pub\src\bc100_cal\IAR\De /
\r
10 // bug\List\ -lB C:\home\kevin\pub\src\bc100_cal\IAR\Debug /
\r
11 // \List\ --initializers_in_flash -z2 --no_cse /
\r
12 // --no_inline --no_code_motion --no_cross_call /
\r
13 // --no_clustering --no_tbaa --debug /
\r
14 // -DENABLE_BIT_DEFINITIONS -e --require_prototypes -I /
\r
15 // "C:\Program Files\IAR Systems\Embedded Workbench /
\r
16 // 4.0\avr\INC\" -I "C:\Program Files\IAR /
\r
17 // Systems\Embedded Workbench 4.0\avr\INC\CLIB\" /
\r
18 // --eeprom_size 512 /
\r
19 // List file = C:\home\kevin\pub\src\bc100_cal\IAR\Debug\List\NIMHchar /
\r
23 ///////////////////////////////////////////////////////////////////////////////
\r
27 RSEG CSTACK:DATA:NOROOT(0)
\r
28 RSEG RSTACK:DATA:NOROOT(0)
\r
30 EXTERN ?EPILOGUE_B2_L09
\r
31 EXTERN ?PROLOGUE2_L09
\r
32 EXTERN ?SS_DIVMOD_L02
\r
33 EXTERN ?US_DIVMOD_L02
\r
44 EXTERN ConstantCurrent
\r
49 EXTERN ChargeParameters
\r
51 EXTERN HaltParameters
\r
53 // C:\home\kevin\pub\src\bc100_cal\IAR\NIMHcharge.c
\r
54 // 1 /* This file has been prepared for Doxygen automatic documentation generation.*/
\r
55 // 2 /*! \file *********************************************************************
\r
58 // 5 * Charge state function for NiMH batteries
\r
60 // 7 * Contains the charge state function, in which the NiMH charging
\r
61 // 8 * algorithm is, plus the associated functions.
\r
63 // 10 * \par Application note:
\r
64 // 11 * AVR463: Charging NiMH Batteries with BC100 \n
\r
66 // 13 * \par Documentation
\r
67 // 14 * For comprehensive code documentation, supported compilers, compiler
\r
68 // 15 * settings and supported devices see readme.html
\r
71 // 18 * Atmel Corporation: http://www.atmel.com \n
\r
72 // 19 * Support email: avr@atmel.com
\r
76 // 23 * $Revision: 2255 $
\r
78 // 25 * $URL: http://svn.norway.atmel.com/AppsAVR8/avr463_Charging_NiMH_Batteries_with_BC100/trunk/code/IAR/NIMHcharge.c $
\r
79 // 26 * $Date: 2007-08-09 14:47:58 +0200 (to, 09 aug 2007) $\n
\r
80 // 27 ******************************************************************************/
\r
82 // 29 #include <ioavr.h>
\r
84 // 31 #include "enums.h"
\r
85 // 32 #include "structs.h"
\r
87 // 34 #include "battery.h"
\r
88 // 35 #include "charge.h"
\r
89 // 36 #include "chargefunc.h"
\r
90 // 37 #include "main.h"
\r
91 // 38 #include "menu.h"
\r
92 // 39 #include "NIMHspecs.h"
\r
93 // 40 #include "PWM.h"
\r
94 // 41 #include "time.h"
\r
97 // 44 #error NIMH not defined in main.h!
\r
98 // 45 #endif // NIMH
\r
101 // 48 //******************************************************************************
\r
103 // 50 //******************************************************************************
\r
104 // 51 /*! \brief Controls the charging.
\r
106 // 53 * This function contains the charging algorithm itself, divided into stages.\n
\r
107 // 54 * For each stage the PWM may be started/stopped, and the timer,
\r
108 // 55 * halt-requirements and charge parameters may be set.\n
\r
109 // 56 * The charging functions return whatever state is next, and as long as no
\r
110 // 57 * errors occur this is the next charging stage.
\r
112 // 59 * \note If more stages are needed simply define more states in menu.h, include
\r
113 // 60 * them in \ref menu_state[] in menu.c, then add the cases to this function.
\r
115 // 62 * \note This algorithm is for NiMH batteries.
\r
118 RSEG CODE:CODE:NOROOT(1)
\r
119 // 64 unsigned char Charge(unsigned char inp)
\r
122 RCALL ?PROLOGUE2_L09
\r
124 // 66 unsigned char NextState;
\r
126 // 68 switch (CurrentState) {
\r
127 LDS R16, CurrentState
\r
139 // 69 // First stage is a prequalification. Attempt to charge battery to 1 V,
\r
140 // 70 // using a 0.1 C current, within 2 minutes.
\r
141 // 71 // If this fails, the battery is likely damaged.
\r
142 // 72 // If it succeeds, start a fast charge.
\r
143 // 73 case ST_PREQUAL:
\r
145 // 75 // Set up charge current and next state.
\r
146 // 76 ChargeParameters.Current = BattData.Capacity / 10;
\r
148 LDI R30, LOW(BattData)
\r
149 LDI R31, (BattData) >> 8
\r
154 RCALL ?US_DIVMOD_L02
\r
155 LDI R30, LOW(ChargeParameters)
\r
156 LDI R31, (ChargeParameters) >> 8
\r
159 // 77 ChargeParameters.NextState = ST_FASTCHARGE;
\r
161 STS (ChargeParameters + 4), R16
\r
163 // 79 // Halt charge on voltage limit or timeout.
\r
164 // 80 // Timeout means battery exhaustion.
\r
165 // 81 HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME |
\r
166 // 82 HALT_FLAG_EXHAUSTION);
\r
168 STS HaltParameters, R16
\r
170 // 84 // Set up voltage limit and temperature limits.
\r
171 // 85 HaltParameters.VoltageMax = BAT_VOLTAGE_PREQUAL;
\r
174 LDI R30, LOW(HaltParameters)
\r
175 LDI R31, (HaltParameters) >> 8
\r
178 // 86 HaltParameters.TemperatureMin = BAT_TEMPERATURE_MIN;
\r
181 LDI R30, LOW(HaltParameters)
\r
182 LDI R31, (HaltParameters) >> 8
\r
185 // 87 HaltParameters.TemperatureMax = 35;
\r
188 LDI R30, LOW(HaltParameters)
\r
189 LDI R31, (HaltParameters) >> 8
\r
193 // 89 // Reset temperature measurement for HaltNow().
\r
194 // 90 HaltParameters.LastNTC = 0;
\r
197 LDI R30, LOW(HaltParameters)
\r
198 LDI R31, (HaltParameters) >> 8
\r
202 // 92 // Start PWM and charge timer before calling the charge function.
\r
205 // 94 Time_Set(TIMER_CHG, BAT_TIME_PREQUAL, 0, 0);
\r
213 // 96 // Call charge function, get next state.
\r
214 // 97 NextState = ConstantCurrent();
\r
215 RCALL ConstantCurrent
\r
221 // 101 // Second stage is a fast charge. Charge at 1.0 C for at most 1.5 hours,
\r
222 // 102 // until either rate of temperature increase or voltage reaches limit, or
\r
223 // 103 // the voltage drops sufficiently.
\r
224 // 104 // Timeout doesn't mean battery exhaustion now.
\r
225 // 105 case ST_FASTCHARGE:
\r
227 // 107 // Set up charge current and next state.
\r
228 // 108 ChargeParameters.Current = BattData.Capacity;
\r
230 LDI R30, LOW(BattData)
\r
231 LDI R31, (BattData) >> 8
\r
234 LDI R30, LOW(ChargeParameters)
\r
235 LDI R31, (ChargeParameters) >> 8
\r
238 // 109 ChargeParameters.NextState = ST_LOWRATECHARGE;
\r
240 STS (ChargeParameters + 4), R16
\r
242 // 111 // Halt charge on voltage limit, timeout, voltage drop or rate of
\r
243 // 112 // temperature increase.
\r
244 // 113 HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME |
\r
245 // 114 HALT_VOLTAGE_DROP | HALT_TEMPERATURE_RISE);
\r
247 STS HaltParameters, R16
\r
249 // 116 // Set up limits for voltage, voltage drop, temperature and rate of
\r
250 // 117 // temperature increase (1 degree C per minute).
\r
251 // 118 HaltParameters.VoltageMax = BAT_VOLTAGE_MAX;
\r
254 LDI R30, LOW(HaltParameters)
\r
255 LDI R31, (HaltParameters) >> 8
\r
258 // 119 HaltParameters.VoltageDrop = BAT_VOLTAGE_DROP;
\r
261 LDI R30, LOW(HaltParameters)
\r
262 LDI R31, (HaltParameters) >> 8
\r
265 // 120 HaltParameters.TemperatureMax = BAT_TEMPERATURE_MAX;
\r
268 LDI R30, LOW(HaltParameters)
\r
269 LDI R31, (HaltParameters) >> 8
\r
272 // 121 HaltParameters.TemperatureRise = 1;
\r
275 LDI R30, LOW(HaltParameters)
\r
276 LDI R31, (HaltParameters) >> 8
\r
280 // 123 // Reset maximum voltage measurement for HaltNow().
\r
281 // 124 HaltParameters.VBATMax = 0;
\r
284 LDI R30, LOW(HaltParameters)
\r
285 LDI R31, (HaltParameters) >> 8
\r
289 // 126 // Start timer, PWM should still be running.
\r
290 // 127 Time_Set(TIMER_CHG, BattData.MaxTime, 0, 0);
\r
293 LDI R30, LOW(BattData)
\r
294 LDI R31, (BattData) >> 8
\r
300 // 129 // Call charge function, get next state.
\r
301 // 130 NextState = ConstantCurrent();
\r
302 RCALL ConstantCurrent
\r
308 // 134 // Last stage is a trickle charge. Charge at 0.1 C for at most 30 minutes,
\r
309 // 135 // until either rate of temperature increase or voltage reaches limit.
\r
310 // 136 case ST_LOWRATECHARGE:
\r
312 // 138 // Set up charge current and next state.
\r
313 // 139 ChargeParameters.Current = BattData.Capacity / 10;
\r
315 LDI R30, LOW(BattData)
\r
316 LDI R31, (BattData) >> 8
\r
321 RCALL ?US_DIVMOD_L02
\r
322 LDI R30, LOW(ChargeParameters)
\r
323 LDI R31, (ChargeParameters) >> 8
\r
326 // 140 ChargeParameters.NextState = ST_ENDCHARGE;
\r
328 STS (ChargeParameters + 4), R16
\r
330 // 142 // Halt charge on voltage limit, timeout or temperature rise.
\r
331 // 143 // Use the same requirements as during the last stage (ST_FASTCHARGE).
\r
332 // 144 HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME |
\r
333 // 145 HALT_TEMPERATURE_RISE);
\r
335 STS HaltParameters, R16
\r
337 // 147 // Start timer, 30 minutes.
\r
338 // 148 Time_Set(TIMER_CHG, 30, 0, 0);
\r
346 // 150 // Call charge function, get next state.
\r
347 // 151 NextState = ConstantCurrent();
\r
348 RCALL ConstantCurrent
\r
354 // 155 // Charging is done!
\r
355 // 156 case ST_ENDCHARGE:
\r
357 // 158 // Stop the PWM output and flag battery as charged.
\r
361 // 160 BattData.Charged = TRUE;
\r
362 LDI R30, LOW(BattData)
\r
363 LDI R31, (BattData) >> 8
\r
368 // 162 // If the other battery is enabled go to ST_BATCON, otherwise
\r
369 // 163 // go to ST_SLEEP.
\r
370 // 164 if (BattControl[(BattActive+1)%2].Enabled) {
\r
371 LDS R16, BattActive
\r
377 RCALL ?SS_DIVMOD_L02
\r
378 LDI R18, LOW(BattControl)
\r
379 LDI R19, (BattControl) >> 8
\r
382 MOVW R21:R20, R19:R18
\r
387 // 165 NextState = ST_BATCON;
\r
391 // 167 NextState = ST_SLEEP;
\r
399 // 172 default: // Shouldn't end up here. Reinitialize for safety.
\r
400 // 173 NextState = ST_INIT;
\r
406 // 177 // Return the next state to main().
\r
407 // 178 return(NextState);
\r
411 RJMP ?EPILOGUE_B2_L09
\r
414 ASEGN ABSOLUTE:DATA:NOROOT,01cH
\r
417 ASEGN ABSOLUTE:DATA:NOROOT,01dH
\r
420 ASEGN ABSOLUTE:DATA:NOROOT,01eH
\r
423 ASEGN ABSOLUTE:DATA:NOROOT,01fH
\r
428 // 356 bytes in segment CODE
\r
430 // 356 bytes of CODE memory
\r