Skip to content

Commit

Permalink
Merge pull request #21 from laurb9/calibrate_aref_teensy
Browse files Browse the repository at this point in the history
Calibrate aref teensy
  • Loading branch information
laurb9 committed Mar 2, 2015
2 parents 7ee3d6c + c862224 commit 2596e19
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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_ */

0 comments on commit 2596e19

Please sign in to comment.