Development Environment:
IDE: MKD 5.38a
STM32CubeMX: V6.12.0
Development board: STM32H7S78-DK development board
MCU: STM32H7S7L8H6H
1 Working principle of internal temperature sensor
The STM32H7S has an internal temperature sensor that can be used to measure the temperature of the CPU and its surroundings. The temperature sensor is connected to the internal V SENSE input channel, which converts the voltage output by the sensor into a digital value. The internal temperature sensor of the STM32H7S supports a temperature range of -40 to 125 degrees.
Figure 1-1 Temperature sensor architecture
The output voltage of the temperature sensor varies linearly with temperature. Due to process differences, the offset of this linear function depends on the individual chips (the temperature variation between chips can be up to 45°C).
The internal temperature sensor is more suitable for applications where the temperature variable rather than the absolute temperature is being measured. If a precise temperature reading is required, an external temperature sensor should be used.
The use of the STM32 internal temperature sensor is very simple. You only need to set the internal ADC and activate its internal channel. Next, we will introduce two places related to the temperature sensor setting.
First, 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 TSEN bit (bit23) of ADC_CCR. Setting this bit to 1 enables the internal temperature sensor.
Secondly, the internal temperature sensor of STM32 is fixedly connected to the ADC channel V SENSE . Therefore, after setting up the ADC, we only need to read the value of V SENSE , which is the voltage value returned by the temperature sensor. Based on this value, we can calculate the current temperature. STM32 has a built-in temperature sensor, and the voltage of the temperature sensor can be read through the V SENSE channel. A calculation formula is given:
- TS_CAL1 is the calibration value of the temperature sensor at 30°C and is fixedly stored in the chip at the following two addresses: 0x08FF F814 - 0x08FF F815 (16 bits).
- TS_CAL2 is the calibration value of the temperature sensor at 130°C and is fixedly stored in the chip at the two addresses 0x08FF F818 - 0x08FF F819 (16 bits).
- TS_DATA: Current temperature sensor conversion value read by ADC1 channel V SENSE .
Table 1-1 Temperature sensor calibration values
Now, we can summarize the steps of using the STM32 internal temperature sensor as follows:
1 ) Set up ADC and turn on the internal temperature sensor.
As for how to set up ADC, we have already introduced it in the previous section. We use the same settings as in the previous section. The difference is that the temperature sensor in the previous section reads the value of the external channel, while the internal temperature sensor is equivalent to connecting the channel port to the internal temperature sensor. The function is as follows.
HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef *hadc, const ADC_ChannelConfTypeDef *pConfig)
2 ) Read the AD value of channel V SENSE and calculate the result.
After setting, we can read the voltage value of the temperature sensor, and then use the above formula to calculate the temperature value.
2 Internal temperature sensor reading implementation
2.1 STM32Cube generates a project
The STM32H7S channel V SENSE is connected to the internal temperature sensor, so we only need to configure the corresponding parameters. We configure it based on the serial port example.
Open the project, open the Analog option, and configure the ADC parameters.
Enable continuous conversion mode (Continuous Conversion Mode). Set the conversion period. Other settings are default.
Then generate the project.
2.2 Internal temperature sensor specific code
The internal temperature data acquisition is the same as the common ADC data acquisition, and its programming process is:
1. Hardware initialization;
2. Parameter configuration of serial port, ADC, etc.
3. Calibrate ADC and process ADC data;
The main function is simple:
/**
* [url=home.php?mod=space&uid=159083]@brief[/url] The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
float ADC_ConvertedValueLocal;
uint32_t ADC_ConvertedValue;
uint16_t ts_cal1 = 0;
uint16_t ts_cal2 = 0;
float temp = 0;
/* USER CODE END 1 */
/* MPU Configuration--------------------------------------------------------*/
MPU_Config();
/* 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_UART4_Init();
MX_ADC1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart4, (uint8_t *)&RxBuffer, 1);
HAL_ADC_Start(&hadc1); //开启
ts_cal1 = *(volatile uint16_t*)(0x08FFF814);
ts_cal2 = *(volatile uint16_t*)(0x08FFF818);
temp = (float)((130.0f - 30.0f) / (ts_cal2-ts_cal1));
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_ADC_PollForConversion(&hadc1,10); //等待转换完成
if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))
{
ADC_ConvertedValue = HAL_ADC_GetValue(&hadc1);
ADC_ConvertedValueLocal =(float) ADC_ConvertedValue/(float)4096*3.3; // 读取转换的AD值
printf("The current AD value = 0x%04X \r\n", ADC_ConvertedValue);
printf("The current AD value = %f V \r\n",ADC_ConvertedValueLocal); //实际电压值
printf("temperture =%f\r\n\r\n",((ADC_ConvertedValue - ts_cal1) * temp + 30));
}
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
HAL_Delay(500);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
The above is to read the temperature by querying, which can also be obtained by interruption and DMA.
3 Experimental Phenomena
After compiling the program, download it to the board. Through the serial port assistant, you can see that the temperature value is output in the receiving area.