Can't get internal temperature sensor via ADC
I am having a tough time getting a valid temperature reading from my STM32L051K8U6 MCU. This should be an easy task. I've spent a LOT of time Googling and believe I have everything setup correctly. Most of the sample code online is identical, but it does not work for me.
Here is the code snippet I'm testing with:
#define TEMP130_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FF8007E))
#define TEMP30_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FF8007A))
#define VDD_CALIB ((uint16_t) (300))
#define VDD_APPLI ((uint16_t) (330))
int32_t ComputeTemperature(uint32_t measure)
{
int32_t temperature;
temperature = ((measure * VDD_APPLI / VDD_CALIB) - (int32_t)*TEMP30_CAL_ADDR );
temperature = temperature *(int32_t)(130-30);
temperature = temperature /(int32_t)(*TEMP130_CAL_ADDR -*TEMP30_CAL_ADDR);
temperature = temperature + 30;
return(temperature);
return(temperature);
}
void ConfigTemperature(void)
{
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
ADC1->CFGR2 |= ADC_CFGR2_CKMODE;
if ((ADC1->CR & ADC_CR_ADEN) != 0)
{
ADC1->CR &= (uint32_t)(~ADC_CR_ADEN);
}
ADC1->CR |= ADC_CR_ADCAL;
while ((ADC1->ISR & ADC_ISR_EOCAL) == 0)
{
PRINTF("Calib");
}
ADC1->ISR |= ADC_ISR_EOCAL;
ADC1->ISR |= ADC_ISR_ADRDY;
ADC1->CR |= ADC_CR_ADEN;
if ((ADC1->CFGR1 & ADC_CFGR1_AUTOFF) == 0)
{
while ((ADC1->ISR & ADC_ISR_ADRDY) == 0)
{
PRINTF("Enable");
}
}
ADC1->ISR |= ADC_ISR_ADRDY;
ADC1->CR |= ADC_CR_ADEN;
ADC1->CFGR1 |= ADC_CFGR1_CONT;
ADC1->CHSELR = ADC_CHSELR_CHSEL18;
ADC1->SMPR |= ADC_SMPR_SMP;
ADC->CCR |= ADC_CCR_TSEN;
uint32_t measure = ADC1->DR;
PRINTF("Measure %i\n\r", measure);
PRINTF("The temperature value is %i\n\r",ComputeTemperature(measure));
}The problem is that ADC1->DR always returns 62, which is way to low as it's calculated as -221 degrees C.
I'm sure I'm missing something simple, but can't seem to figure it out. Please help.
