improved accuracy of battery voltage measurement

Slowing down the ADC 10x while reading the voltage gives better
results due to the very high impedance of the voltage devider.
Readings are now about 100mV higher than before.
This commit is contained in:
schneider 2012-01-16 15:50:48 +01:00
parent 7ec64c04ad
commit 29c7dfdf64
1 changed files with 18 additions and 8 deletions

View File

@ -3,21 +3,31 @@
#include "basic/basic.h" #include "basic/basic.h"
#include "funk/nrf24l01p.h" #include "funk/nrf24l01p.h"
static uint32_t results=5000; static uint32_t voltage=5000;
static uint8_t chrg=1; static uint8_t chrg=1;
void VoltageCheck(void){ void VoltageCheck(void){
chrg=gpioGetValue(RB_PWR_CHRG); chrg=gpioGetValue(RB_PWR_CHRG);
//slow down the adc for our high impedance volatage devider
ADC_AD0CR = ((CFG_CPU_CCLK / SCB_SYSAHBCLKDIV) / 100000 - 1 ) << 8;
voltage = adcRead(1);
//speed it up again
ADC_AD0CR = ((CFG_CPU_CCLK / SCB_SYSAHBCLKDIV) / 1000000 - 1 ) << 8;
results = adcRead(1); voltage *= 10560;
results *= 10560; voltage /= 1024;
results /= 1024;
results += 50; //add the drop over the voltage switch
if( results < 3500 ){ voltage += 50;
//battery is assumed empty if the volatge falls bellow 3.5V
if( voltage < 3500 ){
nrf_off(); nrf_off();
gpioSetValue (RB_PWR_GOOD, 0); gpioSetValue (RB_PWR_GOOD, 0);
gpioSetValue (RB_LCD_BL, 0); gpioSetValue (RB_LCD_BL, 0);
//put the chip into deep power down
SCB_SCR |= SCB_SCR_SLEEPDEEP; SCB_SCR |= SCB_SCR_SLEEPDEEP;
PMU_PMUCTRL = PMU_PMUCTRL_DPDEN_DEEPPOWERDOWN; PMU_PMUCTRL = PMU_PMUCTRL_DPDEN_DEEPPOWERDOWN;
__asm volatile ("WFI"); __asm volatile ("WFI");
@ -25,7 +35,7 @@ void VoltageCheck(void){
}; };
uint32_t GetVoltage(void){ uint32_t GetVoltage(void){
return results; return voltage;
}; };
uint8_t GetChrgStat(void){ uint8_t GetChrgStat(void){