diff --git a/Vcc.cpp b/Vcc.cpp index 8dbcbe3..050b982 100644 --- a/Vcc.cpp +++ b/Vcc.cpp @@ -23,6 +23,9 @@ #include "Vcc.h" +//time to get the averge adc value +#define AVER_NUM 20 + Vcc::Vcc( const float correction ) : m_correction(correction) { @@ -76,3 +79,42 @@ float Vcc::Read_Perc(const float range_min, const float range_max, const boolean return perc; } + +int Vcc::Read_ADC(void) +{ + // Read 1.1V reference against AVcc + // set the reference to Vcc and the measurement to the internal 1.1V reference + if (ADMUX != ADMUX_VCCWRT1V1) + { + ADMUX = ADMUX_VCCWRT1V1; + + // Bandgap reference start-up time: max 70us + // Wait for Vref to settle. + delayMicroseconds(350); + } + + // Start conversion and wait for it to finish. + ADCSRA |= _BV(ADSC); + while (bit_is_set(ADCSRA,ADSC)) {}; + return ADC; +} + +int Vcc::Read_ADC(int num) +{ + static int Vcc_int[AVER_NUM]={0}; + static int k = 0; + static int sum = 0; + if (num > AVER_NUM || num <0) + return -1; + + sum -= Vcc_int[k]; + Vcc_int[k++] = Read_ADC(); + sum += Vcc_int[k-1]; + + if (k>num-1) + { + k = 0; + } + + return sum/num; +} diff --git a/Vcc.h b/Vcc.h index 1d82e22..3ea9855 100644 --- a/Vcc.h +++ b/Vcc.h @@ -44,6 +44,15 @@ class Vcc * @return Current Vcc level, in Volts. */ float Read_Volts(void); + /** + * return the current adc value by int + */ + int Read_ADC(void); + /** + * return the current adc value using digital slide filtering by num times + * + */ + int Read_ADC(int num); /** * Retrieve current Vcc level. The total voltage range shall be passed