X-Git-Url: http://git.kpe.io/?p=avr_bc100.git;a=blobdiff_plain;f=BaseTinyFirmware%2FGCC%2FNIMHcharge.c;fp=BaseTinyFirmware%2FGCC%2FNIMHcharge.c;h=1a436cd2e138008c1f06d565309ee88c4b5b4179;hp=0000000000000000000000000000000000000000;hb=0baf0366ad1924563e53f3d839d69730c7b14a44;hpb=70667e37128288b141e5def02dfb0e47a5e60034 diff --git a/BaseTinyFirmware/GCC/NIMHcharge.c b/BaseTinyFirmware/GCC/NIMHcharge.c new file mode 100644 index 0000000..1a436cd --- /dev/null +++ b/BaseTinyFirmware/GCC/NIMHcharge.c @@ -0,0 +1,179 @@ +/* This file has been prepared for Doxygen automatic documentation generation.*/ +/*! \file ********************************************************************* + * + * \brief + * Charge state function for NiMH batteries + * + * Contains the charge state function, in which the NiMH charging + * algorithm is, plus the associated functions. + * + * \par Application note: + * AVR463: Charging NiMH Batteries with BC100 \n + * + * \par Documentation + * For comprehensive code documentation, supported compilers, compiler + * settings and supported devices see readme.html + * + * \author + * Atmel Corporation: http://www.atmel.com \n + * Support email: avr@atmel.com + * + * + * $Name$ + * $Revision: 2255 $ + * $RCSfile$ + * $URL: http://svn.norway.atmel.com/AppsAVR8/avr463_Charging_NiMH_Batteries_with_BC100/trunk/code/IAR/NIMHcharge.c $ + * $Date: 2007-08-09 14:47:58 +0200 (to, 09 aug 2007) $\n + ******************************************************************************/ + +#include + +#include "enums.h" +#include "structs.h" + +#include "battery.h" +#include "charge.h" +#include "chargefunc.h" +#include "main.h" +#include "menu.h" +#include "NIMHspecs.h" +#include "PWM.h" +#include "time.h" + +#ifndef NIMH +#error NIMH not defined in main.h! +#endif // NIMH + + +//****************************************************************************** +// Functions +//****************************************************************************** +/*! \brief Controls the charging. + * + * This function contains the charging algorithm itself, divided into stages.\n + * For each stage the PWM may be started/stopped, and the timer, + * halt-requirements and charge parameters may be set.\n + * The charging functions return whatever state is next, and as long as no + * errors occur this is the next charging stage. + * + * \note If more stages are needed simply define more states in menu.h, include + * them in \ref menu_state[] in menu.c, then add the cases to this function. + * + * \note This algorithm is for NiMH batteries. + */ +unsigned char Charge(unsigned char inp) +{ + unsigned char NextState; + + switch (CurrentState) { + // First stage is a prequalification. Attempt to charge battery to 1 V, + // using a 0.1 C current, within 2 minutes. + // If this fails, the battery is likely damaged. + // If it succeeds, start a fast charge. + case ST_PREQUAL: + + // Set up charge current and next state. + ChargeParameters.Current = BattData.Capacity / 10; + ChargeParameters.NextState = ST_FASTCHARGE; + + // Halt charge on voltage limit or timeout. + // Timeout means battery exhaustion. + HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME | + HALT_FLAG_EXHAUSTION); + + // Set up voltage limit and temperature limits. + HaltParameters.VoltageMax = BAT_VOLTAGE_PREQUAL; + HaltParameters.TemperatureMin = BAT_TEMPERATURE_MIN; + HaltParameters.TemperatureMax = 35; + + // Reset temperature measurement for HaltNow(). + HaltParameters.LastNTC = 0; + + // Start PWM and charge timer before calling the charge function. + PWM_Start(); + Time_Set(TIMER_CHG, BAT_TIME_PREQUAL, 0, 0); + + // Call charge function, get next state. + NextState = ConstantCurrent(); + break; + + + // Second stage is a fast charge. Charge at 1.0 C for at most 1.5 hours, + // until either rate of temperature increase or voltage reaches limit, or + // the voltage drops sufficiently. + // Timeout doesn't mean battery exhaustion now. + case ST_FASTCHARGE: + + // Set up charge current and next state. + ChargeParameters.Current = BattData.Capacity; + ChargeParameters.NextState = ST_LOWRATECHARGE; + + // Halt charge on voltage limit, timeout, voltage drop or rate of + // temperature increase. + HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME | + HALT_VOLTAGE_DROP | HALT_TEMPERATURE_RISE); + + // Set up limits for voltage, voltage drop, temperature and rate of + // temperature increase (1 degree C per minute). + HaltParameters.VoltageMax = BAT_VOLTAGE_MAX; + HaltParameters.VoltageDrop = BAT_VOLTAGE_DROP; + HaltParameters.TemperatureMax = BAT_TEMPERATURE_MAX; + HaltParameters.TemperatureRise = 1; + + // Reset maximum voltage measurement for HaltNow(). + HaltParameters.VBATMax = 0; + + // Start timer, PWM should still be running. + Time_Set(TIMER_CHG, BattData.MaxTime, 0, 0); + + // Call charge function, get next state. + NextState = ConstantCurrent(); + break; + + + // Last stage is a trickle charge. Charge at 0.1 C for at most 30 minutes, + // until either rate of temperature increase or voltage reaches limit. + case ST_LOWRATECHARGE: + + // Set up charge current and next state. + ChargeParameters.Current = BattData.Capacity / 10; + ChargeParameters.NextState = ST_ENDCHARGE; + + // Halt charge on voltage limit, timeout or temperature rise. + // Use the same requirements as during the last stage (ST_FASTCHARGE). + HaltParameters.HaltFlags = (HALT_VOLTAGE_MAX | HALT_TIME | + HALT_TEMPERATURE_RISE); + + // Start timer, 30 minutes. + Time_Set(TIMER_CHG, 30, 0, 0); + + // Call charge function, get next state. + NextState = ConstantCurrent(); + break; + + + // Charging is done! + case ST_ENDCHARGE: + + // Stop the PWM output and flag battery as charged. + PWM_Stop(); + BattData.Charged = TRUE; + + // If the other battery is enabled go to ST_BATCON, otherwise + // go to ST_SLEEP. + if (eeprom_read_byte(&BattControl[(BattActive+1)%2]) & BIT_BATTERY_ENABLED) { + NextState = ST_BATCON; + } else { + NextState = ST_SLEEP; + } + break; + + + default: // Shouldn't end up here. Reinitialize for safety. + NextState = ST_INIT; + break; + } + + // Return the next state to main(). + return(NextState); +}