X-Git-Url: http://git.kpe.io/?p=avr_bc100.git;a=blobdiff_plain;f=BaseTinyFirmware%2FGCC%2Fbattery.c;fp=BaseTinyFirmware%2FGCC%2Fbattery.c;h=b3617118cd562fd4f29df63ad09e3ec1bcfb9861;hp=e6cacd863849592bcb9794a7ab9f5b0def018a8f;hb=edd0e551e6041f7596c880cdaef13dfa67eff6b5;hpb=5b95e754a4af80c7389486ee874ac07c166a0867 diff --git a/BaseTinyFirmware/GCC/battery.c b/BaseTinyFirmware/GCC/battery.c index e6cacd8..b361711 100644 --- a/BaseTinyFirmware/GCC/battery.c +++ b/BaseTinyFirmware/GCC/battery.c @@ -30,6 +30,7 @@ #include #include +#include #include "structs.h" #include "enums.h" @@ -172,6 +173,9 @@ unsigned char BatteryStatusRefresh(void) { // Assume the worst.. unsigned char success = FALSE; + unsigned char sreg_saved; + unsigned int adcs_VBAT_tmp; + signed int adcs_avgIBAT_tmp; BattData.Present = FALSE; BattData.Charged = FALSE; @@ -186,13 +190,19 @@ unsigned char BatteryStatusRefresh(void) NTCLookUp(); BattData.HasRID = RIDLookUp(); + sreg_saved = SREG; + cli(); + adcs_VBAT_tmp = ADCS.VBAT; + adcs_avgIBAT_tmp = ADCS.avgIBAT; + SREG = sreg_saved; + // Is the battery voltage above minimum safe cell voltage? - if (ADCS.VBAT >= BAT_VOLTAGE_MIN) { + if (adcs_VBAT_tmp >= BAT_VOLTAGE_MIN) { BattData.Low = FALSE; } // Is the battery charged? - if (ADCS.VBAT >= BAT_VOLTAGE_LOW) { + if (adcs_VBAT_tmp >= BAT_VOLTAGE_LOW) { BattData.Charged = TRUE; } @@ -205,7 +215,7 @@ unsigned char BatteryStatusRefresh(void) * not 100% proof. */ if (((OCR1B == 0) && (!BattData.Low)) || - ((OCR1B != 0) && (ADCS.avgIBAT > 0))) { + ((OCR1B != 0) && (adcs_avgIBAT_tmp > 0))) { BattData.Present = TRUE; success = TRUE; } else { @@ -299,7 +309,7 @@ unsigned char BatteryDataRefresh(void) // Erase local EEPROM page if there were any errors during transfer. if (!success) { for (i=0; i<32; i++) { - eeprom_write_byte(&BattEEPROM[page][i], 0); + eeprom_write_byte(&BattEEPROM[page][i], 0); } } } @@ -358,12 +368,18 @@ void DisableBatteries(void) unsigned char RIDLookUp (void) { unsigned char i, found = FALSE; + unsigned int adcs_rawRID_tmp; + unsigned char sreg_saved; // Lookup in the RID-table. If measured RID is within the limits // of an entry, those data are used, and TRUE is returned. for (i = 0 ; i < RID_TABLE_SIZE; i++) { - if (ADCS.rawRID >= RID[i].Low) { - if (ADCS.rawRID <= RID[i].High) { + sreg_saved = SREG; + cli(); + adcs_rawRID_tmp = ADCS.rawRID; + SREG = sreg_saved; + if (adcs_rawRID_tmp >= RID[i].Low) { + if (adcs_rawRID_tmp <= RID[i].High) { BattData.Capacity = RID[i].Capacity; BattData.MaxCurrent = RID[i].Icharge; BattData.MaxTime = RID[i].tCutOff; @@ -401,15 +417,21 @@ void NTCLookUp (void) { unsigned char i; unsigned char found = FALSE; + unsigned char adcs_rawNTC_tmp; + unsigned char sreg_saved; // Lookup in the NTC-table. Use the first entry which is equal or below // sampled NTC. Calculate temperature by using the index number, and the // difference between the measured NTC value and the one in the entry. for (i=0 ; (i < NTC_TABLE_SIZE) && (!found); i++) { - if (ADCS.rawNTC >= NTC[i].ADCV) { + sreg_saved = SREG; + cli(); + adcs_rawNTC_tmp = ADCS.rawNTC; + SREG = sreg_saved; + if (adcs_rawNTC_tmp >= NTC[i].ADCV) { BattData.Temperature = (i<<2) ; BattData.ADCSteps = NTC[i].ADCsteps; - BattData.Temperature -= ((ADCS.rawNTC - NTC[i].ADCV)<<1) / BattData.ADCSteps; + BattData.Temperature -= ((adcs_rawNTC_tmp - NTC[i].ADCV)<<1) / BattData.ADCSteps; found = TRUE; // Could be done with a break, but that violates MISRA. }