adc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. ******************************************************************************
  3. * File Name : ADC.c
  4. * Description : This file provides code for the configuration
  5. * of the ADC instances.
  6. ******************************************************************************
  7. * This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * Copyright (c) 2019 STMicroelectronics International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /* Includes ------------------------------------------------------------------*/
  50. #include "adc.h"
  51. /* USER CODE BEGIN 0 */
  52. /* USER CODE END 0 */
  53. ADC_HandleTypeDef hadc1;
  54. /* ADC1 init function */
  55. void MX_ADC1_Init(void) {
  56. ADC_ChannelConfTypeDef sConfig = { 0 };
  57. /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  58. */
  59. hadc1.Instance = ADC1;
  60. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  61. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  62. hadc1.Init.ScanConvMode = DISABLE;
  63. hadc1.Init.ContinuousConvMode = DISABLE;
  64. hadc1.Init.DiscontinuousConvMode = DISABLE;
  65. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  66. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  67. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  68. hadc1.Init.NbrOfConversion = 1;
  69. hadc1.Init.DMAContinuousRequests = DISABLE;
  70. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  71. if (HAL_ADC_Init(&hadc1) != HAL_OK) {
  72. Error_Handler();
  73. }
  74. /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  75. */
  76. sConfig.Channel = ADC_CHANNEL_9;
  77. sConfig.Rank = 1;
  78. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  79. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
  80. Error_Handler();
  81. }
  82. }
  83. void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle) {
  84. GPIO_InitTypeDef GPIO_InitStruct = { 0 };
  85. if (adcHandle->Instance == ADC1) {
  86. /* USER CODE BEGIN ADC1_MspInit 0 */
  87. /* USER CODE END ADC1_MspInit 0 */
  88. /* ADC1 clock enable */
  89. __HAL_RCC_ADC1_CLK_ENABLE()
  90. ;
  91. __HAL_RCC_GPIOB_CLK_ENABLE()
  92. ;
  93. /**ADC1 GPIO Configuration
  94. PB1 ------> ADC1_IN9
  95. */
  96. GPIO_InitStruct.Pin = THEMP_Pin;
  97. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  98. GPIO_InitStruct.Pull = GPIO_NOPULL;
  99. HAL_GPIO_Init(THEMP_GPIO_Port, &GPIO_InitStruct);
  100. /* USER CODE BEGIN ADC1_MspInit 1 */
  101. /* USER CODE END ADC1_MspInit 1 */
  102. }
  103. }
  104. void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) {
  105. if (adcHandle->Instance == ADC1) {
  106. /* USER CODE BEGIN ADC1_MspDeInit 0 */
  107. /* USER CODE END ADC1_MspDeInit 0 */
  108. /* Peripheral clock disable */
  109. __HAL_RCC_ADC1_CLK_DISABLE();
  110. /**ADC1 GPIO Configuration
  111. PB1 ------> ADC1_IN9
  112. */
  113. HAL_GPIO_DeInit(THEMP_GPIO_Port, THEMP_Pin);
  114. /* USER CODE BEGIN ADC1_MspDeInit 1 */
  115. /* USER CODE END ADC1_MspDeInit 1 */
  116. }
  117. }
  118. /* USER CODE BEGIN 1 */
  119. /* USER CODE END 1 */
  120. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/