1、借助STM32CubeMX生成系统及外设相关初始化代码。
在以上配置后就可以生成相关初始化代码了。
/* ADC1 init function */
void MX_ADC1_Init(void)
{/* USER CODE BEGIN ADC1_Init 0 *//* USER CODE END ADC1_Init 0 */ADC_ChannelConfTypeDef sConfig = {0};/* USER CODE BEGIN ADC1_Init 1 *//* USER CODE END ADC1_Init 1 *//** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)*/hadc1.Instance = ADC1;hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;hadc1.Init.Resolution = ADC_RESOLUTION_12B;hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;hadc1.Init.LowPowerAutoWait = DISABLE;hadc1.Init.LowPowerAutoPowerOff = DISABLE;hadc1.Init.ContinuousConvMode = ENABLE;hadc1.Init.NbrOfConversion = 1;hadc1.Init.DiscontinuousConvMode = DISABLE;hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;hadc1.Init.DMAContinuousRequests = DISABLE;hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_160CYCLES_5;hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_160CYCLES_5;hadc1.Init.OversamplingMode = DISABLE;hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;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_SAMPLINGTIME_COMMON_1;if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK){Error_Handler();}/* USER CODE BEGIN ADC1_Init 2 *//* USER CODE END ADC1_Init 2 */}
void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};if(adcHandle->Instance==ADC1){/* USER CODE BEGIN ADC1_MspInit 0 *//* USER CODE END ADC1_MspInit 0 *//** Initializes the peripherals clocks*/PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_SYSCLK;if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK){Error_Handler();}/* ADC1 clock enable */__HAL_RCC_ADC_CLK_ENABLE();/* USER CODE BEGIN ADC1_MspInit 1 *//* USER CODE END ADC1_MspInit 1 */}
}void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
{if(adcHandle->Instance==ADC1){/* USER CODE BEGIN ADC1_MspDeInit 0 *//* USER CODE END ADC1_MspDeInit 0 *//* Peripheral clock disable */__HAL_RCC_ADC_CLK_DISABLE();/* USER CODE BEGIN ADC1_MspDeInit 1 *//* USER CODE END ADC1_MspDeInit 1 */}
}
//CPU 温度检测功能代码
float TempDectect()
{float Tmep1 = 0.284;uint32_t aDsUM = 0;uint32_t AdcVol = 0.0;int16_t aDCvALUE = 0;HAL_ADC_Start(&hadc1); //启动ADC转换HAL_ADC_PollForConversion(&hadc1,10); //等待转换完成,10ms表示超时时间aDCvALUE = HAL_ADC_GetValue(&hadc1); //读取ADC转换数据(12位数据)/*温度公式与参数释义:TMP = (TS_CAL2_TEMP - TS_CAL1_TEMP)/(TS_CAL2 - TS_CAL1) * (TS_DATA - TS_CAL1) + TS_CAL1_TEMP;TS_CAL2_TEMP:130°TS_CAL1_TEMP:30°TS_CAL2:0x56A(根据手册得知值存储在flash地址0x0x1FFF75CA - 0x1FFF75CB)需要读出来TS_CAL1:0x40A(根据手册得知值存储在flash地址0x0x1FFF75A8 - 0x1FFF75A9)需要读出来TS_DATA:是由 ADC 转换得到的实际温度传感器输出值*///Tmep1 = (130-30)/(1386-1034); //计算一直为0,所以将其设为恒定值Tmep = (Tmep1 * (aDCvALUE - 1034)) + 30;return Tmep;
}