Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Teensy: AREF calibration
  • Loading branch information
laurb9 committed Mar 2, 2015
commit 17fdc7ca1415442922cfef5fa2051d1877eb50f1
2 changes: 1 addition & 1 deletion adc_avr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions adc_teensy3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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.
*/
Expand Down
1 change: 1 addition & 0 deletions adc_teensy3.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ADCInput : public ADCBase {
*buffer++ = analogRead(input);
}
}
uint16_t calibrateAREF();
};

#endif /* ADC_TEENSY3_ */