Created
December 6, 2016 16:20
-
-
Save dvdfreitag/74dbef00026670d7e359303e04448fe8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The XMEGA E-series is different from all the other chips in the line, specifically the temperature sensor. | |
// It needs to be read in signed mode with a 125kHz clock. Everything else is the same. | |
void init(void) | |
{ | |
// Configure signed conversions, internal 1V VREF, and enable temperature readings | |
ADCA.REFCTRL = ADC_CONMODE_bm | ADC_REFSEL_INT1V_gc | ADC_TEMPREF_bm; | |
// 12bit resolution in high current mode | |
ADCA.CTRLB = ADC_RESOLUTION_12BIT_gc | ADC_CURRLIMIT_HIGH_gc; | |
// ADC clock must be 125kHz, 32MHz / 256 = 125kHz | |
ADCA.PRESCALER = ADC_PRESCALER_DIV256_gc; | |
// 1:1 gain with internal positive reading | |
ADCA.CH0.CTRL = ADC_CH_GAIN_1X_gc | ADC_CH_INPUTMODE_INTERNAL_gc; | |
// Configure mux to read from temperature sensor | |
ADCA.CH0.MUXCTRL = ADC_CH_MUXINT_TEMP_gc; | |
// Enable ADC | |
ADCA.CTRLA |= ADC_ENABLE_bm; | |
// Enable interrupts globally | |
sei(); | |
// Initiate a dummy reading | |
ADCA.CH0.CTRL |= ADC_CH_START_bm; | |
// Wait for result to be converted | |
while(!(ADCA.INTFLAGS & ADC_CH0IF_bm)); | |
// Clear interrupt bit | |
ADCA.INTFLAGS = ADC_CH0IF_bm; | |
} | |
uint16_t get_temp(void) | |
{ | |
// Initiate reading | |
ADCA.CH0.CTRL |= ADC_CH_START_bm; | |
// Wait for result to be converted | |
while(!(ADCA.INTFLAGS & ADC_CH0IF_bm)); | |
// Return result | |
return ADCA.CH0.RES; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment