2018-12-29 03:33 AM
Hi guys and girls!
I'm using STSPIN32F0, an MCU with built-in 3-phase motor driver and I'm trying to read internal temperature sensor. What I'm getting back looks to me like random walk within range of about 10 degrees Celsius (please see attachment). When the chip gets hotter the average temp goes up, which means sensor does sense the temperature, however it still fluctuates about 10 degrees.
My question is: am I doing something wrong or is internal sensor that noisy?
By the way I tested the same thing with STM32F031C6 which is the core of STSPIN32F0 and I get similar picture but with about 5 degrees fluctuation.
Thanks everyone for help =)
Misha.
#include <main.h>
#include <stm32f0xx_ll_adc.h>
const uint32_t TEMP_PERIOD = 1000;
const uint32_t TEMP_PRESCALER = 48000;
#define TEMP110_CAL_ADDR TEMPSENSOR_CAL2_ADDR
#define TEMP30_CAL_ADDR TEMPSENSOR_CAL1_ADDR
int32_t temperature; // temperature in degrees Celsius
extern "C"
void TIM3_IRQHandler(void) {
TIM3->SR &= ~TIM_SR_UIF;
// temp
int sensor = ADC1->DR;
int calib30 = (int) *TEMP30_CAL_ADDR;
int calib110 = (int) *TEMP110_CAL_ADDR;
temperature = (sensor - calib30) * (110 - 30) / (calib110 - calib30) + 30;
}
void initTemperature(){
// timer
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; // enable timer 3
TIM3->ARR = TEMP_PERIOD; // tim3 period
TIM3->PSC = TEMP_PRESCALER; // prescaler
TIM3->DIER |= TIM_DIER_UIE; // enable update interrupt
TIM3->CR1 |= TIM_CR1_CEN; // enable timer 3
HAL_NVIC_SetPriority(TIM3_IRQn, 1, 1);
HAL_NVIC_EnableIRQ(TIM3_IRQn);
// ADC
RCC->APB2ENR |= RCC_APB2ENR_ADCEN;
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // enable ADC1
ADC1->CHSELR = ADC_CHSELR_CHSEL16; // Select CHSEL16 for temperature sensor
ADC1->SMPR |= ADC_SMPR_SMP_0 | ADC_SMPR_SMP_1 | ADC_SMPR_SMP_2; // sample rate
ADC->CCR |= ADC_CCR_TSEN; // enable temp sensor
ADC->CCR |= ADC_CCR_VREFEN; // enable internal voltage reference
ADC1->CFGR1 |= ADC_CFGR1_CONT; // continous mode
ADC1->CR |= ADC_CR_ADEN; // enable ADC
while ((ADC1->ISR & ADC_ISR_ADRDY) == 0) {} // wait for ADC to be ready
ADC1->CR |= ADC_CR_ADSTART;
}
2018-12-29 05:58 AM
The temp sensor in the stm32 measures its junction temperatute which might fluctuate. If the goal is to measure the motor temp, better put an external motor thermistor connected to the adc with proper impedence. Otherwise, maybe there could be a vdd compensation vs calibrated values?
2018-12-29 04:59 PM
Thank you KIC for your answer :)
That was exactly my question: does temp fluctuate that much (or is sensor noisy) or am I doing something wrong? I'm still hoping anyone can share their experience.
Your answer does not convince me though. Why would temp fluctuate? MCU does not do anything computationally variable, it just reads a couple of peripherals at a steady speed inside main(), plus this timer interrupt, that's it.
2019-01-02 03:52 AM
ADC fluctuates. Measure more often and average several sample.