当前位置:   article > 正文

STM32最小核心板使用HAL库ADC读取MCU温度(使用DMA通道)_内部温度 adc hal_adc_start_dma

内部温度 adc hal_adc_start_dma

STM32自带CPU的温度数据,需要使用ADC去读取。因此在MX创建项目时如图配置:

模块初始化代码如下:

  1. void MX_ADC1_Init(void)
  2. {
  3. /* USER CODE BEGIN ADC1_Init 0 */
  4. /* USER CODE END ADC1_Init 0 */
  5. ADC_ChannelConfTypeDef sConfig = {0};
  6. /* USER CODE BEGIN ADC1_Init 1 */
  7. /* USER CODE END ADC1_Init 1 */
  8. /** Common config
  9. */
  10. hadc1.Instance = ADC1;
  11. hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  12. hadc1.Init.ContinuousConvMode = ENABLE;
  13. hadc1.Init.DiscontinuousConvMode = DISABLE;
  14. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  15. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  16. hadc1.Init.NbrOfConversion = 1;
  17. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  18. {
  19. Error_Handler();
  20. }
  21. /** Configure Regular Channel
  22. */
  23. sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
  24. sConfig.Rank = ADC_REGULAR_RANK_1;
  25. sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
  26. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  27. {
  28. Error_Handler();
  29. }
  30. /* USER CODE BEGIN ADC1_Init 2 */
  31. /* USER CODE END ADC1_Init 2 */
  32. }
  33. void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
  34. {
  35. if(adcHandle->Instance==ADC1)
  36. {
  37. /* USER CODE BEGIN ADC1_MspInit 0 */
  38. /* USER CODE END ADC1_MspInit 0 */
  39. /* ADC1 clock enable */
  40. __HAL_RCC_ADC1_CLK_ENABLE();
  41. /* ADC1 DMA Init */
  42. /* ADC1 Init */
  43. hdma_adc1.Instance = DMA1_Channel1;
  44. hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
  45. hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
  46. hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
  47. hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
  48. hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
  49. hdma_adc1.Init.Mode = DMA_CIRCULAR;
  50. hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
  51. if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
  52. {
  53. Error_Handler();
  54. }
  55. __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
  56. /* USER CODE BEGIN ADC1_MspInit 1 */
  57. /* USER CODE END ADC1_MspInit 1 */
  58. }
  59. }
  60. /
  61. void MX_DMA_Init(void)
  62. {
  63. /* DMA controller clock enable */
  64. __HAL_RCC_DMA1_CLK_ENABLE();
  65. /* DMA interrupt init */
  66. /* DMA1_Channel1_IRQn interrupt configuration */
  67. HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  68. HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  69. }

给出Main的调用

  1. //全局变量
  2. uint32_t CPU_Temp_AD =0;
  3. double CalcCPUTemp(ADC_HandleTypeDef* hadc)
  4. {
  5. double temp = 0;
  6. //需要静态指针保存数据
  7. HAL_ADC_Start_DMA(hadc, (uint32_t *)&CPU_Temp_AD, 1);
  8. HAL_Delay(5);
  9. //采样10次
  10. for(uint8_t a = 0; a < 10; a++)
  11. {
  12. temp += CPU_Temp_AD;
  13. HAL_Delay(2);
  14. }
  15. return (1.43 - (temp/1000/10)) + 25;
  16. }
  17. int main(void)
  18. {
  19. MX_DMA_Init();
  20. MX_ADC1_Init();
  21. while (1) {
  22. //计算获取温度
  23. double CPU_TEMP = CalcCPUTemp(&hadc1);
  24. HAL_Delay(10);
  25. }
  26. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/330762
推荐阅读
相关标签
  

闽ICP备14008679号