Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method that directly return the adc value and get average value by digital slide filtering algorithm #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Vcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
9 changes: 9 additions & 0 deletions Vcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down