adc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. ******************************************************************************
  3. * @file adc.c
  4. * @brief This file provides code for the configuration
  5. * of the ADC instances.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  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. /** Common config
  29. */
  30. hadc1.Instance = ADC1;
  31. hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  32. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  33. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  34. hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  35. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  36. hadc1.Init.LowPowerAutoWait = DISABLE;
  37. hadc1.Init.ContinuousConvMode = DISABLE;
  38. hadc1.Init.NbrOfConversion = 1;
  39. hadc1.Init.DiscontinuousConvMode = DISABLE;
  40. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  41. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  42. hadc1.Init.DMAContinuousRequests = DISABLE;
  43. hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  44. hadc1.Init.OversamplingMode = DISABLE;
  45. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  46. {
  47. Error_Handler();
  48. }
  49. /** Configure Regular Channel
  50. */
  51. sConfig.Channel = ADC_CHANNEL_14;
  52. sConfig.Rank = ADC_REGULAR_RANK_1;
  53. sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
  54. sConfig.SingleDiff = ADC_SINGLE_ENDED;
  55. sConfig.OffsetNumber = ADC_OFFSET_NONE;
  56. sConfig.Offset = 0;
  57. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  58. {
  59. Error_Handler();
  60. }
  61. }
  62. void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
  63. {
  64. GPIO_InitTypeDef GPIO_InitStruct = {0};
  65. if(adcHandle->Instance==ADC1)
  66. {
  67. /* USER CODE BEGIN ADC1_MspInit 0 */
  68. /* USER CODE END ADC1_MspInit 0 */
  69. /* ADC1 clock enable */
  70. __HAL_RCC_ADC_CLK_ENABLE();
  71. __HAL_RCC_GPIOC_CLK_ENABLE();
  72. /**ADC1 GPIO Configuration
  73. PC5 ------> ADC1_IN14
  74. */
  75. GPIO_InitStruct.Pin = RFID_RF_IN_Pin;
  76. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  77. GPIO_InitStruct.Pull = GPIO_NOPULL;
  78. HAL_GPIO_Init(RFID_RF_IN_GPIO_Port, &GPIO_InitStruct);
  79. /* ADC1 interrupt Init */
  80. HAL_NVIC_SetPriority(ADC1_IRQn, 5, 0);
  81. HAL_NVIC_EnableIRQ(ADC1_IRQn);
  82. /* USER CODE BEGIN ADC1_MspInit 1 */
  83. /* USER CODE END ADC1_MspInit 1 */
  84. }
  85. }
  86. void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
  87. {
  88. if(adcHandle->Instance==ADC1)
  89. {
  90. /* USER CODE BEGIN ADC1_MspDeInit 0 */
  91. /* USER CODE END ADC1_MspDeInit 0 */
  92. /* Peripheral clock disable */
  93. __HAL_RCC_ADC_CLK_DISABLE();
  94. /**ADC1 GPIO Configuration
  95. PC5 ------> ADC1_IN14
  96. */
  97. HAL_GPIO_DeInit(RFID_RF_IN_GPIO_Port, RFID_RF_IN_Pin);
  98. /* ADC1 interrupt Deinit */
  99. HAL_NVIC_DisableIRQ(ADC1_IRQn);
  100. /* USER CODE BEGIN ADC1_MspDeInit 1 */
  101. /* USER CODE END ADC1_MspDeInit 1 */
  102. }
  103. }
  104. /* USER CODE BEGIN 1 */
  105. /* USER CODE END 1 */
  106. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/