STM32 internal temperature sensor experiment summary
[Copy link]
1.STM32有一个内部的温度传感器,可以用来测量CPU及周围的温度(TA)。
2.该温度传感器在内部和ADCx_IN16输入通道相连接,此通道把传感器输出的电压转换成数字值。
3.温度传感器模拟输入推荐采样时间是17.1μs。
4.STM32的内部温度传感器支持的温度范围为:-40~125度。精度比较差,为±1.5℃左右。
内部温度传感器更适合于检测温度的变化,而不是测量绝对温度。
如果需要测量绝度温度,应该使用一个外部温度传感器。
Next, we will start to configure the temperature value inside the chip: [To put it bluntly, the internal temperature sensor is still part of the ADC, so to understand the internal sensor, you must understand the configuration of the ADC]
void Adc_Temperature_Init(void)
{
//第一步:开启PA时钟以及ADC1的通道16的时钟。
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1,ENABLE);
//第二步:复位ADC1,并且设置ADC1的分频因子,让ADC1工作在多少Mhz。
// 最高不能超过14MHz.那么我们就6分频,让其工作在12Mhz,因为ADC挂载在
// APB2总线下,也就对应的是PCLK2的时钟,72/6=12;
RCC_ADCCLKConfig(RCC_PCLK2_Div6);//
ADC_DeInit(ADC1);//ADC1去初始化
//第三步:初始化ADC1的相关参数,设置ADC1的工作模式以及规则序列的相关信息
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //工作在单一模式而不是循环模式
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//数据右对齐
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//转换是没有外部出发启动的。是同过软件转换触发启动
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式
ADC_InitStructure.ADC_NbrOfChannel = 1; //顺序进行规则转换的ADC1通道数目。这里只有一个通道。
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //扫描模式关闭 。扫描模式是用来扫描一组通道的这里我们只用到了一个通道,也就用不到扫描模式,所以失能
ADC_Init(ADC1,&ADC_InitStructure);//ADC1的初始化。
ADC_TempSensorVrefintCmd(ENABLE);//这一步不能忘记了。AD通道方面的配置的差不多了,就要开启内部温度传感器。
//第四步:使能ADC1并且校准ADC1,【校准之前要进行去校准,等待去校准完成后再进行校准】
ADC_Cmd(ADC1,ENABLE);
ADC_GetResetCalibrationStatus(ADC1);//ADC1的去校准
*while(ADC_GetResetCalibrationStatus(ADC1)); //等待去校准完成。*//当没玩成校准时是1,在循环内死这呢。等待脚注完成,ADC->CR2中的位3就变成0,就跳出循环。哈哈
ADC_StartCalibration(ADC1);//ADC1开始校准
while(ADC_GetCalibrationStatus(ADC1));//等待校准完成
}
ps: [Italic part above: while(ADC_GetResetCalibrationStatus(ADC1)); //Wait for calibration to complete. ]
RSTCAL: Reset calibration
This bit is set by software and cleared by hardware. This bit will be cleared after the calibration register is initialized.
0: Calibration register initialized
1: Initialize calibration register
Note: If RSTCAL is set while conversion is in progress, clearing the calibration register requires additional cycles
//Get AD conversion value function
u16 Get_Adc(u8 ch)
{
//Step 5: Configure the working channel parameters, set the specified number of ADC regular channels, one sequence, and a sampling time of 239.5 cycles
ADC_RegularChannelConfig(ADC1,ch,ADC_SampleTime_239Cycles5); //ADC1, ADC channel 16, one sequence, and a sampling time of 239.5 cycles
//Step 6: Enable software conversion function
ADC_SoftwareStartConvCmd(ADC1,ENABLE); //Enable software conversion function of ADC1
while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC) != RESET); //Wait for conversion to complete
return ADC_GetConversionValue(ADC1); //Return the converted AD value.
}
The sample value of the ADC internal temperature sensor is obtained. The value is averaged 10 times for more accuracy.
u16 T_Get_Temp(void)
{
u16 temp_val=0;
u8 t;
for(t=0;t<10;t++)
{
temp_val+=Get_Adc(ADC_Channel_16); //TampSensor
delay_ms(5);
}
return temp_val/10;
}
//Get the conversion average value of channel ch. Take times and then calculate the average.
u16 T_Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t<times;t++)
{
temp_val+=Get_Adc(ch);
delay_ms(5);
}
return temp_val/times;
}
//Get the temperature value. The temperature value is expanded by 100 times, unit: Celsius.
short Get_Temprate(void) //Get the temperature value of the internal temperature sensor
{
u32 adcx;
short result;
double temperate;
adcx=T_Get_Adc_Average(ADC_Channel_16,20); //Channel 16, read 20 times
temperate=(float)adcx*(3.3/4096); //The voltage value obtained.
temperate=(1.43-temperate)/0.0043+25; //Convert to temperature value
result=temperate*=100; //Enlarge 100 times. It is better to display on LCD
return result;
}
|