Created
December 6, 2016 16:13
-
-
Save dvdfreitag/41191074598accfeb6d9cb1a775b1009 to your computer and use it in GitHub Desktop.
Snippet for reading XMEGA temperature sensor
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
void init(void) | |
{ | |
// Configure internal 1V VREF, enable temperature readings | |
ADCA.REFCTRL = 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 62.5kHz, 32MHz / 512 = 62.5kHz | |
ADCA.PRESCALER = ADC_PRESCALER_DIV512_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