📄 stm32f0xx_adc.c
字号:
/**
******************************************************************************
* @file stm32f0xx_adc.c
* @author MCD Application Team
* @version V1.0.0
* @date 23-March-2012
* @brief This file provides firmware functions to manage the following
* functionalities of the Analog to Digital Convertor (ADC) peripheral:
* + Initialization and Configuration
* + Power saving
* + Analog Watchdog configuration
* + Temperature Sensor, Vrefint (Internal Reference Voltage) and
* Vbat (Voltage battery) management
* + ADC Channels Configuration
* + ADC Channels DMA Configuration
* + Interrupts and flags management
*
* @verbatim
================================================================================
##### How to use this driver #####
================================================================================
[..]
(#) Enable the ADC interface clock using
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
(#) ADC pins configuration
(++) Enable the clock for the ADC GPIOs using the following function:
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOx, ENABLE);
(++) Configure these ADC pins in analog mode using GPIO_Init();
(#) Configure the ADC conversion resolution, data alignment, external
trigger and edge, scan direction and Enable/Disable the continuous mode
using the ADC_Init() function.
(#) Activate the ADC peripheral using ADC_Cmd() function.
*** ADC channels group configuration ***
============================================
[..]
(+) To configure the ADC channels features, use ADC_Init() and
ADC_ChannelConfig() functions.
(+) To activate the continuous mode, use the ADC_ContinuousModeCmd()
function.
(+) To activate the Discontinuous mode, use the ADC_DiscModeCmd() functions.
(+) To activate the overrun mode, use the ADC_OverrunModeCmd() functions.
(+) To activate the calibration mode, use the ADC_GetCalibrationFactor() functions.
(+) To read the ADC converted values, use the ADC_GetConversionValue()
function.
*** DMA for ADC channels features configuration ***
=============================================================
[..]
(+) To enable the DMA mode for ADC channels group, use the ADC_DMACmd() function.
(+) To configure the DMA transfer request, use ADC_DMARequestModeConfig() function.
* @endverbatim
*
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_adc.h"
#include "stm32f0xx_rcc.h"
/** @addtogroup STM32F0xx_StdPeriph_Driver
* @{
*/
/** @defgroup ADC
* @brief ADC driver modules
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* ADC CFGR mask */
#define CFGR1_CLEAR_MASK ((uint32_t)0xFFFFD203)
/* Calibration time out */
#define CALIBRATION_TIMEOUT ((uint32_t)0x0000F000)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/** @defgroup ADC_Private_Functions
* @{
*/
/** @defgroup ADC_Group1 Initialization and Configuration functions
* @brief Initialization and Configuration functions
*
@verbatim
===============================================================================
##### Initialization and Configuration functions #####
===============================================================================
[..] This section provides functions allowing to:
(+) Initialize and configure the ADC Prescaler
(+) ADC Conversion Resolution (12bit..6bit)
(+) ADC Continuous Conversion Mode (Continuous or Single conversion)
(+) External trigger Edge and source
(+) Converted data alignment (left or right)
(+) The direction in which the channels will be scanned in the sequence
(+) Enable or disable the ADC peripheral
@endverbatim
* @{
*/
/**
* @brief Deinitializes ADC1 peripheral registers to their default reset values.
* @param ADCx: where x can be 1 to select the ADC peripheral.
* @retval None
*/
void ADC_DeInit(ADC_TypeDef* ADCx)
{
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
if(ADCx == ADC1)
{
/* Enable ADC1 reset state */
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);
/* Release ADC1 from reset state */
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, DISABLE);
}
}
/**
* @brief Initializes the ADCx peripheral according to the specified parameters
* in the ADC_InitStruct.
* @note This function is used to configure the global features of the ADC (
* Resolution, Data Alignment, continuous mode activation, External
* trigger source and edge, Sequence Scan Direction).
* @param ADCx: where x can be 1 to select the ADC peripheral.
* @param ADC_InitStruct: pointer to an ADC_InitTypeDef structure that contains
* the configuration information for the specified ADC peripheral.
* @retval None
*/
void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct)
{
uint32_t tmpreg = 0;
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_RESOLUTION(ADC_InitStruct->ADC_Resolution));
assert_param(IS_FUNCTIONAL_STATE(ADC_InitStruct->ADC_ContinuousConvMode));
assert_param(IS_ADC_EXT_TRIG_EDGE(ADC_InitStruct->ADC_ExternalTrigConvEdge));
assert_param(IS_ADC_EXTERNAL_TRIG_CONV(ADC_InitStruct->ADC_ExternalTrigConv));
assert_param(IS_ADC_DATA_ALIGN(ADC_InitStruct->ADC_DataAlign));
assert_param(IS_ADC_SCAN_DIRECTION(ADC_InitStruct->ADC_ScanDirection));
/* Get the ADCx CFGR value */
tmpreg = ADCx->CFGR1;
/* Clear SCANDIR, RES[1:0], ALIGN, EXTSEL[2:0], EXTEN[1:0] and CONT bits */
tmpreg &= CFGR1_CLEAR_MASK;
/*---------------------------- ADCx CFGR Configuration ---------------------*/
/* Set RES[1:0] bits according to ADC_Resolution value */
/* Set CONT bit according to ADC_ContinuousConvMode value */
/* Set EXTEN[1:0] bits according to ADC_ExternalTrigConvEdge value */
/* Set EXTSEL[2:0] bits according to ADC_ExternalTrigConv value */
/* Set ALIGN bit according to ADC_DataAlign value */
/* Set SCANDIR bit according to ADC_ScanDirection value */
tmpreg |= (uint32_t)(ADC_InitStruct->ADC_Resolution | ((uint32_t)(ADC_InitStruct->ADC_ContinuousConvMode) << 13) |
ADC_InitStruct->ADC_ExternalTrigConvEdge | ADC_InitStruct->ADC_ExternalTrigConv |
ADC_InitStruct->ADC_DataAlign | ADC_InitStruct->ADC_ScanDirection);
/* Write to ADCx CFGR */
ADCx->CFGR1 = tmpreg;
}
/**
* @brief Fills each ADC_InitStruct member with its default value.
* @note This function is used to initialize the global features of the ADC (
* Resolution, Data Alignment, continuous mode activation, External
* trigger source and edge, Sequence Scan Direction).
* @param ADC_InitStruct: pointer to an ADC_InitTypeDef structure which will
* be initialized.
* @retval None
*/
void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct)
{
/* Reset ADC init structure parameters values */
/* Initialize the ADC_Resolution member */
ADC_InitStruct->ADC_Resolution = ADC_Resolution_12b;
/* Initialize the ADC_ContinuousConvMode member */
ADC_InitStruct->ADC_ContinuousConvMode = DISABLE;
/* Initialize the ADC_ExternalTrigConvEdge member */
ADC_InitStruct->ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
/* Initialize the ADC_ExternalTrigConv member */
ADC_InitStruct->ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_TRGO;
/* Initialize the ADC_DataAlign member */
ADC_InitStruct->ADC_DataAlign = ADC_DataAlign_Right;
/* Initialize the ADC_ScanDirection member */
ADC_InitStruct->ADC_ScanDirection = ADC_ScanDirection_Upward;
}
/**
* @brief Enables or disables the specified ADC peripheral.
* @param ADCx: where x can be 1 to select the ADC1 peripheral.
* @param NewState: new state of the ADCx peripheral.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Set the ADEN bit to Enable the ADC peripheral */
ADCx->CR |= (uint32_t)ADC_CR_ADEN;
}
else
{
/* Set the ADDIS to Disable the ADC peripheral */
ADCx->CR |= (uint32_t)ADC_CR_ADDIS;
}
}
/**
* @brief Enables or disables the jitter when the ADC is clocked by PCLK div2
* or div4
* @param ADCx: where x can be 1 to select the ADC1 peripheral.
* @param ADC_JitterOff: This parameter can be :
* @arg ADC_JitterOff_PCLKDiv2: Remove jitter when ADC is clocked by PLCK divided by 2
* @arg ADC_JitterOff_PCLKDiv4: Remove jitter when ADC is clocked by PLCK divided by 4
* @param NewState: new state of the ADCx jitter.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void ADC_JitterCmd(ADC_TypeDef* ADCx, uint32_t ADC_JitterOff, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_JITTEROFF(ADC_JitterOff));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Disable Jitter */
ADCx->CFGR2 |= (uint32_t)ADC_JitterOff;
}
else
{
/* Enable Jitter */
ADCx->CFGR2 &= (uint32_t)(~ADC_JitterOff);
}
}
/**
* @}
*/
/** @defgroup ADC_Group2 Power saving functions
* @brief Power saving functions
*
@verbatim
===============================================================================
##### Power saving functions #####
===============================================================================
[..] This section provides functions allowing to reduce power consumption.
[..] The two function must be combined to get the maximal benefits:
When the ADC frequency is higher than the CPU one, it is recommended to
(#) Enable the Auto Delayed Conversion mode :
==> using ADC_WaitModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
(#) Enable the power off in Delay phases :
==> using ADC_AutoPowerOffCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
@endverbatim
* @{
*/
/**
* @brief Enables or disables the ADC Power Off.
* @note ADC power-on and power-off can be managed by hardware to cut the
* consumption when the ADC is not converting.
* @param ADCx: where x can be 1 to select the ADC1 peripheral.
* @note The ADC can be powered down:
* - During the Auto delay phase
* => The ADC is powered on again at the end of the delay (until the
* previous data is read from the ADC data register).
* - During the ADC is waiting for a trigger event
* => The ADC is powered up at the next trigger event (when the
* conversion is started).
* @param NewState: new state of the ADCx power Off.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void ADC_AutoPowerOffCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the ADC Automatic Power-Off */
ADCx->CFGR1 |= ADC_CFGR1_AUTOFF;
}
else
{
/* Disable the ADC Automatic Power-Off */
ADCx->CFGR1 &= (uint32_t)~ADC_CFGR1_AUTOFF;
}
}
/**
* @brief Enables or disables the Wait conversion mode.
* @note When the CPU clock is not fast enough to manage the data rate, a
* Hardware delay can be introduced between ADC conversions to reduce
* this data rate.
* @note The Hardware delay is inserted after :
* - after each conversions and until the previous data is read from the
* ADC data register
* @note This is a way to automatically adapt the speed of the ADC to the speed
* of the system which will read the data.
* @note Any hardware triggers wich occur while a conversion is on going or
* while the automatic Delay is applied are ignored
* @param ADCx: where x can be 1 to select the ADC1 peripheral.
* @param NewState: new state of the ADCx Auto-Delay.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void ADC_WaitModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the ADC Automatic Delayed conversion */
ADCx->CFGR1 |= ADC_CFGR1_WAIT;
}
else
{
/* Disable the ADC Automatic Delayed conversion */
ADCx->CFGR1 &= (uint32_t)~ADC_CFGR1_WAIT;
}
}
/**
* @}
*/
/** @defgroup ADC_Group3 Analog Watchdog configuration functions
* @brief Analog Watchdog configuration functions
*
@verbatim
===============================================================================
##### Analog Watchdog configuration functions #####
===============================================================================
[..] This section provides functions allowing to configure the Analog Watchdog
(AWD) feature in the ADC.
[..] A typical configuration Analog Watchdog is done following these steps :
(#) the ADC guarded channel(s) is (are) selected using the
ADC_AnalogWatchdogSingleChannelConfig() function.
(#) The Analog watchdog lower and higher threshold are configured using the
ADC_AnalogWatchdogThresholdsConfig() function.
(#) The Analog watchdog is enabled and configured to enable the check, on one
or more channels, using the ADC_AnalogWatchdogCmd() function.
(#) Enable the analog watchdog on the selected channel using
ADC_AnalogWatchdogSingleChannelCmd() function
@endverbatim
* @{
*/
/**
* @brief Enables or disables the analog watchdog
* @param ADCx: where x can be 1 to select the ADC1 peripheral.
* @param NewState: new state of the ADCx Analog Watchdog.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -