X-Git-Url: http://git.kpe.io/?p=avr_bc100.git;a=blobdiff_plain;f=BaseTinyFirmware%2FGCC%2Fchargefunc.c;fp=BaseTinyFirmware%2FGCC%2Fchargefunc.c;h=b3db020480f6a5991925d8bb41f2da57de7fd50e;hp=5f441b7f85dbc45970108c8fea68747f367c07ff;hb=65612c4c7df34cdae10f9427ace6fd9e9e430d05;hpb=89173ec9cd6f33843bf27c45be0f2f9be3f5f0ce diff --git a/BaseTinyFirmware/GCC/chargefunc.c b/BaseTinyFirmware/GCC/chargefunc.c index 5f441b7..b3db020 100644 --- a/BaseTinyFirmware/GCC/chargefunc.c +++ b/BaseTinyFirmware/GCC/chargefunc.c @@ -12,14 +12,14 @@ * AVR463: Charging NiMH Batteries with BC100 * * \par Documentation - * For comprehensive code documentation, supported compilers, compiler + * 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: 2299 $ * $RCSfile$ @@ -84,7 +84,7 @@ unsigned char ConstantCurrent(void) wasStopped = FALSE; unsigned char sreg_saved; signed int adcs_avgIBAT_tmp; - + do { // Wait for ADC conversions to complete. ADC_Wait(); @@ -99,7 +99,7 @@ unsigned char ConstantCurrent(void) // Continue charging! if (wasStopped) { wasStopped = FALSE; - + // Timer variables are not reset by this. Time_Start(); } @@ -113,7 +113,7 @@ unsigned char ConstantCurrent(void) // +/- BAT_CURRENT_HYST. if ((adcs_avgIBAT_tmp < 0) || (adcs_avgIBAT_tmp < (ChargeParameters.Current - BAT_CURRENT_HYST))) { - + if(!PWM_IncrementDutyCycle()) { #ifdef ABORT_IF_PWM_MAX // If the duty cycle cannot be incremented, flag error and @@ -125,7 +125,7 @@ unsigned char ConstantCurrent(void) } } else if ((adcs_avgIBAT_tmp >= 0) && (adcs_avgIBAT_tmp > (ChargeParameters.Current + BAT_CURRENT_HYST))) { - + if(!PWM_DecrementDutyCycle()) { #ifdef ABORT_IF_PWM_MIN // If the duty cycle cannot be decremented, flag error and @@ -165,12 +165,12 @@ unsigned char ConstantVoltage(void) wasStopped = FALSE; unsigned char sreg_saved; unsigned int adcs_VBAT_tmp; - + do{ - + // Wait for ADC conversions to complete. ADC_Wait(); - + // If Master has flagged for a charge inhibit, pause charging. // (This is to prevent damage during prolonged serial communication.) if (eeprom_read_byte(&BattControl[BattActive]) & BIT_BATTERY_CHARGE_INHIBIT) { @@ -178,16 +178,16 @@ unsigned char ConstantVoltage(void) Time_Stop(); OCR1B = 0; } - + else { // Continue charging! if (wasStopped) { wasStopped = FALSE; - + // Timer variables aren't reset by this. Time_Start(); } - + sreg_saved = SREG; cli(); adcs_VBAT_tmp = ADCS.VBAT; @@ -237,7 +237,7 @@ unsigned char ConstantVoltage(void) * * The function also checks if the battery temperature is within limits, * if mains is OK, and if BatteryCheck() returns TRUE. - * If an error is detected, the associated errorflag is set and + * If an error is detected, the associated errorflag is set and * ChargeParameters.NextState is changed to an appropriate state. * * \retval TRUE Halt now. @@ -256,7 +256,7 @@ unsigned char HaltNow(void) unsigned int adcs_rawNTC_tmp, adcs_VBAT_tmp; signed int adcs_avgIBAT_tmp; unsigned char sreg_saved; - + // Wait for a full ADC-cycle to finish. ADC_Wait(); @@ -265,29 +265,29 @@ unsigned char HaltNow(void) // (Gets overridden if either mains is failing, or the battery changes.) for (i = 0x01; i != 0; i <<= 1) { if (HaltParameters.HaltFlags & i) { - + sreg_saved = SREG; cli(); adcs_VBAT_tmp = ADCS.VBAT; adcs_avgIBAT_tmp = ADCS.avgIBAT; SREG = sreg_saved; - + switch (i) { // Is VBAT less than the recorded maximum? case HALT_VOLTAGE_DROP: // Update VBATMax if VBAT is higher. Evaluate for halt otherwise. if (adcs_VBAT_tmp > HaltParameters.VBATMax) { HaltParameters.VBATMax = adcs_VBAT_tmp; - } else if((HaltParameters.VBATMax - adcs_VBAT_tmp) >= + } else if((HaltParameters.VBATMax - adcs_VBAT_tmp) >= HaltParameters.VoltageDrop) { halt = TRUE; } break; - + // Has VBAT reached the maximum limit? - case HALT_VOLTAGE_MAX: - + case HALT_VOLTAGE_MAX: + if (adcs_VBAT_tmp >= HaltParameters.VoltageMax) { halt = TRUE; } @@ -296,16 +296,16 @@ unsigned char HaltNow(void) // Has IBAT reached the minimum limit? case HALT_CURRENT_MIN: - + if (adcs_avgIBAT_tmp <= HaltParameters.CurrentMin) { halt = TRUE; } break; - - + + // Is the temperature rising too fast? case HALT_TEMPERATURE_RISE: - + sreg_saved = SREG; cli(); adcs_rawNTC_tmp = ADCS.rawNTC; @@ -316,12 +316,12 @@ unsigned char HaltNow(void) if (adcs_rawNTC_tmp > HaltParameters.LastNTC) { HaltParameters.LastNTC = adcs_rawNTC_tmp; Time_Set(TIMER_TEMP,0,30,0); - + // Is the increase in temperature greater than the set threshold? } else if ((HaltParameters.LastNTC - adcs_rawNTC_tmp) >= (BattData.ADCSteps * HaltParameters.TemperatureRise)) { - - // If this happened within a timeframe of 30 seconds, the + + // If this happened within a timeframe of 30 seconds, the // temperature is rising faster than we want. // If not, update LastNTC and reset timer. if (Time_Left(TIMER_TEMP)) { @@ -332,29 +332,31 @@ unsigned char HaltNow(void) } } break; - - + + // Is there any time left? - case HALT_TIME: - + case HALT_TIME: + if (!Time_Left(TIMER_CHG)) { halt = TRUE; - - // If exhaustion flagging is selected, stop the PWM, disable the + + // If exhaustion flagging is selected, stop the PWM, disable the // battery and flag it as exhausted. Make ST_ERROR next state. if (HaltParameters.HaltFlags & HALT_FLAG_EXHAUSTION) { PWM_Stop(); + Battery_t tmp = eeprom_read_byte(&BattControl[BattActive]); tmp &= ~BIT_BATTERY_ENABLED; // Enabled = FALSE; eeprom_write_byte(&BattControl[BattActive], tmp); + BattData.Exhausted = TRUE; SetErrorFlag(ERR_BATTERY_EXHAUSTED); ChargeParameters.NextState = ST_ERROR; } } break; - - + + default: // Shouldn't end up here, but is needed for MISRA compliance. break; } @@ -366,7 +368,7 @@ unsigned char HaltNow(void) // Battery too cold or hot? if ((BattData.Temperature <= HaltParameters.TemperatureMin) || (BattData.Temperature >= HaltParameters.TemperatureMax)) { - + PWM_Stop(); SetErrorFlag(ERR_BATTERY_TEMPERATURE); ChargeParameters.NextState = ST_ERROR;