📄 stm32l1xx_adc.c
字号:
/**
* @brief Fills each ADC_CommonInitStruct member with its default value.
* @param ADC_CommonInitStruct: pointer to an ADC_CommonInitTypeDef structure
* which will be initialized.
* @retval None
*/
void ADC_CommonStructInit(ADC_CommonInitTypeDef* ADC_CommonInitStruct)
{
/* Reset ADC init structure parameters values */
/* Initialize the ADC_Prescaler member */
ADC_CommonInitStruct->ADC_Prescaler = ADC_Prescaler_Div1;
}
/**
* @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 ADON bit to wake up the ADC from power down mode */
ADCx->CR2 |= (uint32_t)ADC_CR2_ADON;
}
else
{
/* Disable the selected ADC peripheral */
ADCx->CR2 &= (uint32_t)(~ADC_CR2_ADON);
}
}
/**
* @brief Selects the specified ADC Channels Bank.
* @param ADCx: where x can be 1 to select the ADC1 peripheral.
* @param ADC_Bank: ADC Channels Bank.
* @arg ADC_Bank_A: ADC Channels Bank A.
* @arg ADC_Bank_B: ADC Channels Bank B.
* @retval None
*/
void ADC_BankSelection(ADC_TypeDef* ADCx, uint8_t ADC_Bank)
{
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_BANK(ADC_Bank));
if (ADC_Bank != ADC_Bank_A)
{
/* Set the ADC_CFG bit to select the ADC Bank B channels */
ADCx->CR2 |= (uint32_t)ADC_CR2_CFG;
}
else
{
/* Reset the ADC_CFG bit to select the ADC Bank A channels */
ADCx->CR2 &= (uint32_t)(~ADC_CR2_CFG);
}
}
/**
* @}
*/
/** @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:
(#) Insert a freeze delay :
==> using ADC_DelaySelectionConfig(ADC1, ADC_DelayLength_Freeze).
(#) Enable the power down in Idle and Delay phases :
==> using ADC_PowerDownCmd(ADC1, ADC_PowerDown_Idle_Delay, ENABLE).
@endverbatim
* @{
*/
/**
* @brief Enables or disables the ADC Power Down during Delay and/or Idle phase.
* @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.
* @param ADC_PowerDown: The ADC power down configuration.
* This parameter can be one of the following values:
* @arg ADC_PowerDown_Delay: ADC is powered down during delay phase.
* @arg ADC_PowerDown_Idle: ADC is powered down during Idle phase.
* @arg ADC_PowerDown_Idle_Delay: ADC is powered down during Delay and Idle phases.
* @note The ADC can be powered down:
* @note During the hardware delay insertion (using the ADC_PowerDown_Delay
* parameter).
* => The ADC is powered up again at the end of the delay.
* @note During the ADC is waiting for a trigger event ( using the
* ADC_PowerDown_Idle parameter).
* => The ADC is powered up at the next trigger event.
* @note During the hardware delay insertion or the ADC is waiting for a
* trigger event (using the ADC_PowerDown_Idle_Delay parameter).
* => The ADC is powered up only at the end of the delay and at the
* next trigger event.
* @param NewState: new state of the ADCx power down.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void ADC_PowerDownCmd(ADC_TypeDef* ADCx, uint32_t ADC_PowerDown, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
assert_param(IS_ADC_POWER_DOWN(ADC_PowerDown));
if (NewState != DISABLE)
{
/* Enable the ADC power-down during Delay and/or Idle phase */
ADCx->CR1 |= ADC_PowerDown;
}
else
{
/* Disable The ADC power-down during Delay and/or Idle phase */
ADCx->CR1 &= (uint32_t)~ADC_PowerDown;
}
}
/**
* @brief Defines the length of the delay which is applied after a conversion
* or a sequence of conversion.
* @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 :
* - each regular conversion.
* - after each sequence of injected conversions.
* @note No Hardware delay is inserted between conversions of different groups.
* @note When the hardware delay is not enough, the Freeze Delay Mode can be
* selected and a new conversion can start only if all the previous data
* of the same group have been treated:
* - for a regular conversion: once the ADC conversion data register has
* been read (using ADC_GetConversionValue() function) or if the EOC
* Flag has been cleared (using ADC_ClearFlag() function).
* - for an injected conversion: when the JEOC bit has been cleared
* (using ADC_ClearFlag() function).
* @param ADCx: where x can be 1 to select the ADC1 peripheral.
* @param ADC_DelayLength: The length of delay which is applied after a
* conversion or a sequence of conversion.
* This parameter can be one of the following values:
* @arg ADC_DelayLength_None: No delay.
* @arg ADC_DelayLength_Freeze: Delay until the converted data has been read.
* @arg ADC_DelayLength_7Cycles: Delay length equal to 7 APB clock cycles.
* @arg ADC_DelayLength_15Cycles: Delay length equal to 15 APB clock cycles
* @arg ADC_DelayLength_31Cycles: Delay length equal to 31 APB clock cycles
* @arg ADC_DelayLength_63Cycles: Delay length equal to 63 APB clock cycles
* @arg ADC_DelayLength_127Cycles: Delay length equal to 127 APB clock cycles
* @arg ADC_DelayLength_255Cycles: Delay length equal to 255 APB clock cycles
* @retval None
*/
void ADC_DelaySelectionConfig(ADC_TypeDef* ADCx, uint8_t ADC_DelayLength)
{
uint32_t tmpreg = 0;
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_DELAY_LENGTH(ADC_DelayLength));
/* Get the old register value */
tmpreg = ADCx->CR2;
/* Clear the old delay length */
tmpreg &= CR2_DELS_RESET;
/* Set the delay length */
tmpreg |= ADC_DelayLength;
/* Store the new register value */
ADCx->CR2 = tmpreg;
}
/**
* @}
*/
/** @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.
@endverbatim
* @{
*/
/**
* @brief Enables or disables the analog watchdog on single/all regular
* or injected channels.
* @param ADCx: where x can be 1 to select the ADC1 peripheral.
* @param ADC_AnalogWatchdog: the ADC analog watchdog configuration.
* This parameter can be one of the following values:
* @arg ADC_AnalogWatchdog_SingleRegEnable: Analog watchdog on a single
* regular channel.
* @arg ADC_AnalogWatchdog_SingleInjecEnable: Analog watchdog on a single
* injected channel.
* @arg ADC_AnalogWatchdog_SingleRegOrInjecEnable: Analog watchdog on a
* single regular or injected channel.
* @arg ADC_AnalogWatchdog_AllRegEnable: Analog watchdog on all regular
* channel.
* @arg ADC_AnalogWatchdog_AllInjecEnable: Analog watchdog on all injected
* channel.
* @arg ADC_AnalogWatchdog_AllRegAllInjecEnable: Analog watchdog on all
* regular and injected channels.
* @arg ADC_AnalogWatchdog_None: No channel guarded by the analog watchdog.
* @retval None
*/
void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog)
{
uint32_t tmpreg = 0;
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_ANALOG_WATCHDOG(ADC_AnalogWatchdog));
/* Get the old register value */
tmpreg = ADCx->CR1;
/* Clear AWDEN, JAWDEN and AWDSGL bits */
tmpreg &= CR1_AWDMODE_RESET;
/* Set the analog watchdog enable mode */
tmpreg |= ADC_AnalogWatchdog;
/* Store the new register value */
ADCx->CR1 = tmpreg;
}
/**
* @brief Configures the high and low thresholds of the analog watchdog.
* @param ADCx: where x can be 1 to select the ADC1 peripheral.
* @param HighThreshold: the ADC analog watchdog High threshold value.
* This parameter must be a 12bit value.
* @param LowThreshold: the ADC analog watchdog Low threshold value.
* This parameter must be a 12bit value.
* @retval None
*/
void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold,
uint16_t LowThreshold)
{
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_THRESHOLD(HighThreshold));
assert_param(IS_ADC_THRESHOLD(LowThreshold));
/* Set the ADCx high threshold */
ADCx->HTR = HighThreshold;
/* Set the ADCx low threshold */
ADCx->LTR = LowThreshold;
}
/**
* @brief Configures the analog watchdog guarded single channel.
* @param ADCx: where x can be 1 to select the ADC1 peripheral.
* @param ADC_Channel: the ADC channel to configure for the analog watchdog.
* This parameter can be one of the following values:
* @arg ADC_Channel_0: ADC Channel0 selected
* @arg ADC_Channel_1: ADC Channel1 selected
* @arg ADC_Channel_2: ADC Channel2 selected
* @arg ADC_Channel_3: ADC Channel3 selected
* @arg ADC_Channel_4: ADC Channel4 selected
* @arg ADC_Channel_5: ADC Channel5 selected
* @arg ADC_Channel_6: ADC Channel6 selected
* @arg ADC_Channel_7: ADC Channel7 selected
* @arg ADC_Channel_8: ADC Channel8 selected
* @arg ADC_Channel_9: ADC Channel9 selected
* @arg ADC_Channel_10: ADC Channel10 selected
* @arg ADC_Channel_11: ADC Channel11 selected
* @arg ADC_Channel_12: ADC Channel12 selected
* @arg ADC_Channel_13: ADC Channel13 selected
* @arg ADC_Channel_14: ADC Channel14 selected
* @arg ADC_Channel_15: ADC Channel15 selected
* @arg ADC_Channel_16: ADC Channel16 selected
* @arg ADC_Channel_17: ADC Channel17 selected
* @arg ADC_Channel_18: ADC Channel18 selected
* @arg ADC_Channel_19: ADC Channel19 selected
* @arg ADC_Channel_20: ADC Channel20 selected
* @arg ADC_Channel_21: ADC Channel21 selected
* @arg ADC_Channel_22: ADC Channel22 selected
* @arg ADC_Channel_23: ADC Channel23 selected
* @arg ADC_Channel_24: ADC Channel24 selected
* @arg ADC_Channel_25: ADC Channel25 selected
* @arg ADC_Channel_27: ADC Channel27 selected
* @arg ADC_Channel_28: ADC Channel28 selected
* @arg ADC_Channel_29: ADC Channel29 selected
* @arg ADC_Channel_30: ADC Channel30 selected
* @arg ADC_Channel_31: ADC Channel31 selected
* @arg ADC_Channel_0b: ADC Channel0b selected
* @arg ADC_Channel_1b: ADC Channel1b selected
* @arg ADC_Channel_2b: ADC Channel2b selected
* @arg ADC_Channel_3b: ADC Channel3b selected
* @arg ADC_Channel_6b: ADC Channel6b selected
* @arg ADC_Channel_7b: ADC Channel7b selected
* @arg ADC_Channel_8b: ADC Channel8b selected
* @arg ADC_Channel_9b: ADC Channel9b selected
* @arg ADC_Channel_10b: ADC Channel10b selected
* @arg ADC_Channel_11b: ADC Channel11b selected
* @arg ADC_Channel_12b: ADC Channel12b selected
* @retval None
*/
void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel)
{
uint32_t tmpreg = 0;
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_CHANNEL(ADC_Channel));
/* Get the old register value */
tmpreg = ADCx->CR1;
/* Clear the Analog watchdog channel select bits */
tmpreg &= CR1_AWDCH_RESET;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -