STM32F40x 内部温度获取

#include "ADC.h"


//user guide
// in board init, call function: adc_init();
// get_internal_temperture(); get tempertare, uinit℃


static void AdcPinComInit(void)
{
ADC_CommonInitTypeDef   ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);

ADC_InitStructure.ADC_Mode = ADC_DualMode_RegSimult;
ADC_InitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_InitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_InitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_6Cycles;
ADC_CommonInit(&ADC_InitStructure);
}


static void Adc1ChxConfig(void)
{
  ADC_InitTypeDef ADC_InitStructure;


ADC_TempSensorVrefintCmd(ENABLE);
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;
  ADC_Init(ADC1, &ADC_InitStructure);

ADC_ContinuousModeCmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
}


void adc_init(void)
{
AdcPinComInit();
Adc1ChxConfig();
}


uint16_t get_internal_temperture(void)
{
uint16_t temp_adc_value;
uint16_t temperature;

ADC_TempSensorVrefintCmd(ENABLE);
ADC_RegularChannelConfig(ADC1, ADC_Channel_TempSensor, 1, ADC_SampleTime_15Cycles);
ADC_SoftwareStartConv(ADC1);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));

temp_adc_value = ADC_GetConversionValue(ADC1);

// T = (Vsense -V15)/AVG_Slope +25 ,
//Vsense =  (adc_value *3.3)/4096, 12bit ADC
// V25 = 0.76,AVG_Slope = 2.5mV/℃
temperature = ((temp_adc_value * 3.3)/4096 - 0.76) *1000/2.5 + 25 ;
return temperature;

}


实际测量的值为40℃左右。


































































你可能感兴趣的:(MCU)