int16_t ref, degrees_x10; float kelvin_per_adc_x10;
ADCA.CTRLA = ADC_FLUSH_bm; // cancel any pending conversions, disable ADC
// set up exactly how Atmel did when they measured the calibration value ADCA.CTRLB = ADC_RESOLUTION_12BIT_gc; // unsigned conversion, produces result in range 0-2048 ADCA.REFCTRL = ADC_REFSEL_INT1V_gc | ADC_TEMPREF_bm; ADCA.PRESCALER = ADC_PRESCALER_DIV512_gc;
ADCA.CH0.CTRL = ADC_CH_INPUTMODE_INTERNAL_gc | ADC_CH_GAIN_1X_gc; ADCA.CH0.MUXCTRL = ADC_CH_MUXINT_TEMP_gc;
ADCA.CTRLA |= ADC_ENABLE_bm; adc_wait_8mhz();
// get 358 K factory calibrated value ref = HW_read_signature_byte(PROD_SIGNATURES_START + TEMPSENSE0); ref += HW_read_signature_byte(PROD_SIGNATURES_START + TEMPSENSE1) << 8; kelvin_per_adc_x10 = ((273 + 85)*10) / (float)ref; // reference is ADC reading at 85C, scaled by 10 to get units of 0.1C
degrees_x10 = ADC_sample(&ADCA.CH0); ADCA.CTRLA = 0; // turn ADC off ADCA.REFCTRL = 0; // turn temperature sensor off
degrees_x10 *= kelvin_per_adc_x10;
degrees_x10 -= 2730;
return (degrees_x10);