/* Example for analogRead ----> Simplifed by Deane Feb 7 2020
You can change the number of averages, bits of resolution and also the comparison value or range.
*/
#include <ADC.h>
#include <ADC_util.h>
const int readPin = A1; // ADC0
ADC *adc = new ADC(); // adc object;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(readPin, INPUT);
Serial.begin(9600);
[B][COLOR="#FF0000"]while( !Serial && millis()<5000 );[/COLOR][/B]
Serial.println("Begin setup");
///// ADC0 ////
// reference can be ADC_REFERENCE::REF_3V3, ADC_REFERENCE::REF_1V2 (not for Teensy LC) or ADC_REFERENCE::REF_EXT.
//adc->adc0->setReference(ADC_REFERENCE::REF_1V2); // change all 3.3 to 1.2 if you change the reference to 1V2
adc->adc0->setAveraging(4); // set number of averages
adc->adc0->setResolution(12); // set bits of resolution
// Choose one of the following Conversion speeds:
// ADC_CONVERSION_SPEED enum: VERY_LOW_SPEED, LOW_SPEED, MED_SPEED, HIGH_SPEED_16BITS,
// HIGH_SPEED or VERY_HIGH_SPEED
// see the documentation for more information
// Additionally the conversion speed can also be ADACK_2_4, ADACK_4_0, ADACK_5_2 and ADACK_6_2,
// where the numbers are the frequency of the ADC clock in MHz and are independent of the
// bus speed.
adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::VERY_HIGH_SPEED); // change conversion speed
// Choose sampling speed: ADC_MED_SPEED enum: VERY_LOW_SPEED, LOW_SPEED, MED_SPEED,
// HIGH_SPEED or VERY_HIGH_SPEED
adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::VERY_HIGH_SPEED); // change sampling speed
// always call the compare functions after changing the resolution!
//adc->adc0->enableCompare(1.0/3.3*adc->adc0->getMaxValue(), 0); // measurement will be ready
// if value < 1.0V
//adc->adc0->enableCompareRange(1.0*adc->adc0->getMaxValue()/3.3, 2.0*adc->adc0->getMaxValue()/3.3, 0, 1); // ready if value lies out of [1.0,2.0] V
// If you enable interrupts, notice that the ISR will read the result,
// so that isComplete() will return false (most of the time)
//adc->adc0->enableInterrupts(adc0_isr);
Serial.println("End setup");
}
int value;
void loop() {
// Single reads
value = adc->adc0->analogRead(readPin); // read a new value, will return ADC_ERROR_VALUE if
// the comparison is false.
Serial.print("Pin: ");
Serial.print(readPin);
Serial.print(", value ADC0: ");
Serial.println(value * 3.3 / adc->adc0->getMaxValue(), DEC);
// Print errors, if any.
if (adc->adc0->fail_flag != ADC_ERROR::CLEAR) {
Serial.print("ADC0: "); Serial.println(getStringADCError(adc->adc0->fail_flag));
}
digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
delay(50);
}
// If you enable interrupts make sure to call readSingle() to clear the interrupt.
void adc0_isr() {
// adc->adc0->readSingle();
}
// ***** END *****