1
0

adc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. ******************************************************************************
  3. * File Name : ADC.c
  4. * Description : This file provides code for the configuration
  5. * of the ADC instances.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "adc.h"
  21. /* USER CODE BEGIN 0 */
  22. /* USER CODE END 0 */
  23. ADC_HandleTypeDef hadc1;
  24. /* ADC1 init function */
  25. void MX_ADC1_Init(void)
  26. {
  27. ADC_ChannelConfTypeDef sConfig = {0};
  28. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  29. */
  30. hadc1.Instance = ADC1;
  31. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  32. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  33. hadc1.Init.ScanConvMode = DISABLE;
  34. hadc1.Init.ContinuousConvMode = DISABLE;
  35. hadc1.Init.DiscontinuousConvMode = DISABLE;
  36. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  37. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  38. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  39. hadc1.Init.NbrOfConversion = 1;
  40. hadc1.Init.DMAContinuousRequests = DISABLE;
  41. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  42. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  43. {
  44. Error_Handler();
  45. }
  46. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  47. */
  48. sConfig.Channel = ADC_CHANNEL_4;
  49. sConfig.Rank = 1;
  50. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  51. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  52. {
  53. Error_Handler();
  54. }
  55. }
  56. void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
  57. {
  58. GPIO_InitTypeDef GPIO_InitStruct = {0};
  59. if(adcHandle->Instance==ADC1)
  60. {
  61. /* USER CODE BEGIN ADC1_MspInit 0 */
  62. /* USER CODE END ADC1_MspInit 0 */
  63. /* ADC1 clock enable */
  64. __HAL_RCC_ADC1_CLK_ENABLE();
  65. __HAL_RCC_GPIOA_CLK_ENABLE();
  66. /**ADC1 GPIO Configuration
  67. PA4 ------> ADC1_IN4
  68. */
  69. GPIO_InitStruct.Pin = GPIO_PIN_4;
  70. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  71. GPIO_InitStruct.Pull = GPIO_NOPULL;
  72. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  73. /* USER CODE BEGIN ADC1_MspInit 1 */
  74. /* USER CODE END ADC1_MspInit 1 */
  75. }
  76. }
  77. void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
  78. {
  79. if(adcHandle->Instance==ADC1)
  80. {
  81. /* USER CODE BEGIN ADC1_MspDeInit 0 */
  82. /* USER CODE END ADC1_MspDeInit 0 */
  83. /* Peripheral clock disable */
  84. __HAL_RCC_ADC1_CLK_DISABLE();
  85. /**ADC1 GPIO Configuration
  86. PA4 ------> ADC1_IN4
  87. */
  88. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4);
  89. /* USER CODE BEGIN ADC1_MspDeInit 1 */
  90. /* USER CODE END ADC1_MspDeInit 1 */
  91. }
  92. }
  93. /* USER CODE BEGIN 1 */
  94. /* USER CODE END 1 */
  95. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/