Motion_EC_Stm32_archived/Core/Src/adc.c

208 lines
5.5 KiB
C
Raw Normal View History

2020-07-15 18:26:22 +08:00
/**
******************************************************************************
* @file adc.c
* @brief This file provides code for the configuration
* of the ADC instances.
2020-07-15 18:26:22 +08:00
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "adc.h"
/* USER CODE BEGIN 0 */
//watch dog threshold setting:
//1828--35°C
//1721--15°C
2020-07-15 18:26:22 +08:00
/* USER CODE END 0 */
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
/* ADC1 init function */
void MX_ADC1_Init(void)
{
ADC_AnalogWDGConfTypeDef AnalogWDGConfig = {0};
2020-07-15 18:26:22 +08:00
ADC_ChannelConfTypeDef sConfig = {0};
2020-12-16 12:02:47 +08:00
/** Common config
2020-07-15 18:26:22 +08:00
*/
hadc1.Instance = ADC1;
2020-08-25 19:33:15 +08:00
hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
2021-01-05 14:46:47 +08:00
hadc1.Init.ContinuousConvMode = DISABLE;
2020-07-15 18:26:22 +08:00
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 7;
2020-07-15 18:26:22 +08:00
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
2020-12-16 12:02:47 +08:00
/** Configure Analog WatchDog 1
*/
AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_SINGLE_REG;
AnalogWDGConfig.HighThreshold = 1828;
AnalogWDGConfig.LowThreshold = 1721;
AnalogWDGConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
AnalogWDGConfig.ITMode = ENABLE;
if (HAL_ADC_AnalogWDGConfig(&hadc1, &AnalogWDGConfig) != HAL_OK)
{
Error_Handler();
}
2020-12-16 12:02:47 +08:00
/** Configure Regular Channel
2020-07-15 18:26:22 +08:00
*/
sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
2020-07-15 18:26:22 +08:00
sConfig.Rank = ADC_REGULAR_RANK_1;
2020-08-25 19:33:15 +08:00
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
2020-12-16 12:02:47 +08:00
/** Configure Regular Channel
2020-08-25 19:33:15 +08:00
*/
sConfig.Channel = ADC_CHANNEL_VREFINT;
2020-08-25 19:33:15 +08:00
sConfig.Rank = ADC_REGULAR_RANK_2;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
2020-12-16 12:02:47 +08:00
/** Configure Regular Channel
2020-08-25 19:33:15 +08:00
*/
sConfig.Channel = ADC_CHANNEL_3;
2020-08-25 19:33:15 +08:00
sConfig.Rank = ADC_REGULAR_RANK_3;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
2020-12-16 12:02:47 +08:00
/** Configure Regular Channel
2020-08-25 19:33:15 +08:00
*/
sConfig.Channel = ADC_CHANNEL_4;
2020-08-25 19:33:15 +08:00
sConfig.Rank = ADC_REGULAR_RANK_4;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
2020-12-16 12:02:47 +08:00
/** Configure Regular Channel
2020-08-25 19:33:15 +08:00
*/
sConfig.Channel = ADC_CHANNEL_5;
2020-08-25 19:33:15 +08:00
sConfig.Rank = ADC_REGULAR_RANK_5;
2020-07-15 18:26:22 +08:00
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
2020-12-16 12:02:47 +08:00
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_6;
sConfig.Rank = ADC_REGULAR_RANK_6;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
2020-12-16 12:02:47 +08:00
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_7;
sConfig.Rank = ADC_REGULAR_RANK_7;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
2020-07-15 18:26:22 +08:00
}
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();
2020-12-16 12:02:47 +08:00
2020-07-15 18:26:22 +08:00
__HAL_RCC_GPIOA_CLK_ENABLE();
2020-12-16 12:02:47 +08:00
/**ADC1 GPIO Configuration
2020-07-15 18:26:22 +08:00
PA3 ------> ADC1_IN3
PA4 ------> ADC1_IN4
PA5 ------> ADC1_IN5
PA6 ------> ADC1_IN6
2020-12-16 12:02:47 +08:00
PA7 ------> ADC1_IN7
2020-07-15 18:26:22 +08:00
*/
2020-12-16 12:02:47 +08:00
GPIO_InitStruct.Pin = MON_24V0_DIV16_Pin|MON_5V0_DIV8_Pin|MON_12V0_DIV8_Pin|MON_BKP_BAT_DIV8_Pin
2020-07-15 18:26:22 +08:00
|MON_MAIN_PWR_DIV16_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* ADC1 DMA Init */
/* ADC1 Init */
hdma_adc1.Instance = DMA1_Channel1;
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_WORD;
hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_adc1.Init.Mode = DMA_CIRCULAR;
hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
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();
2020-12-16 12:02:47 +08:00
/**ADC1 GPIO Configuration
2020-07-15 18:26:22 +08:00
PA3 ------> ADC1_IN3
PA4 ------> ADC1_IN4
PA5 ------> ADC1_IN5
PA6 ------> ADC1_IN6
2020-12-16 12:02:47 +08:00
PA7 ------> ADC1_IN7
2020-07-15 18:26:22 +08:00
*/
2020-12-16 12:02:47 +08:00
HAL_GPIO_DeInit(GPIOA, MON_24V0_DIV16_Pin|MON_5V0_DIV8_Pin|MON_12V0_DIV8_Pin|MON_BKP_BAT_DIV8_Pin
2020-07-15 18:26:22 +08:00
|MON_MAIN_PWR_DIV16_Pin);
/* ADC1 DMA DeInit */
HAL_DMA_DeInit(adcHandle->DMA_Handle);
/* ADC1 interrupt Deinit */
HAL_NVIC_DisableIRQ(ADC1_2_IRQn);
/* USER CODE BEGIN ADC1_MspDeInit 1 */
/* USER CODE END ADC1_MspDeInit 1 */
}
2020-12-16 12:02:47 +08:00
}
2020-07-15 18:26:22 +08:00
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/