在看STM32的AD部分时,发现内置了温度采集功能,
拿来用用,监测下芯片温度蛮不错的。
根据用户手册,需要通过ADC1的第16通道来采集,然后根据计算公式换算成温度,软件实现分三步:
1) 初始化设置:
void ADC_HardInit(void)
{
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel14 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
}
2) AD采集函数:
u8 ADC_GetChannel(u8 channel, u16* value)
{
switch (channel)
{
case 0x16:
channel = ADC_Channel_16;
ADC_TempSensorVrefintCmd(ENABLE);
break;
case 0x17:
channel = ADC_Channel_17;
break;
default:
channel = ADC_Channel_16;
break;
}
/* ADC1 regular channel14 configuration */
ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_55Cycles5);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
ADC_Delay(2);
*value = ADC_GetConversionValue(ADC1);
if(channel == 0x16)
{
ADC_TempSensorVrefintCmd(DISABLE);
}
return 0;
}
3) 温度还原计算:
u16 value;
CPU_FP32 voltage;
if(!ADC_GetChannel(0x16, &value))
{
/*Supported Temperature Range: –40 to 125 degrees
Precision: ± 1.5 °C */
/*Temperature (in °C) = {(V25 - VSENSE) / Avg_Slope} + 25.
Avg_Slope = 4.3 mV/°C
V25 = 1.43 V-------注:这里应用的全为典型值,在datasheet上可以查取。
*/
voltage = value*3.3/0x1000;
Shell_OutTXT("voltage = %.2f V\n",voltage);
voltage = 25 + ((1.43 - voltage)/0.0043);
Shell_OutTXT("STM32 tempsensor = %.2f °C\n",voltage);
}
else
{
Shell_OutTXT("tempsensor Fail. \n");
}
我测出来的结果为31度左右,(办公室环境)
用户377235 2013-8-1 14:36
用户156476 2008-12-24 23:06