📄 stm8l15x_adc.c
字号:
/**
******************************************************************************
* @file stm8l15x_adc.c
* @author MCD Application Team
* @version V1.5.0
* @date 13-May-2011
* @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 (Voltage Reference internal) management
* - Regular Channels Configuration
* - Regular Channels DMA Configuration
* - Injected channels Configuration
* - Interrupts and flags management
*
* @verbatim
*
* ===================================================================
* How to use this driver
* ===================================================================
* 1. Enable The ADC controller clock using CLK_PeripheralClockConfig()
* function : CLK_PeripheralClockConfig(CLK_Peripheral_ADC1, ENABLE).
*
* 2. Configure the ADC Prescaler, conversion resolution and data
* alignment using the ADC_Init() function.
*
* 3. Activate the ADC peripheral using ADC_Cmd() function.
*
* Regular channels group configuration
* ====================================
* - To configure the ADC regular channels group features, use
* ADC_Init() and ADC_RegularChannelConfig() functions.
* - To activate the continuous mode, use the ADC_continuousModeCmd()
* function.
* - To configure and activate the Discontinuous mode, use the
* ADC_DiscModeChannelCountConfig() and ADC_DiscModeCmd() functions.
* - To read the ADC converted values, use the ADC_GetConversionValue()
* function.
*
* DMA for Regular channels group features configuration
* ======================================================
* - To enable the DMA mode for regular channels group, use the
* ADC_DMACmd() function.
* - To enable the generation of DMA requests continuously at the end
* of the last DMA transfer, use the ADC_DMARequestAfterLastTransferCmd()
* function.
* Injected channels group configuration
* =====================================
* - To configure the ADC Injected channels group features, use
* ADC_InjectedChannelConfig() and ADC_InjectedSequencerLengthConfig()
* functions.
* - To activate the continuous mode, use the ADC_continuousModeCmd()
* function.
* - To activate the Injected Discontinuous mode, use the
* ADC_InjectedDiscModeCmd() function.
* - To activate the AutoInjected mode, use the ADC_AutoInjectedConvCmd()
* function.
* - To read the ADC converted values, use the ADC_GetInjectedConversionValue()
* function.
*
* @endverbatim
*
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8l15x_adc.h"
/** @addtogroup STM8L15x_StdPeriph_Driver
* @{
*/
/** @defgroup ADC
* @brief ADC driver modules
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* 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:
- Enable or disable the ADC peripheral,
- Initialize and configure the ADC Prescaler, ADC Conversion Resolution
(12bit..6bit), ADC Continuous Conversion Mode (Continuous or Single
conversion),
- Configure External trigger Sensitivity and source,
- Start ADC conversion, by software trigger.
@endverbatim
* @{
*/
/**
* @brief Deinitializes the ADC peripheral registers to their default reset
* values.
* @param ADCx where x can be 1 to select the specified ADC peripheral.
* @retval None
*/
void ADC_DeInit(ADC_TypeDef* ADCx)
{
/* Set the Configuration registers to their reset values */
ADCx->CR1 = ADC_CR1_RESET_VALUE;
ADCx->CR2 = ADC_CR2_RESET_VALUE;
ADCx->CR3 = ADC_CR3_RESET_VALUE;
/* Set the status registers to their reset values */
ADCx->SR = (uint8_t)~ADC_SR_RESET_VALUE;
/* Set the High threshold registers to their reset values */
ADCx->HTRH = ADC_HTRH_RESET_VALUE;
ADCx->HTRL = ADC_HTRL_RESET_VALUE;
/* Set the low threshold registers to their reset values */
ADCx->LTRH = ADC_LTRH_RESET_VALUE;
ADCx->LTRL = ADC_LTRL_RESET_VALUE;
/* Set the channels sequence registers to their reset values */
ADCx->SQR[0] = ADC_SQR1_RESET_VALUE;
ADCx->SQR[1] = ADC_SQR2_RESET_VALUE;
ADCx->SQR[2] = ADC_SQR3_RESET_VALUE;
ADCx->SQR[3] = ADC_SQR4_RESET_VALUE;
/* Set the channels Trigger registers to their reset values */
ADCx->TRIGR[0] = ADC_TRIGR1_RESET_VALUE;
ADCx->TRIGR[1] = ADC_TRIGR2_RESET_VALUE;
ADCx->TRIGR[2] = ADC_TRIGR3_RESET_VALUE;
ADCx->TRIGR[3] = ADC_TRIGR4_RESET_VALUE;
}
/**
* @brief Initializes the specified ADC peripheral according to the specified
* parameters.
* @param ADCx where x can be 1 to select the specified ADC peripheral.
* @param ADC_ConversionMode : specifies the ADC conversion mode,
* This parameter can be one of the following values:
* @arg ADC_ConversionMode_Single: Single Conversion Mode
* @arg ADC_ConversionMode_Continuous: Continuous Conversion Mode
* @param ADC_Resolution : specifies the ADC Data resolution,
* This parameter can be one of the following values:
* @arg ADC_Resolution_12Bit: 12 bit resolution
* @arg ADC_Resolution_10Bit: 10 bit resolution
* @arg ADC_Resolution_8Bit: 8 bit resolution
* @arg ADC_Resolution_6Bit: 6 bit resolution
* @param ADC_Prescaler : specifies the ADC Prescaler,
* This parameter can be one of the following values:
* @arg ADC_Prescaler_1: ADC Clock frequency is divided by 1
* @arg ADC_Prescaler_2: ADC Clock frequency is divided by 2
* @retval None
*/
void ADC_Init(ADC_TypeDef* ADCx,
ADC_ConversionMode_TypeDef ADC_ConversionMode,
ADC_Resolution_TypeDef ADC_Resolution,
ADC_Prescaler_TypeDef ADC_Prescaler)
{
/* Check the parameters */
assert_param(IS_ADC_CONVERSION_MODE(ADC_ConversionMode));
assert_param(IS_ADC_RESOLUTION(ADC_Resolution));
assert_param(IS_ADC_PRESCALER(ADC_Prescaler));
/*clear CR1 register */
ADCx->CR1 &= (uint8_t)~(ADC_CR1_CONT | ADC_CR1_RES);
/* set the resolution and the conversion mode */
ADCx->CR1 |= (uint8_t)((uint8_t)ADC_ConversionMode | (uint8_t)ADC_Resolution);
/*clear CR2 register */
ADCx->CR2 &= (uint8_t)~(ADC_CR2_PRESC);
/* set the Prescaler */
ADCx->CR2 |= (uint8_t) ADC_Prescaler;
}
/**
* @brief Enables or disables the selected ADC peripheral.
* @param ADCx where x can be 1 to select the specified ADC peripheral.
* @param NewState : new state of the specified ADC peripheral.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void ADC_Cmd(ADC_TypeDef* ADCx,
FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Set the ADON bit to wake up the specified ADC from power down mode */
ADCx->CR1 |= ADC_CR1_ADON;
}
else
{
/* Disable the selected ADC peripheral */
ADCx->CR1 &= (uint8_t)~ADC_CR1_ADON;
}
}
/**
* @brief Starts ADC conversion, by software trigger.
* @param ADCx where x can be 1 to select the specified ADC peripheral.
* @retval None
*/
void ADC_SoftwareStartConv(ADC_TypeDef* ADCx)
{
/* Start the ADC software conversion */
ADCx->CR1 |= ADC_CR1_START;
}
/**
* @brief Configures the ADC conversion through external trigger.
* @param ADCx where x can be 1 to select the specified ADC peripheral.
* @param ADC_ExtEventSelection : Specifies the external trigger.
* This parameter can be one of the following values:
* @arg ADC_ExtEventSelection_None: Conversion starts only by software start
* @arg ADC_ExtEventSelection_Trigger1: Trigger 1 Enables conversion
* @arg ADC_ExtEventSelection_Trigger2: Trigger 2 Enables conversion
* @arg ADC_ExtEventSelection_Trigger3: Trigger 3 Enables conversion
* @param ADC_ExtTRGSensitivity : Specifies the external trigger sensitivity.
* This parameter can be one of the following values:
* @arg ADC_ExtTRGSensitivity_Rising: External Trigger Sensitivity is Rising Edge
* @arg ADC_ExtTRGSensitivity_Falling: External Trigger Sensitivity is Falling Edge
* @arg ADC_ExtTRGSensitivity_All: External Trigger Sensitivity is Falling and Rising Edge
* @retval None
*/
void ADC_ExternalTrigConfig(ADC_TypeDef* ADCx,
ADC_ExtEventSelection_TypeDef ADC_ExtEventSelection,
ADC_ExtTRGSensitivity_TypeDef ADC_ExtTRGSensitivity)
{
/* Check the parameters */
assert_param(IS_ADC_EXT_EVENT_SELECTION(ADC_ExtEventSelection));
assert_param(IS_ADC_EXT_TRG_SENSITIVITY(ADC_ExtTRGSensitivity));
/*clear old configuration of CR2 register */
ADCx->CR2 &= (uint8_t)~(ADC_CR2_TRIGEDGE | ADC_CR2_EXTSEL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -