41 #error "F_CPU not defined for adc.h" 45 #error "VCC not defined for adc.h" 85 uint16_t
read(uint8_t channel, uint8_t nsamples = 1)
88 for (uint8_t n = 0; n < nsamples; n++) {
89 sum += readAdc(channel);
91 return (uint16_t)(sum / nsamples);
103 return toMillivolts(
read(channel, nsamples));
115 return toVolts(
read(channel, nsamples));
119 static Adc s_instance;
124 ADMUX = (1 << REFS0);
127 ADCSRA |= prescaler();
130 ADCSRA |= (1 << ADEN);
136 uint16_t readAdc(uint8_t channel)
138 ADMUX = (ADMUX & ~(0x1F)) | (channel & 0x1F);
139 ADCSRA |= (1 << ADSC);
140 while (ADCSRA & (1 << ADSC));
144 static uint16_t toMillivolts(uint16_t adc)
146 uint16_t result = (
static_cast<uint32_t
>(VCC) * adc) / 1024;
150 static float toVolts(uint16_t adc)
152 float result = (
static_cast<uint32_t
>(VCC) * adc) / 1024000.0F;
156 template <u
int16_t prescaler>
157 static constexpr uint8_t adcPrescaler()
159 static_assert(prescaler == 2 || prescaler == 4 || prescaler == 8 ||
160 prescaler == 16 || prescaler == 32 || prescaler == 64 ||
162 "Invalid prescaler (2, 4, 8, 16, 32, 64, 128)");
163 return prescaler == 2 ? (1 << ADPS0) :
164 prescaler == 4 ? (1 << ADPS1) :
165 prescaler == 8 ? (1 << ADPS1) | (1 << ADPS0) :
166 prescaler == 16 ? (1 << ADPS2) :
167 prescaler == 32 ? (1 << ADPS2) | (1 << ADPS0) :
168 prescaler == 64 ? (1 << ADPS2) | (1 << ADPS1) :
169 prescaler == 128 ? (1 << ADPS2) | (1 << ADPS1) |
173 template <u
int32_t min, u
int32_t max, u
int8_t prescaler>
174 static constexpr
bool checkPrescaler()
176 return (F_CPU / prescaler) <=
max &&
177 (F_CPU / prescaler) >=
min ?
true :
false;
180 template <u
int32_t min, u
int32_t max>
181 static constexpr uint8_t calcPrescaler()
183 return checkPrescaler<min, max, 2>() ? 2 :
184 checkPrescaler<min, max, 4>() ? 4 :
185 checkPrescaler<min, max, 8>() ? 8 :
186 checkPrescaler<min, max, 16>() ? 16 :
187 checkPrescaler<min, max, 32>() ? 32 :
188 checkPrescaler<min, max, 64>() ? 64 :
189 checkPrescaler<min, max, 128>() ? 128 : 0;
192 static constexpr uint8_t prescaler()
194 return adcPrescaler<calcPrescaler<50000, 200000>()>();
198 Adc Adc::s_instance =
Adc();
uint16_t read(uint8_t channel, uint8_t nsamples=1)
Reads the (average) raw ADC value (in Single Conversion mode) for the given channel.
Definition: adc.h:85
static Adc & instance()
Returns the Avr::Adc instance.
Definition: adc.h:73
Namespace containing all symbols of the AVR C++ utilities library.
Definition: adc.h:48
T min(T a, T b)
Returns the minimum of the two values a and b.
Definition: utils.h:188
float readVolts(uint8_t channel, uint8_t nsamples=1)
Reads the (average) ADC voltage (in Single Conversion mode) for the given channel.
Definition: adc.h:113
T max(T a, T b)
Returns the maximum of the two values a and b.
Definition: utils.h:201
uint16_t readMilliVolts(uint8_t channel, uint8_t nsamples=1)
Reads the (average) ADC voltage (in Single Conversion mode) for the given channel.
Definition: adc.h:101
A C++ wrapper for accessing the built-in 10 bit Analog to Digital Conversion (ADC) facilities...
Definition: adc.h:65