STM32CubeMX Series|Internal Temperature Sensor

Internal temperature sensor

1. Introduction to internal temperature sensor

STM32F1 has an internal temperature sensor that can be used to measure the temperature of the CPU and its surroundings. The temperature sensor is internally connected to the ADCx_IN16 input channel. This channel converts the sensor output voltage into a digital value. The recommended sampling time for the temperature sensor analog input is 17.1us. The internal temperature sensor supports the temperature range: -40~125℃, The accuracy is about ±1.5℃

Insert picture description here
By reading the value of ADC channel 16, the current temperature can be calculated by the following calculation formula:

T = { (V25 - Vsense) / Avg_Slope } + 25

In the above formula:

V 25 = Value of V sense at 25°C (typical value: 1.43)
Avg_Slope = Average slope of temperature and V sense curve (typical value: 4.3 mV/°C)

For the above typical values, please refer to the introduction in the electrical characteristics chapter of the data sheet

Insert picture description here

2. Hardware design

In this experiment, the internal temperature is collected through ADC1 channel 16, and the sampled AD value and the converted temperature value are printed out through the USART1 serial port. At the same time, the D1 indicator flashes, indicating that the system is operating normally

  • D1 indicator
  • ADC1_INT1
  • USART1 serial port

3. Software design

3.1 STM32CubeMX settings
  • RCC sets external HSE, clock is set to 72M, ADC prescaler factor is set to 6, ADC_CLK is 12MHz
  • PC0 is set to GPIO push-pull output mode, pull-up, high-speed, and the default output level is high
  • USART1 is selected as the asynchronous communication mode, the baud rate is set to 115200Bits/s, the transmission data length is 8Bit, no parity, 1 stop bit
  • Activate ADC1 temperature sensor channel, set right alignment, turn off scanning, continuous and intermittent modes, enable regular conversion, set software trigger, set sampling time 239.5 cycles (19.96us)

Insert picture description here

  • Enter the project name, select the project path (no Chinese), select MDK-ARM V5; check Generated periphera initialization as a pair of'.c/.h' files per IP; click GENERATE CODE to generate the project code
3.2 MDK-ARM programming
  • The ADC initialization function can be seen in the adc.c file
void MX_ADC1_Init(void){
    
    
  ADC_ChannelConfTypeDef sConfig = {
    
    0};
  /** Common config */
  hadc1.Instance = ADC1;
  hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 1;
  if (HAL_ADC_Init(&hadc1) != HAL_OK){
    
    
    Error_Handler();
  }
  /** Configure Regular Channel*/
  sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK){
    
    
    Error_Handler();
  }
}

void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle){
    
    
  if(adcHandle->Instance==ADC1)  {
    
    
    /* ADC1 clock enable */
    __HAL_RCC_ADC1_CLK_ENABLE();
  }
}
  • Add the following test program in the while loop of the main function
while (1){
    
    
	HAL_ADC_Start(&hadc1);	//启动ADC转换
	HAL_ADC_PollForConversion(&hadc1,10);	//等待转换完成,10ms表示超时时间
	AD_Value = HAL_ADC_GetValue(&hadc1);	//读取ADC转换数据(12位数据)
	printf("ADC1_IN16 ADC value: %d\r\n",AD_Value);
	Vol_Value = AD_Value*(3.3/4096);	//AD值乘以分辨率即为电压值
	printf("ADC1_IN16 VOL value: %.2fV\r\n",Vol_Value);
	Temperature = (1.43 - Vol_Value)/0.0043 + 25;	//根据公式算出温度值
	printf("MCU Internal Temperature: %.2f¡æ\r\n",Temperature);
	printf("\r\n");
  
	HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_0);
	HAL_Delay(1000);
}

4. Download verification

After the compilation is correct, download it to the development board, you can see that the D1 indicator light keeps flashing when the system is running, and the serial port keeps printing the temperature data read
Insert picture description here

Guess you like

Origin blog.csdn.net/Chuangke_Andy/article/details/108773119