169 lines
4.2 KiB
C
169 lines
4.2 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file adc.c
|
|
* @brief This file provides code for the configuration
|
|
* of the ADC instances.
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* Copyright (c) 2024 STMicroelectronics.
|
|
* All rights reserved.
|
|
*
|
|
* This software is licensed under terms that can be found in the LICENSE file
|
|
* in the root directory of this software component.
|
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
/* USER CODE END Header */
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "adc.h"
|
|
|
|
/* USER CODE BEGIN 0 */
|
|
#include "stm32f4xx_hal_adc.h"
|
|
|
|
#define ADC1_CHANNEL_CNT 1
|
|
#define ADC1_CHANNEL_FRE 3
|
|
uint16_t adc1_val_buf[ADC1_CHANNEL_CNT * ADC1_CHANNEL_FRE];
|
|
// uint32_t adc1_aver_val[ADC1_CHANNEL_CNT] = {0};
|
|
// uint16_t value[ADC1_CHANNEL_CNT] = {0};
|
|
/* USER CODE END 0 */
|
|
|
|
ADC_HandleTypeDef hadc1;
|
|
|
|
/* ADC1 init function */
|
|
void MX_ADC1_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN ADC1_Init 0 */
|
|
|
|
/* USER CODE END ADC1_Init 0 */
|
|
|
|
ADC_ChannelConfTypeDef sConfig = {0};
|
|
|
|
/* USER CODE BEGIN ADC1_Init 1 */
|
|
|
|
/* USER CODE END ADC1_Init 1 */
|
|
|
|
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
|
|
*/
|
|
hadc1.Instance = ADC1;
|
|
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
|
|
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
|
|
hadc1.Init.ScanConvMode = DISABLE;
|
|
hadc1.Init.ContinuousConvMode = DISABLE;
|
|
hadc1.Init.DiscontinuousConvMode = DISABLE;
|
|
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
|
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
|
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
|
hadc1.Init.NbrOfConversion = 1;
|
|
hadc1.Init.DMAContinuousRequests = DISABLE;
|
|
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
|
|
if (HAL_ADC_Init(&hadc1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
|
|
*/
|
|
sConfig.Channel = ADC_CHANNEL_1;
|
|
sConfig.Rank = 1;
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
|
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN ADC1_Init 2 */
|
|
|
|
/* USER CODE END ADC1_Init 2 */
|
|
|
|
}
|
|
|
|
void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
|
|
{
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
if(adcHandle->Instance==ADC1)
|
|
{
|
|
/* USER CODE BEGIN ADC1_MspInit 0 */
|
|
|
|
/* USER CODE END ADC1_MspInit 0 */
|
|
/* ADC1 clock enable */
|
|
__HAL_RCC_ADC1_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
/**ADC1 GPIO Configuration
|
|
PA1 ------> ADC1_IN1
|
|
*/
|
|
GPIO_InitStruct.Pin = CV_ADC_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
HAL_GPIO_Init(CV_ADC_GPIO_Port, &GPIO_InitStruct);
|
|
|
|
/* USER CODE BEGIN ADC1_MspInit 1 */
|
|
|
|
/* USER CODE END ADC1_MspInit 1 */
|
|
}
|
|
}
|
|
|
|
void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
|
|
{
|
|
|
|
if(adcHandle->Instance==ADC1)
|
|
{
|
|
/* USER CODE BEGIN ADC1_MspDeInit 0 */
|
|
|
|
/* USER CODE END ADC1_MspDeInit 0 */
|
|
/* Peripheral clock disable */
|
|
__HAL_RCC_ADC1_CLK_DISABLE();
|
|
|
|
/**ADC1 GPIO Configuration
|
|
PA1 ------> ADC1_IN1
|
|
*/
|
|
HAL_GPIO_DeInit(CV_ADC_GPIO_Port, CV_ADC_Pin);
|
|
|
|
/* USER CODE BEGIN ADC1_MspDeInit 1 */
|
|
|
|
/* USER CODE END ADC1_MspDeInit 1 */
|
|
}
|
|
}
|
|
|
|
/* USER CODE BEGIN 1 */
|
|
static void fun_adc1_dma_start()
|
|
{
|
|
if (HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc1_val_buf, (ADC1_CHANNEL_CNT * ADC1_CHANNEL_FRE)) != HAL_OK)
|
|
{
|
|
/* Start Conversation Error */
|
|
Error_Handler();
|
|
}
|
|
}
|
|
|
|
void fun_proc_adc1_dma(float32 *data)
|
|
{
|
|
uint8_t i;
|
|
uint32_t adc1_aver_val[ADC1_CHANNEL_CNT] = {0};
|
|
uint16_t value[ADC1_CHANNEL_CNT] = {0};
|
|
|
|
fun_adc1_dma_start();
|
|
|
|
for (i = 0; i < ADC1_CHANNEL_CNT; i++)
|
|
{
|
|
adc1_aver_val[i] = 0;
|
|
}
|
|
|
|
for (i = 0; i < ADC1_CHANNEL_FRE; i++)
|
|
{
|
|
adc1_aver_val[0] += adc1_val_buf[i * ADC1_CHANNEL_CNT + 0];
|
|
}
|
|
|
|
for (i = 0; i < ADC1_CHANNEL_CNT; i++)
|
|
{
|
|
value[i] = adc1_aver_val[i] / ADC1_CHANNEL_FRE;
|
|
}
|
|
|
|
*data = (float32)value[0] * 30.0f / 4095;
|
|
}
|
|
|
|
/* USER CODE END 1 */
|