summaryrefslogtreecommitdiff
path: root/project/Core/Src/Devices/adc.c
diff options
context:
space:
mode:
authorsowgro <tpoke.ferrari@gmail.com>2025-09-02 14:45:40 -0400
committersowgro <tpoke.ferrari@gmail.com>2025-09-02 14:45:40 -0400
commite3a880051ccf1ba9a16fd9cf031b7386b2533bf1 (patch)
tree96a096b2a668bf7b7929385ebb54c1a3cbd1b9d2 /project/Core/Src/Devices/adc.c
parent0ff718e7cd7159c30636aa323a666ac1af684f63 (diff)
download340-repo-Sowgro-e3a880051ccf1ba9a16fd9cf031b7386b2533bf1.tar.gz
340-repo-Sowgro-e3a880051ccf1ba9a16fd9cf031b7386b2533bf1.tar.bz2
340-repo-Sowgro-e3a880051ccf1ba9a16fd9cf031b7386b2533bf1.zip
Organized project
Diffstat (limited to 'project/Core/Src/Devices/adc.c')
-rw-r--r--project/Core/Src/Devices/adc.c130
1 files changed, 130 insertions, 0 deletions
diff --git a/project/Core/Src/Devices/adc.c b/project/Core/Src/Devices/adc.c
new file mode 100644
index 0000000..549f4bb
--- /dev/null
+++ b/project/Core/Src/Devices/adc.c
@@ -0,0 +1,130 @@
+/*
+ * adc.c
+ *
+ * Created on: Apr 13, 2022
+ * Author: bruce
+ */
+
+#include "adc.h"
+#include "stm32l4xx.h"
+#include "main.h"
+
+ADC_HandleTypeDef hadc1;
+
+/**
+* @brief ADC MSP Initialization
+* This function configures the hardware resources used in this example
+* @param hadc: ADC handle pointer
+* @retval None
+*/
+void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
+{
+ GPIO_InitTypeDef GPIO_InitStruct = {0};
+ RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
+ if(hadc->Instance==ADC1)
+ {
+ /** Initializes the peripherals clock
+ */
+ PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
+ PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI1;
+ PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_HSI;
+ PeriphClkInit.PLLSAI1.PLLSAI1M = 1;
+ PeriphClkInit.PLLSAI1.PLLSAI1N = 8;
+ PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV7;
+ PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
+ PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
+ PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_ADC1CLK;
+ if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
+ {
+ Error_Handler();
+ }
+
+
+ /* Peripheral clock enable */
+ __HAL_RCC_ADC_CLK_ENABLE();
+
+ __HAL_RCC_GPIOA_CLK_ENABLE();
+ /**ADC1 GPIO Configuration
+ PA0 ------> ADC1_IN5
+ */
+ GPIO_InitStruct.Pin = GPIO_PIN_0;
+ GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
+ GPIO_InitStruct.Pull = GPIO_NOPULL;
+ HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+ }
+
+}
+
+/**
+* @brief ADC MSP De-Initialization
+* This function freeze the hardware resources used in this example
+* @param hadc: ADC handle pointer
+* @retval None
+*/
+void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
+{
+ if(hadc->Instance==ADC1)
+ {
+ __HAL_RCC_ADC_CLK_DISABLE();
+ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0);
+ }
+
+}
+
+void ADC_Init () {
+ ADC_MultiModeTypeDef multimode = {0};
+ ADC_ChannelConfTypeDef sConfig = {0};
+
+ /** Common config
+ */
+ hadc1.Instance = ADC1;
+ hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
+ hadc1.Init.Resolution = ADC_RESOLUTION_12B;
+ hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
+ hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
+ hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
+ hadc1.Init.LowPowerAutoWait = DISABLE;
+ hadc1.Init.ContinuousConvMode = DISABLE;
+ hadc1.Init.NbrOfConversion = 1;
+ hadc1.Init.DiscontinuousConvMode = DISABLE;
+ hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
+ hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
+ hadc1.Init.DMAContinuousRequests = DISABLE;
+ hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
+ hadc1.Init.OversamplingMode = DISABLE;
+ if (HAL_ADC_Init(&hadc1) != HAL_OK)
+ {
+ Error_Handler();
+ }
+
+ /** Configure the ADC multi-mode
+ */
+ multimode.Mode = ADC_MODE_INDEPENDENT;
+ if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
+ {
+ Error_Handler();
+ }
+
+
+ /** Configure Regular Channel
+ */
+ sConfig.Channel = ADC_CHANNEL;
+ sConfig.Rank = ADC_REGULAR_RANK_1;
+ sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
+ sConfig.SingleDiff = ADC_SINGLE_ENDED;
+ sConfig.OffsetNumber = ADC_OFFSET_NONE;
+ sConfig.Offset = 0;
+ if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
+ {
+ Error_Handler();
+ }
+}
+
+void ADC_Start () {
+ HAL_ADC_Start (&hadc1);
+}
+
+uint32_t ADC_Read_Value_Blocking () {
+ HAL_ADC_PollForConversion (&hadc1, 10);
+ return HAL_ADC_GetValue (&hadc1);
+}