foreword
This chapter introduces how to use and obtain the STM32 chip temperature sensor.
1. What is an internal temperature sensor?
STM32 has an internal temperature sensor, which can be used to measure the temperature of the CPU and its surroundings (the internal temperature sensor is more suitable for detecting temperature changes, and an external sensor should be used when accurate temperature measurement is required). For STM32F103, the temperature sensor is internally connected to the ADC1_IN16 input channel, which converts the voltage output by the sensor
into a numeric value. The recommended sampling time for temperature sensor analog input is 17.1us. The temperature range supported by the STM32F103 internal temperature sensor is: -40~125 degrees. The accuracy is about ±1.5°C. The use of the STM32 internal temperature sensor is very simple, just set up the internal ADC and activate its internal temperature sensor channel. Regarding the ADC settings, we have already introduced them in detail in the last chapter, so I won’t say more here. Next, let's introduce two places related to the temperature sensor setting. In the first place, if we want to use the internal temperature sensor of STM32, we must first activate the internal channel of ADC, which is set by the AWDEN bit (bit23) of ADC_CR2. Setting this bit to 1 enables the internal temperature sensor. In the second place, the internal temperature sensor of STM32 is fixedly connected to channel 16 of ADC1, so we only need to read the value of channel 16 after setting ADC1, which is the voltage value returned by the temperature sensor. Based on this value, we can calculate the current temperature. Calculated as follows:
𝑈(℃) ={ (V25-Vsense) / Avg_Slope}+25
In the formula:
V25 = Value of Vsense at 25 degrees (Typical value: 1.43)
Avg_Slope = Average slope of temperature vs. Vsense curve (unit: mv/°C or uv/°C) (typical value: 4.3mv/°C).
Using the above formula, we can easily calculate the temperature of the current temperature sensor.
As can be seen from the list of ADC channels below, the 16 channels of ADC1 are connected to the internal temperature sensor
2. Experimental process
1.STM32CubeMX configuration
Select the chip stm32f103c6t6 and create a new project
Set the clock source, the minimum system external crystal oscillator is 8Mhz, as the external high-speed HSE clock source. Since there is no external low-speed crystal oscillator, here the low-speed clock source selects the bypass clock source.
Configure the clock tree, here use the officially recommended configuration
In order to show the change of internal temperature, we configure USART1 and print the result of obtaining the temperature
The parameter configuration of USART1 is as follows, the baud rate is 115200, the transmission data length is 8 Bit, the parity check is not available, and the stop bit is 1. Other parameters are default
Configure ADC, activate ADC1 temperature sensor channel, set right alignment, turn off scan, continuous and intermittent mode, enable regular conversion, set software trigger, set sampling time 239.5 cycles (19.96us)
Set in Code Generator to only copy the used library, separate .c and .h files
Set the project name and path, click GENERATE CODE, and open it with keil5 IDE after generation.
2. Code implementation
Add the following code after the usart.c file, add #ifdef macro definition in the code for conditional compilation, if using GUNC compilation, then PUTCHAR_PROTOTYPE is defined as int __io_putchar(int ch) function, otherwise it is defined as int fputc(int ch, FILE * f) function.
/* USER CODE BEGIN 0 */ #include "stdio.h" #ifdef __GNUC__ /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf set to 'Yes') calls __io_putchar() */ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif /* __GNUC__ */ /** * @brief Retargets the C library printf function to the USART. * @param None * @retval None */ PUTCHAR_PROTOTYPE { /* Place your implementation of fputc here */ /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */ HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF); return ch; } /* USER CODE END 0 */
The main function is as follows:
int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_ADC1_Init(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ HAL_ADC_Start(&hadc1); HAL_ADC_PollForConversion(&hadc1,10); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ float AD_Value = HAL_ADC_GetValue(&hadc1); float Vol_Value = AD_Value*(3.3/4096); float Temperature = (1.43 - Vol_Value)/0.0043 + 25; printf("MCU Internal Temperature: %.2f¡æ\r\n",Temperature); printf("\r\n"); HAL_Delay(1000); } /* USER CODE END 3 */ }
3. Experimental results
You can see the printed experimental results as follows
Summarize
The above introduces the use and acquisition methods of the STM32 chip temperature sensor. The codes are all in the group, and everyone can learn together.