adc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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) 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_MultiModeTypeDef multimode = {0};
  28. ADC_ChannelConfTypeDef sConfig = {0};
  29. /** Common config
  30. */
  31. hadc1.Instance = ADC1;
  32. hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  33. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  34. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  35. hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  36. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  37. hadc1.Init.LowPowerAutoWait = DISABLE;
  38. hadc1.Init.ContinuousConvMode = DISABLE;
  39. hadc1.Init.NbrOfConversion = 1;
  40. hadc1.Init.DiscontinuousConvMode = DISABLE;
  41. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  42. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  43. hadc1.Init.DMAContinuousRequests = DISABLE;
  44. hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  45. hadc1.Init.OversamplingMode = DISABLE;
  46. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  47. {
  48. Error_Handler();
  49. }
  50. /** Configure the ADC multi-mode
  51. */
  52. multimode.Mode = ADC_MODE_INDEPENDENT;
  53. if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
  54. {
  55. Error_Handler();
  56. }
  57. /** Configure Regular Channel
  58. */
  59. sConfig.Channel = ADC_CHANNEL_4;
  60. sConfig.Rank = ADC_REGULAR_RANK_1;
  61. sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
  62. sConfig.SingleDiff = ADC_SINGLE_ENDED;
  63. sConfig.OffsetNumber = ADC_OFFSET_NONE;
  64. sConfig.Offset = 0;
  65. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  66. {
  67. Error_Handler();
  68. }
  69. }
  70. void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
  71. {
  72. GPIO_InitTypeDef GPIO_InitStruct = {0};
  73. if(adcHandle->Instance==ADC1)
  74. {
  75. /* USER CODE BEGIN ADC1_MspInit 0 */
  76. /* USER CODE END ADC1_MspInit 0 */
  77. /* ADC1 clock enable */
  78. __HAL_RCC_ADC_CLK_ENABLE();
  79. __HAL_RCC_GPIOC_CLK_ENABLE();
  80. __HAL_RCC_GPIOA_CLK_ENABLE();
  81. /**ADC1 GPIO Configuration
  82. PC3 ------> ADC1_IN4
  83. PA0 ------> ADC1_IN5
  84. */
  85. GPIO_InitStruct.Pin = BATT_V_Pin;
  86. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
  87. GPIO_InitStruct.Pull = GPIO_NOPULL;
  88. HAL_GPIO_Init(BATT_V_GPIO_Port, &GPIO_InitStruct);
  89. GPIO_InitStruct.Pin = IR_RX_Pin;
  90. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
  91. GPIO_InitStruct.Pull = GPIO_NOPULL;
  92. HAL_GPIO_Init(IR_RX_GPIO_Port, &GPIO_InitStruct);
  93. /* USER CODE BEGIN ADC1_MspInit 1 */
  94. /* USER CODE END ADC1_MspInit 1 */
  95. }
  96. }
  97. void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
  98. {
  99. if(adcHandle->Instance==ADC1)
  100. {
  101. /* USER CODE BEGIN ADC1_MspDeInit 0 */
  102. /* USER CODE END ADC1_MspDeInit 0 */
  103. /* Peripheral clock disable */
  104. __HAL_RCC_ADC_CLK_DISABLE();
  105. /**ADC1 GPIO Configuration
  106. PC3 ------> ADC1_IN4
  107. PA0 ------> ADC1_IN5
  108. */
  109. HAL_GPIO_DeInit(BATT_V_GPIO_Port, BATT_V_Pin);
  110. HAL_GPIO_DeInit(IR_RX_GPIO_Port, IR_RX_Pin);
  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 */
  117. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/