* AVR463: Charging NiMH Batteries with BC100\r
*\r
* \par Documentation\r
- * For comprehensive code documentation, supported compilers, compiler \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
+ *\r
* $Name$\r
* $Revision: 2299 $\r
* $RCSfile$\r
******************************************************************************/\r
\r
#include <avr/io.h>\r
+#include <avr/interrupt.h>\r
\r
#include "enums.h"\r
#include "structs.h"\r
{\r
unsigned char error = FALSE,\r
wasStopped = FALSE;\r
- \r
+ unsigned char sreg_saved;\r
+ signed int adcs_avgIBAT_tmp;\r
+\r
do {\r
- // Wait for ADC conversions to complete.\r
+ // Wait for ADC conversions to complete.\r
ADC_Wait();\r
\r
// If Master has flagged for a charge inhibit, pause charging.\r
// Continue charging!\r
if (wasStopped) {\r
wasStopped = FALSE;\r
- \r
+\r
// Timer variables are not reset by this.\r
Time_Start();\r
}\r
- \r
+\r
+ sreg_saved = SREG;\r
+ cli();\r
+ adcs_avgIBAT_tmp = ADCS.avgIBAT;\r
+ SREG = sreg_saved;\r
+\r
// Adjust the charge current to within ChargeParameters.Current\r
// +/- BAT_CURRENT_HYST.\r
- if ((ADCS.avgIBAT < 0) ||\r
- (ADCS.avgIBAT < (ChargeParameters.Current - BAT_CURRENT_HYST))) {\r
- \r
+ if ((adcs_avgIBAT_tmp < 0) ||\r
+ (adcs_avgIBAT_tmp < (ChargeParameters.Current - BAT_CURRENT_HYST))) {\r
+\r
if(!PWM_IncrementDutyCycle()) {\r
#ifdef ABORT_IF_PWM_MAX\r
// If the duty cycle cannot be incremented, flag error and\r
error = TRUE;\r
#endif\r
}\r
- } else if ((ADCS.avgIBAT >= 0) &&\r
- (ADCS.avgIBAT > (ChargeParameters.Current + BAT_CURRENT_HYST))) {\r
- \r
+ } else if ((adcs_avgIBAT_tmp >= 0) &&\r
+ (adcs_avgIBAT_tmp > (ChargeParameters.Current + BAT_CURRENT_HYST))) {\r
+\r
if(!PWM_DecrementDutyCycle()) {\r
#ifdef ABORT_IF_PWM_MIN\r
// If the duty cycle cannot be decremented, flag error and\r
{\r
unsigned char error = FALSE,\r
wasStopped = FALSE;\r
- \r
+ unsigned char sreg_saved;\r
+ unsigned int adcs_VBAT_tmp;\r
+\r
do{\r
- \r
+\r
// Wait for ADC conversions to complete.\r
ADC_Wait();\r
- \r
+\r
// If Master has flagged for a charge inhibit, pause charging.\r
// (This is to prevent damage during prolonged serial communication.)\r
if (eeprom_read_byte(&BattControl[BattActive]) & BIT_BATTERY_CHARGE_INHIBIT) {\r
Time_Stop();\r
OCR1B = 0;\r
}\r
- \r
+\r
else {\r
// Continue charging!\r
if (wasStopped) {\r
wasStopped = FALSE;\r
- \r
+\r
// Timer variables aren't reset by this.\r
Time_Start();\r
}\r
- \r
+\r
+ sreg_saved = SREG;\r
+ cli();\r
+ adcs_VBAT_tmp = ADCS.VBAT;\r
+ SREG = sreg_saved;\r
+\r
// Adjust the charge voltage to within ChargeParameters.Voltage\r
// +/- BAT_VOLTAGE_HYST.\r
- if (ADCS.VBAT < (ChargeParameters.Voltage - BAT_VOLTAGE_HYST)) {\r
+ if (adcs_VBAT_tmp < (ChargeParameters.Voltage - BAT_VOLTAGE_HYST)) {\r
\r
if(!PWM_IncrementDutyCycle()) {\r
#ifdef ABORT_IF_PWM_MAX\r
error = TRUE;\r
#endif\r
}\r
- } else if (ADCS.VBAT > (ChargeParameters.Voltage + BAT_VOLTAGE_HYST)) {\r
+ } else if (adcs_VBAT_tmp > (ChargeParameters.Voltage + BAT_VOLTAGE_HYST)) {\r
\r
if(!PWM_DecrementDutyCycle()) {\r
#ifdef ABORT_IF_PWM_MIN\r
*\r
* The function also checks if the battery temperature is within limits,\r
* if mains is OK, and if BatteryCheck() returns TRUE.\r
- * If an error is detected, the associated errorflag is set and \r
+ * If an error is detected, the associated errorflag is set and\r
* ChargeParameters.NextState is changed to an appropriate state.\r
*\r
* \retval TRUE Halt now.\r
unsigned char HaltNow(void)\r
{\r
unsigned char i, halt = FALSE;\r
- \r
+ unsigned int adcs_rawNTC_tmp, adcs_VBAT_tmp;\r
+ signed int adcs_avgIBAT_tmp;\r
+ unsigned char sreg_saved;\r
+\r
// Wait for a full ADC-cycle to finish.\r
ADC_Wait();\r
\r
// (Gets overridden if either mains is failing, or the battery changes.)\r
for (i = 0x01; i != 0; i <<= 1) {\r
if (HaltParameters.HaltFlags & i) {\r
+\r
+ sreg_saved = SREG;\r
+ cli();\r
+ adcs_VBAT_tmp = ADCS.VBAT;\r
+ adcs_avgIBAT_tmp = ADCS.avgIBAT;\r
+ SREG = sreg_saved;\r
+\r
switch (i) {\r
// Is VBAT less than the recorded maximum?\r
case HALT_VOLTAGE_DROP:\r
-\r
// Update VBATMax if VBAT is higher. Evaluate for halt otherwise.\r
- if (ADCS.VBAT > HaltParameters.VBATMax) {\r
- HaltParameters.VBATMax = ADCS.VBAT;\r
- } else if((HaltParameters.VBATMax - ADCS.VBAT) >= \r
+ if (adcs_VBAT_tmp > HaltParameters.VBATMax) {\r
+ HaltParameters.VBATMax = adcs_VBAT_tmp;\r
+ } else if((HaltParameters.VBATMax - adcs_VBAT_tmp) >=\r
HaltParameters.VoltageDrop) {\r
halt = TRUE;\r
}\r
break;\r
\r
- \r
+\r
// Has VBAT reached the maximum limit?\r
- case HALT_VOLTAGE_MAX: \r
- \r
- if (ADCS.VBAT >= HaltParameters.VoltageMax) {\r
+ case HALT_VOLTAGE_MAX:\r
+\r
+ if (adcs_VBAT_tmp >= HaltParameters.VoltageMax) {\r
halt = TRUE;\r
}\r
break;\r
\r
// Has IBAT reached the minimum limit?\r
case HALT_CURRENT_MIN:\r
- \r
- if (ADCS.avgIBAT <= HaltParameters.CurrentMin) {\r
+\r
+ if (adcs_avgIBAT_tmp <= HaltParameters.CurrentMin) {\r
halt = TRUE;\r
}\r
break;\r
- \r
- \r
+\r
+\r
// Is the temperature rising too fast?\r
case HALT_TEMPERATURE_RISE:\r
\r
+ sreg_saved = SREG;\r
+ cli();\r
+ adcs_rawNTC_tmp = ADCS.rawNTC;\r
+ SREG = sreg_saved;\r
// If rawNTC has increased, the temperature has dropped.\r
// We can store this value for now, and start the timer.\r
// Otherwise, check if NTC has changed too fast.\r
- if (ADCS.rawNTC > HaltParameters.LastNTC) {\r
- HaltParameters.LastNTC = ADCS.rawNTC;\r
+ if (adcs_rawNTC_tmp > HaltParameters.LastNTC) {\r
+ HaltParameters.LastNTC = adcs_rawNTC_tmp;\r
Time_Set(TIMER_TEMP,0,30,0);\r
- \r
+\r
// Is the increase in temperature greater than the set threshold?\r
- } else if ((HaltParameters.LastNTC - ADCS.rawNTC) >=\r
+ } else if ((HaltParameters.LastNTC - adcs_rawNTC_tmp) >=\r
(BattData.ADCSteps * HaltParameters.TemperatureRise)) {\r
- \r
- // If this happened within a timeframe of 30 seconds, the \r
+\r
+ // If this happened within a timeframe of 30 seconds, the\r
// temperature is rising faster than we want.\r
// If not, update LastNTC and reset timer.\r
if (Time_Left(TIMER_TEMP)) {\r
halt = TRUE;\r
} else {\r
- HaltParameters.LastNTC = ADCS.rawNTC;\r
+ HaltParameters.LastNTC = adcs_rawNTC_tmp;\r
Time_Set(TIMER_TEMP,0,30,0);\r
}\r
}\r
break;\r
- \r
- \r
+\r
+\r
// Is there any time left?\r
- case HALT_TIME: \r
- \r
+ case HALT_TIME:\r
+\r
if (!Time_Left(TIMER_CHG)) {\r
halt = TRUE;\r
- \r
- // If exhaustion flagging is selected, stop the PWM, disable the \r
+\r
+ // If exhaustion flagging is selected, stop the PWM, disable the\r
// battery and flag it as exhausted. Make ST_ERROR next state.\r
if (HaltParameters.HaltFlags & HALT_FLAG_EXHAUSTION) {\r
PWM_Stop();\r
+\r
Battery_t tmp = eeprom_read_byte(&BattControl[BattActive]);\r
- tmp &= ~BIT_BATTERY_ENABLED; // Enabled = FALSE;\r
- eeprom_write_byte(&BattControl[BattActive], tmp);\r
+ tmp &= ~BIT_BATTERY_ENABLED; // Enabled = FALSE;\r
+ eeprom_write_byte(&BattControl[BattActive], tmp);\r
+\r
BattData.Exhausted = TRUE;\r
SetErrorFlag(ERR_BATTERY_EXHAUSTED);\r
ChargeParameters.NextState = ST_ERROR;\r
}\r
}\r
break;\r
- \r
- \r
+\r
+\r
default: // Shouldn't end up here, but is needed for MISRA compliance.\r
break;\r
}\r
// Battery too cold or hot?\r
if ((BattData.Temperature <= HaltParameters.TemperatureMin) ||\r
(BattData.Temperature >= HaltParameters.TemperatureMax)) {\r
- \r
+\r
PWM_Stop();\r
SetErrorFlag(ERR_BATTERY_TEMPERATURE);\r
ChargeParameters.NextState = ST_ERROR;\r