Zoltan Hudak
/
Internal_Temperature_F103RB
Reading temperature sensor built into STM32F103RB chips
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* 00002 * Reading built-in temperature sensor of STM32F103RB chip (on a NUCLEO-F103RB board) 00003 */ 00004 00005 #include "mbed.h" 00006 00007 /* 00008 * STM32F103x data-sheet: 00009 * 5.3.19 Temperature sensor characteristics 00010 * Table 50. TS characteristics, Page 80 00011 */ 00012 const float AVG_SLOPE = 4.3E-03; // slope (gradient) of temperature line function [V/°C] 00013 const float V25 = 1.43; // sensor's output voltage at 25°C [V] 00014 const float ADC_TO_VOLT = 3.3 / 4096; // conversion coefficient of digital value to voltage [V] 00015 // when using 3.3V ref. voltage at 12-bit resolution (2^12 = 4096) 00016 00017 Serial pc(USBTX, USBRX); 00018 DigitalOut led(PC_13); 00019 ADC_HandleTypeDef hadc1; // ADC handle 00020 uint16_t adcValue; // digital value of sensor 00021 float vSense; // sensor's output voltage [V] 00022 float temp; // sensor's temperature [°C] 00023 00024 /* ADC1 init function */ 00025 void MX_ADC1_Init(void) 00026 { 00027 ADC_ChannelConfTypeDef sConfig; 00028 00029 /**Common config 00030 */ 00031 hadc1.Instance = ADC1; 00032 hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; 00033 hadc1.Init.ContinuousConvMode = DISABLE; 00034 hadc1.Init.DiscontinuousConvMode = DISABLE; 00035 hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; 00036 hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; 00037 hadc1.Init.NbrOfConversion = 1; 00038 HAL_ADC_Init(&hadc1); 00039 00040 /**Configure Regular Channel 00041 */ 00042 sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; 00043 sConfig.Rank = 1; 00044 sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; 00045 HAL_ADC_ConfigChannel(&hadc1, &sConfig); 00046 } 00047 00048 int main() 00049 { 00050 MX_ADC1_Init(); // initialize AD convertor 00051 while(HAL_ADCEx_Calibration_Start(&hadc1) != HAL_OK); // calibrate AD convertor 00052 00053 while(1) { 00054 HAL_ADC_Start(&hadc1); // start analog to digital conversion 00055 while(HAL_ADC_PollForConversion(&hadc1, 1000000) != HAL_OK);// wait for completing the conversion 00056 adcValue = HAL_ADC_GetValue(&hadc1); // read sensor's digital value 00057 vSense = adcValue * ADC_TO_VOLT; // convert sensor's digital value to voltage [V] 00058 /* 00059 * STM32F103xx Reference Manual: 00060 * 11.10 Temperature sensor 00061 * Reading the temperature, Page 235 00062 * Temperature (in °C) = {(V25 - Vsense) / Avg_Slope} + 25 00063 */ 00064 temp = (V25 - vSense) / AVG_SLOPE + 25.0f; // convert sensor's output voltage to temperature [°C] 00065 pc.printf("temp = %3.1f%cC\n", temp, 176); // display chip's temperature [°C] 00066 led = !led; 00067 wait_ms(1000); 00068 } 00069 }
Generated on Fri Jul 15 2022 10:36:34 by 1.7.2