STM32F103C8 ADC internal temperature read way too high.
Hi
Using the STM32F103C8 with STM32CubeIDE and bare metal programming, I am trying to read the internal temperature. While I manage to read the ADC, enter the ADC ISR and do the calculation to get the temperature, the result is unrealistic. The data register value is around 1600, which seems to be way too high. The resulting calculation to get the temperature in degC is then around 400degC.
Does anyone see a big issue in my setup?
Thank you,
/**
* STM32F103C8 ADC
* Uses ADC1
* Reads the temperature from the internal temp sensor
*/
#include <stdint.h>
#include "stm32f1xx.h"
#define AVG_SLOPE 4.3 // mV per C
#define V25 1.43 // V
int main(void)
{
__NVIC_EnableIRQ(ADC1_2_IRQn);
// Setup of ADC1
uint8_t i = 0;
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
for(;i<255;i++);
i=0;
ADC1->CR1 |= ADC_CR1_EOSIE;
ADC1->CR2 |= ADC_CR2_ADON | ADC_CR2_EXTSEL_0 | ADC_CR2_EXTSEL_1 | ADC_CR2_EXTTRIG | ADC_CR2_TSVREFE;
ADC1->SMPR1 |= ADC_SMPR1_SMP16;
ADC1->SQR3 |= 16;
// Setup of TIM2
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
for(;i<255;i++);
i=0;
GPIOA->CRL |= GPIO_CRL_MODE1_1 | GPIO_CRL_CNF1_1;
// Enables TIM2 bus
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
for(;i<255;i++);
i=0;
TIM2->CR1 |= TIM_CR1_ARPE | TIM_CR1_URS;
TIM2->DIER |= TIM_DIER_UIE;
TIM2->PSC = 7999;
TIM2->ARR = 1000;
TIM2->CCMR1 |= TIM_CCMR1_OC2PE | TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2M_2;
TIM2->EGR |= TIM_EGR_CC2G;
TIM2->CCER |= TIM_CCER_CC2E;
TIM2->CCR2 = 500;
TIM2->CR1 |= TIM_CR1_CEN;
/* Loop forever */
for(;;);
}
void ADC1_2_IRQHandler(void)
{
// Handles the IRQ of ADC1. EOC flag is cleared by reading data register
static uint32_t temp = 0;
static uint32_t temp_sensor = 0;
temp = ADC1->DR;
temp_sensor = (temp-V25)/(AVG_SLOPE)+25;
(void)temp_sensor;
}