adc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file adc.c
  5. * @brief This file provides code for the configuration
  6. * of the ADC instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2022 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "adc.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. ADC_HandleTypeDef hadc1;
  25. /* ADC1 init function */
  26. void MX_ADC1_Init(void)
  27. {
  28. /* USER CODE BEGIN ADC1_Init 0 */
  29. /* USER CODE END ADC1_Init 0 */
  30. ADC_ChannelConfTypeDef sConfig = {0};
  31. /* USER CODE BEGIN ADC1_Init 1 */
  32. /* USER CODE END ADC1_Init 1 */
  33. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  34. */
  35. hadc1.Instance = ADC1;
  36. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  37. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  38. hadc1.Init.ScanConvMode = DISABLE;
  39. hadc1.Init.ContinuousConvMode = DISABLE;
  40. hadc1.Init.DiscontinuousConvMode = DISABLE;
  41. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  42. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  43. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  44. hadc1.Init.NbrOfConversion = 1;
  45. hadc1.Init.DMAContinuousRequests = DISABLE;
  46. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  47. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  48. {
  49. Error_Handler();
  50. }
  51. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  52. */
  53. sConfig.Channel = ADC_CHANNEL_4;
  54. sConfig.Rank = 1;
  55. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  56. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  57. {
  58. Error_Handler();
  59. }
  60. /* USER CODE BEGIN ADC1_Init 2 */
  61. /* USER CODE END ADC1_Init 2 */
  62. }
  63. void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
  64. {
  65. GPIO_InitTypeDef GPIO_InitStruct = {0};
  66. if(adcHandle->Instance==ADC1)
  67. {
  68. /* USER CODE BEGIN ADC1_MspInit 0 */
  69. /* USER CODE END ADC1_MspInit 0 */
  70. /* ADC1 clock enable */
  71. __HAL_RCC_ADC1_CLK_ENABLE();
  72. __HAL_RCC_GPIOA_CLK_ENABLE();
  73. /**ADC1 GPIO Configuration
  74. PA4 ------> ADC1_IN4
  75. */
  76. GPIO_InitStruct.Pin = Themp_Pin;
  77. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  78. GPIO_InitStruct.Pull = GPIO_NOPULL;
  79. HAL_GPIO_Init(Themp_GPIO_Port, &GPIO_InitStruct);
  80. /* USER CODE BEGIN ADC1_MspInit 1 */
  81. /* USER CODE END ADC1_MspInit 1 */
  82. }
  83. }
  84. void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
  85. {
  86. if(adcHandle->Instance==ADC1)
  87. {
  88. /* USER CODE BEGIN ADC1_MspDeInit 0 */
  89. /* USER CODE END ADC1_MspDeInit 0 */
  90. /* Peripheral clock disable */
  91. __HAL_RCC_ADC1_CLK_DISABLE();
  92. /**ADC1 GPIO Configuration
  93. PA4 ------> ADC1_IN4
  94. */
  95. HAL_GPIO_DeInit(Themp_GPIO_Port, Themp_Pin);
  96. /* USER CODE BEGIN ADC1_MspDeInit 1 */
  97. /* USER CODE END ADC1_MspDeInit 1 */
  98. }
  99. }
  100. /* USER CODE BEGIN 1 */
  101. /* USER CODE END 1 */