223 lines
5.9 KiB
C
223 lines
5.9 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 */
|
||
|
||
/* USER CODE END 0 */
|
||
|
||
ADC_HandleTypeDef hadc1;
|
||
DMA_HandleTypeDef hdma_adc1;
|
||
|
||
/* 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_DIV4;
|
||
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
|
||
hadc1.Init.ScanConvMode = ENABLE;
|
||
hadc1.Init.ContinuousConvMode = DISABLE;
|
||
hadc1.Init.DiscontinuousConvMode = DISABLE;
|
||
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
|
||
hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T2_CC2;
|
||
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||
hadc1.Init.NbrOfConversion = 4;
|
||
hadc1.Init.DMAContinuousRequests = ENABLE;
|
||
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_2;
|
||
sConfig.Rank = 1;
|
||
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
|
||
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != 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_4;
|
||
sConfig.Rank = 2;
|
||
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != 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_12;
|
||
sConfig.Rank = 3;
|
||
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != 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_13;
|
||
sConfig.Rank = 4;
|
||
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_GPIOC_CLK_ENABLE();
|
||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||
/**ADC1 GPIO Configuration
|
||
PC2 ------> ADC1_IN12
|
||
PC3 ------> ADC1_IN13
|
||
PA2 ------> ADC1_IN2
|
||
PA4 ------> ADC1_IN4
|
||
*/
|
||
GPIO_InitStruct.Pin = AO1FB_Pin|AO2FB_Pin;
|
||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||
|
||
GPIO_InitStruct.Pin = AI_Pin|BATTERY_Pin;
|
||
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||
|
||
/* ADC1 DMA Init */
|
||
/* ADC1 Init */
|
||
hdma_adc1.Instance = DMA2_Stream0;
|
||
hdma_adc1.Init.Channel = DMA_CHANNEL_0;
|
||
hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
||
hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
|
||
hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
|
||
hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
|
||
hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
|
||
hdma_adc1.Init.Mode = DMA_CIRCULAR;
|
||
hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
|
||
hdma_adc1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
|
||
if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
|
||
{
|
||
Error_Handler();
|
||
}
|
||
|
||
__HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
|
||
|
||
/* 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
|
||
PC2 ------> ADC1_IN12
|
||
PC3 ------> ADC1_IN13
|
||
PA2 ------> ADC1_IN2
|
||
PA4 ------> ADC1_IN4
|
||
*/
|
||
HAL_GPIO_DeInit(GPIOC, AO1FB_Pin|AO2FB_Pin);
|
||
|
||
HAL_GPIO_DeInit(GPIOA, AI_Pin|BATTERY_Pin);
|
||
|
||
/* ADC1 DMA DeInit */
|
||
HAL_DMA_DeInit(adcHandle->DMA_Handle);
|
||
/* USER CODE BEGIN ADC1_MspDeInit 1 */
|
||
|
||
/* USER CODE END ADC1_MspDeInit 1 */
|
||
}
|
||
}
|
||
|
||
/* USER CODE BEGIN 1 */
|
||
uint16_t ADC_ConvertedValue[4] = {0};//ADC<44><43><EFBFBD><EFBFBD>
|
||
uint16_t adcs_data[4] = {0};
|
||
uint32_t adcs_sum[4] = {0};
|
||
int adcs_cnt = 0;
|
||
|
||
void adcs_init(void)
|
||
{
|
||
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)ADC_ConvertedValue, 4);
|
||
|
||
}
|
||
void adcs_data_processing(void)
|
||
{
|
||
adcs_sum[0] += ADC_ConvertedValue[0];
|
||
adcs_sum[1] += ADC_ConvertedValue[1];
|
||
adcs_sum[2] += ADC_ConvertedValue[2];
|
||
adcs_sum[3] += ADC_ConvertedValue[3];
|
||
adcs_cnt++;
|
||
|
||
if(adcs_cnt > 49)
|
||
{
|
||
adcs_data[0] = adcs_sum[0] / 50.0;
|
||
adcs_sum[0] = 0;
|
||
adcs_data[1] = adcs_sum[1] / 50.0;
|
||
adcs_sum[1] = 0;
|
||
adcs_data[2] = adcs_sum[2] / 50.0;
|
||
adcs_sum[2] = 0;
|
||
adcs_data[3] = adcs_sum[3] / 50.0;
|
||
adcs_sum[3] = 0;
|
||
adcs_cnt = 0;
|
||
}
|
||
|
||
}
|
||
/* USER CODE END 1 */
|