adc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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) 2024 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. /** Common config
  34. */
  35. hadc1.Instance = ADC1;
  36. hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  37. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  38. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  39. hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  40. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  41. hadc1.Init.LowPowerAutoWait = DISABLE;
  42. hadc1.Init.ContinuousConvMode = DISABLE;
  43. hadc1.Init.NbrOfConversion = 1;
  44. hadc1.Init.DiscontinuousConvMode = DISABLE;
  45. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  46. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  47. hadc1.Init.DMAContinuousRequests = DISABLE;
  48. hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  49. hadc1.Init.OversamplingMode = DISABLE;
  50. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  51. {
  52. Error_Handler();
  53. }
  54. /** Configure Regular Channel
  55. */
  56. sConfig.Channel = ADC_CHANNEL_6;
  57. sConfig.Rank = ADC_REGULAR_RANK_1;
  58. sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
  59. sConfig.SingleDiff = ADC_SINGLE_ENDED;
  60. sConfig.OffsetNumber = ADC_OFFSET_NONE;
  61. sConfig.Offset = 0;
  62. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  63. {
  64. Error_Handler();
  65. }
  66. /* USER CODE BEGIN ADC1_Init 2 */
  67. /* USER CODE END ADC1_Init 2 */
  68. }
  69. void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
  70. {
  71. GPIO_InitTypeDef GPIO_InitStruct = {0};
  72. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  73. if(adcHandle->Instance==ADC1)
  74. {
  75. /* USER CODE BEGIN ADC1_MspInit 0 */
  76. /* USER CODE END ADC1_MspInit 0 */
  77. /** Initializes the peripherals clock
  78. */
  79. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  80. PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_SYSCLK;
  81. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  82. {
  83. Error_Handler();
  84. }
  85. /* ADC1 clock enable */
  86. __HAL_RCC_ADC_CLK_ENABLE();
  87. __HAL_RCC_GPIOA_CLK_ENABLE();
  88. /**ADC1 GPIO Configuration
  89. PA1 ------> ADC1_IN6
  90. */
  91. GPIO_InitStruct.Pin = GPIO_PIN_1;
  92. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
  93. GPIO_InitStruct.Pull = GPIO_NOPULL;
  94. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  95. /* USER CODE BEGIN ADC1_MspInit 1 */
  96. /* USER CODE END ADC1_MspInit 1 */
  97. }
  98. }
  99. void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
  100. {
  101. if(adcHandle->Instance==ADC1)
  102. {
  103. /* USER CODE BEGIN ADC1_MspDeInit 0 */
  104. /* USER CODE END ADC1_MspDeInit 0 */
  105. /* Peripheral clock disable */
  106. __HAL_RCC_ADC_CLK_DISABLE();
  107. /**ADC1 GPIO Configuration
  108. PA1 ------> ADC1_IN6
  109. */
  110. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1);
  111. /* USER CODE BEGIN ADC1_MspDeInit 1 */
  112. /* USER CODE END ADC1_MspDeInit 1 */
  113. }
  114. }
  115. /* USER CODE BEGIN 1 */
  116. /* USER CODE END 1 */