From 87657ad04cab5769438b3513fc038477482f4e35 Mon Sep 17 00:00:00 2001 From: cheng3100 Date: Sun, 7 Jan 2018 16:02:43 +0800 Subject: [PATCH 1/2] add a method that returnadc int directly --- Vcc.cpp | 19 +++++++++++++++++++ Vcc.h | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/Vcc.cpp b/Vcc.cpp index 8dbcbe3..057d39b 100644 --- a/Vcc.cpp +++ b/Vcc.cpp @@ -76,3 +76,22 @@ 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; +} diff --git a/Vcc.h b/Vcc.h index 1d82e22..64db237 100644 --- a/Vcc.h +++ b/Vcc.h @@ -44,6 +44,10 @@ class Vcc * @return Current Vcc level, in Volts. */ float Read_Volts(void); + /** + * return the current adc value by int + */ + int Read_ADC(void); /** * Retrieve current Vcc level. The total voltage range shall be passed From 940f1cfb9c9ee96a427d46c75b82afe0f9021f8e Mon Sep 17 00:00:00 2001 From: cheng3100 Date: Sun, 7 Jan 2018 16:26:00 +0800 Subject: [PATCH 2/2] slide flitering --- Vcc.cpp | 23 +++++++++++++++++++++++ Vcc.h | 5 +++++ 2 files changed, 28 insertions(+) diff --git a/Vcc.cpp b/Vcc.cpp index 057d39b..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) { @@ -95,3 +98,23 @@ int Vcc::Read_ADC(void) 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 64db237..3ea9855 100644 --- a/Vcc.h +++ b/Vcc.h @@ -48,6 +48,11 @@ class Vcc * 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