adc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_MultiModeTypeDef multimode = {0};
  31. ADC_ChannelConfTypeDef sConfig = {0};
  32. /* USER CODE BEGIN ADC1_Init 1 */
  33. /* USER CODE END ADC1_Init 1 */
  34. /** Common config
  35. */
  36. hadc1.Instance = ADC1;
  37. hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  38. hadc1.Init.Resolution = ADC_RESOLUTION_16B;
  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.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
  48. hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  49. hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
  50. hadc1.Init.OversamplingMode = DISABLE;
  51. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  52. {
  53. Error_Handler();
  54. }
  55. /** Configure the ADC multi-mode
  56. */
  57. multimode.Mode = ADC_MODE_INDEPENDENT;
  58. if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
  59. {
  60. Error_Handler();
  61. }
  62. /** Configure Regular Channel
  63. */
  64. sConfig.Channel = ADC_CHANNEL_3;
  65. sConfig.Rank = ADC_REGULAR_RANK_1;
  66. sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  67. sConfig.SingleDiff = ADC_DIFFERENTIAL_ENDED;
  68. sConfig.OffsetNumber = ADC_OFFSET_NONE;
  69. sConfig.Offset = 0;
  70. sConfig.OffsetSignedSaturation = DISABLE;
  71. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  72. {
  73. Error_Handler();
  74. }
  75. /* USER CODE BEGIN ADC1_Init 2 */
  76. /* USER CODE END ADC1_Init 2 */
  77. }
  78. void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
  79. {
  80. GPIO_InitTypeDef GPIO_InitStruct = {0};
  81. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  82. if(adcHandle->Instance==ADC1)
  83. {
  84. /* USER CODE BEGIN ADC1_MspInit 0 */
  85. /* USER CODE END ADC1_MspInit 0 */
  86. /** Initializes the peripherals clock
  87. */
  88. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  89. PeriphClkInitStruct.PLL2.PLL2M = 4;
  90. PeriphClkInitStruct.PLL2.PLL2N = 9;
  91. PeriphClkInitStruct.PLL2.PLL2P = 2;
  92. PeriphClkInitStruct.PLL2.PLL2Q = 2;
  93. PeriphClkInitStruct.PLL2.PLL2R = 2;
  94. PeriphClkInitStruct.PLL2.PLL2RGE = RCC_PLL2VCIRANGE_3;
  95. PeriphClkInitStruct.PLL2.PLL2VCOSEL = RCC_PLL2VCOMEDIUM;
  96. PeriphClkInitStruct.PLL2.PLL2FRACN = 3072;
  97. PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_PLL2;
  98. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  99. {
  100. Error_Handler();
  101. }
  102. /* ADC1 clock enable */
  103. __HAL_RCC_ADC12_CLK_ENABLE();
  104. __HAL_RCC_GPIOA_CLK_ENABLE();
  105. __HAL_RCC_GPIOC_CLK_ENABLE();
  106. /**ADC1 GPIO Configuration
  107. PA6 ------> ADC1_INP3
  108. PA7 ------> ADC1_INN3
  109. PC4 ------> ADC1_INP4
  110. PC5 ------> ADC1_INN4
  111. */
  112. GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  113. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  114. GPIO_InitStruct.Pull = GPIO_NOPULL;
  115. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  116. GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5;
  117. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  118. GPIO_InitStruct.Pull = GPIO_NOPULL;
  119. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  120. /* USER CODE BEGIN ADC1_MspInit 1 */
  121. /* USER CODE END ADC1_MspInit 1 */
  122. }
  123. }
  124. void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
  125. {
  126. if(adcHandle->Instance==ADC1)
  127. {
  128. /* USER CODE BEGIN ADC1_MspDeInit 0 */
  129. /* USER CODE END ADC1_MspDeInit 0 */
  130. /* Peripheral clock disable */
  131. __HAL_RCC_ADC12_CLK_DISABLE();
  132. /**ADC1 GPIO Configuration
  133. PA6 ------> ADC1_INP3
  134. PA7 ------> ADC1_INN3
  135. PC4 ------> ADC1_INP4
  136. PC5 ------> ADC1_INN4
  137. */
  138. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_6|GPIO_PIN_7);
  139. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_4|GPIO_PIN_5);
  140. /* USER CODE BEGIN ADC1_MspDeInit 1 */
  141. /* USER CODE END ADC1_MspDeInit 1 */
  142. }
  143. }
  144. /* USER CODE BEGIN 1 */
  145. /* USER CODE END 1 */