From d9b7a2c6bd523fe1a93e075ca7d5ddc379af8fe8 Mon Sep 17 00:00:00 2001 From: schneider Date: Thu, 19 Jan 2012 23:01:00 +0100 Subject: [PATCH] added some filtering for the voltage reading --- firmware/basic/voltage.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/firmware/basic/voltage.c b/firmware/basic/voltage.c index 4996077..ff37ab6 100644 --- a/firmware/basic/voltage.c +++ b/firmware/basic/voltage.c @@ -3,26 +3,31 @@ #include "basic/basic.h" #include "funk/nrf24l01p.h" -static uint32_t voltage=5000; +#define VOLTAGE_SAMPLES 8 +static uint32_t voltage=5000*VOLTAGE_SAMPLES; static uint8_t chrg=1; void VoltageCheck(void){ - + uint32_t v; chrg=gpioGetValue(RB_PWR_CHRG); - //slow down the adc for our high impedance volatage devider + //slow down the adc for our high impedance voltage devider ADC_AD0CR = ((CFG_CPU_CCLK / SCB_SYSAHBCLKDIV) / 100000 - 1 ) << 8; - voltage = adcRead(1); + v = adcRead(1); //speed it up again ADC_AD0CR = ((CFG_CPU_CCLK / SCB_SYSAHBCLKDIV) / 1000000 - 1 ) << 8; - voltage *= 10560; - voltage /= 1024; + v *= 10560; + v /= 1024; //add the drop over the voltage switch - voltage += 50; + v += 50; + + voltage -= voltage/VOLTAGE_SAMPLES; + voltage += v; //battery is assumed empty if the volatge falls bellow 3.5V - if( voltage < 3500 ){ + if( voltage < (3500*VOLTAGE_SAMPLES) ){ + //if( voltage < 3500 ){ nrf_off(); gpioSetValue (RB_PWR_GOOD, 0); gpioSetValue (RB_LCD_BL, 0); @@ -35,7 +40,8 @@ void VoltageCheck(void){ }; uint32_t GetVoltage(void){ - return voltage; + return voltage/8; + //return voltage; }; uint8_t GetChrgStat(void){