|
| 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 | + |
| 23 | +/* USER CODE BEGIN 0 */ |
| 24 | + |
| 25 | +/* USER CODE END 0 */ |
| 26 | + |
| 27 | +ADC_HandleTypeDef hadc1; |
| 28 | + |
| 29 | +/* ADC1 init function */ |
| 30 | +void MX_ADC1_Init(void) |
| 31 | +{ |
| 32 | + |
| 33 | + /* USER CODE BEGIN ADC1_Init 0 */ |
| 34 | + |
| 35 | + /* USER CODE END ADC1_Init 0 */ |
| 36 | + |
| 37 | + ADC_ChannelConfTypeDef sConfig = {0}; |
| 38 | + |
| 39 | + /* USER CODE BEGIN ADC1_Init 1 */ |
| 40 | + |
| 41 | + /* USER CODE END ADC1_Init 1 */ |
| 42 | + |
| 43 | + /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) |
| 44 | + */ |
| 45 | + hadc1.Instance = ADC1; |
| 46 | + hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; |
| 47 | + hadc1.Init.Resolution = ADC_RESOLUTION_12B; |
| 48 | + hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; |
| 49 | + hadc1.Init.ContinuousConvMode = DISABLE; |
| 50 | + hadc1.Init.DiscontinuousConvMode = DISABLE; |
| 51 | + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; |
| 52 | + hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; |
| 53 | + hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; |
| 54 | + hadc1.Init.NbrOfConversion = 1; |
| 55 | + hadc1.Init.DMAContinuousRequests = DISABLE; |
| 56 | + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; |
| 57 | + if (HAL_ADC_Init(&hadc1) != HAL_OK) |
| 58 | + { |
| 59 | + Error_Handler(); |
| 60 | + } |
| 61 | + |
| 62 | + /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. |
| 63 | + */ |
| 64 | + sConfig.Channel = ADC_CHANNEL_5; |
| 65 | + sConfig.Rank = ADC_REGULAR_RANK_1; |
| 66 | + sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; |
| 67 | + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) |
| 68 | + { |
| 69 | + Error_Handler(); |
| 70 | + } |
| 71 | + /* USER CODE BEGIN ADC1_Init 2 */ |
| 72 | + |
| 73 | + /* USER CODE END ADC1_Init 2 */ |
| 74 | + |
| 75 | +} |
| 76 | + |
| 77 | +void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle) |
| 78 | +{ |
| 79 | + |
| 80 | + GPIO_InitTypeDef GPIO_InitStruct = {0}; |
| 81 | + if(adcHandle->Instance==ADC1) |
| 82 | + { |
| 83 | + /* USER CODE BEGIN ADC1_MspInit 0 */ |
| 84 | + |
| 85 | + /* USER CODE END ADC1_MspInit 0 */ |
| 86 | + /* ADC1 clock enable */ |
| 87 | + __HAL_RCC_ADC1_CLK_ENABLE(); |
| 88 | + |
| 89 | + __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 90 | + /**ADC1 GPIO Configuration |
| 91 | + PA5 ------> ADC1_IN5 |
| 92 | + */ |
| 93 | + GPIO_InitStruct.Pin = GPIO_PIN_5; |
| 94 | + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
| 95 | + GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 96 | + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 97 | + |
| 98 | + /* USER CODE BEGIN ADC1_MspInit 1 */ |
| 99 | + |
| 100 | + /* USER CODE END ADC1_MspInit 1 */ |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) |
| 105 | +{ |
| 106 | + |
| 107 | + if(adcHandle->Instance==ADC1) |
| 108 | + { |
| 109 | + /* USER CODE BEGIN ADC1_MspDeInit 0 */ |
| 110 | + |
| 111 | + /* USER CODE END ADC1_MspDeInit 0 */ |
| 112 | + /* Peripheral clock disable */ |
| 113 | + __HAL_RCC_ADC1_CLK_DISABLE(); |
| 114 | + |
| 115 | + /**ADC1 GPIO Configuration |
| 116 | + PA5 ------> ADC1_IN5 |
| 117 | + */ |
| 118 | + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5); |
| 119 | + |
| 120 | + /* USER CODE BEGIN ADC1_MspDeInit 1 */ |
| 121 | + |
| 122 | + /* USER CODE END ADC1_MspDeInit 1 */ |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/* USER CODE BEGIN 1 */ |
| 127 | + |
| 128 | +/* USER CODE END 1 */ |
0 commit comments