From 17fdc7ca1415442922cfef5fa2051d1877eb50f1 Mon Sep 17 00:00:00 2001 From: Laurentiu Badea Date: Sun, 1 Mar 2015 23:16:50 -0800 Subject: [PATCH 1/2] Teensy: AREF calibration --- adc_avr.cpp | 2 +- adc_teensy3.cpp | 24 ++++++++++++++++++++++++ adc_teensy3.h | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/adc_avr.cpp b/adc_avr.cpp index c5d896f..592e15a 100644 --- a/adc_avr.cpp +++ b/adc_avr.cpp @@ -27,7 +27,7 @@ #if defined(ADCSRA) && defined(ADCL) // Actual voltage of Vbg (internal 1.1V ref) in mV. This is different from chip to chip. -// = 5000*1100/VREFmeasured (with accurate multimeter). 3300*1100/VREFmeasured for 3.3V systems. +// = VrefMeasured*1100/5.000 (with accurate multimeter). VrefMeasured*1100/3.300 for 3.3V systems. #define INTERNAL_REF_MV 1090 const uint8_t ADCInput::prescalers[] = {7,6,5,4,3,2}; // 1:8MHz clock is out of ADC spec for 16MHz AVR diff --git a/adc_teensy3.cpp b/adc_teensy3.cpp index e5bbc5c..1f7dd36 100644 --- a/adc_teensy3.cpp +++ b/adc_teensy3.cpp @@ -11,6 +11,11 @@ #ifdef __MK20DX256__ #include "adc_teensy3.h" +// Actual voltage of internal 1.2V ref in mV. This is different from chip to chip. +// = VRefMeasured*1200/3.3 for 3.3V systems. +#define INTERNAL_REF_MV (3280*1200L/3300) + +#define INTERNAL_REF_PORT 39 #define ADC_CLOCK_TO_SAMPLING 15 static unsigned int averagingTable[] = {32, 16, 8, 4, 0, 0}; @@ -45,6 +50,25 @@ bool ADCInput::setMode(uint8_t mode=0){ } } +/* + * Read internal reference voltage. + */ +uint16_t ADCInput::calibrateAREF(){ + // reset ADC to default mode for highest precision. + uint8_t oldMode = curMode; + setMode(0); + analogReadRes(16); + analogRead(INTERNAL_REF_PORT); + delay(200); + uint16_t rangemV = INTERNAL_REF_MV * ((1L<<16)-1) / analogRead(INTERNAL_REF_PORT); + + // set ADC to previous mode + setMode(oldMode); + read(); + + return rangemV; +} + /* * Return ADC clock in Hz. This is only useful to estimate sampling rate. */ diff --git a/adc_teensy3.h b/adc_teensy3.h index 3f691f7..1debc72 100644 --- a/adc_teensy3.h +++ b/adc_teensy3.h @@ -35,6 +35,7 @@ class ADCInput : public ADCBase { *buffer++ = analogRead(input); } } + uint16_t calibrateAREF(); }; #endif /* ADC_TEENSY3_ */ From c862224632e73928828927b69ab88423424b7931 Mon Sep 17 00:00:00 2001 From: Laurentiu Badea Date: Sun, 1 Mar 2015 23:18:44 -0800 Subject: [PATCH 2/2] Add AREF calibration feature --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 46e8933..328c04b 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ Features ======== - Configurable ADC speed for sampling rates from 10KHz to 300KHz (on UNO) - Auto grid on time base from 2ms to 50us (on UNO) +- Vmax calibration and 3.3-5V detection using internal reference - 0V auto trigger - Display minimum-maximum voltage of captured signal - Square waveforms are displayed properly