📄 stm32l1xx_adc.c
字号:
* @note The ADC can be powered down:
* - During the hardware delay insertion (using the ADC_PowerDown_Delay
* parameter)
* => The ADC is powered up again at the end of the delay.
* - 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.
* - 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 :
1. the ADC guarded channel(s) is (are) selected using the
ADC_AnalogWatchdogSingleChannelConfig() function.
2. The Analog watchdog lower and higher threshold are configured using the
ADC_AnalogWatchdogThresholdsConfig() function.
3. 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
* @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;
/* Set the Analog watchdog channel */
tmpreg |= ADC_Channel;
/* Store the new register value */
ADCx->CR1 = tmpreg;
}
/**
* @}
*/
/** @defgroup ADC_Group4 Temperature Sensor & Vrefint (Voltage Reference internal) management function
* @brief Temperature Sensor & Vrefint (Voltage Reference internal) management function
*
@verbatim
===============================================================================
Temperature Sensor & Vrefint (Voltage Reference internal) management function
===============================================================================
This section provides a function allowing to enable/ disable the internal
connections between the ADC and the Temperature Sensor and the Vrefint source.
A typical configuration to get the Temperature sensor and Vrefint channels
voltages or is done following these steps :
1. Enable the internal connection of Temperature sensor and Vrefint sources
with the ADC channels using ADC_TempSensorVrefintCmd() function.
2. select the ADC_Channel_TempSensor and/or ADC_Channel_Vrefint using
ADC_RegularChannelConfig() or ADC_InjectedChannelConfig() functions
3. Get the voltage values, using ADC_GetConversionValue() or
ADC_GetInjectedConversionValue().
@endverbatim
* @{
*/
/**
* @brief Enables or disables the temperature sensor and Vrefint channel.
* @param NewState: new state of the temperature sensor and Vref int channels.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void ADC_TempSensorVrefintCmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the temperature sensor and Vrefint channel*/
ADC->CCR |= (uint32_t)ADC_CCR_TSVREFE;
}
else
{
/* Disable the temperature sensor and Vrefint channel*/
ADC->CCR &= (uint32_t)(~ADC_CCR_TSVREFE);
}
}
/**
* @}
*/
/** @defgroup ADC_Group5 Regular Channels Configuration functions
* @brief Regular Channels Configuration functions
*
@verbatim
===============================================================================
Regular Channels Configuration functions
===============================================================================
This section provides functions allowing to manage the ADC regular channels,
it is composed of 2 sub sections :
1. Configuration and management functions for regular channels: This subsection
provides functions allowing to configure the ADC regular channels :
- Configure the rank in the regular group sequencer for each channel
- Configure the sampling time for each channel
- select the conversion Trigger for regular channels
- select the desired EOC event behavior configuration
- Activate the continuous Mode (*)
- Activate the Discontinuous Mode
Please Note that the following features for regular channels are configurated
using the ADC_Init() function :
- scan mode activation
- continuous mode activation (**)
- External trigger source
- External trigger edge
- number of conversion in the regular channels group sequencer.
@note : (*) and (**) are performing the same configuration
2. Get the conversion data: This subsection provides an important function in
the ADC peripheral since it returns the converted data of the current
regular channel. When the Conversion value is read, the EOC Flag is
automatically cleared.
@endverbatim
* @{
*/
/**
* @brief Configures for the selected ADC regular channel its corresponding
* rank in the sequencer and its sampling time.
* @param ADCx: where x can be 1 to select the ADC peripheral.
* @param ADC_Channel: the ADC channel to configure.
* 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
* @param Rank: The rank in the regular group sequencer. This parameter
* must be between 1 to 26.
* @param ADC_SampleTime: The sample time value to be set for the selected
* channel.
* This parameter can be one of the following values:
* @arg ADC_SampleTime_4Cycles: Sample time equal to 4 cycles
* @arg ADC_SampleTime_9Cycles: Sample time equal to 9 cycles
* @arg ADC_SampleTime_16Cycles: Sample time equal to 16 cycles
* @arg ADC_SampleTime_24Cycles: Sample time equal to 24 cycles
* @arg ADC_SampleTime_48Cycles: Sample time equal to 48 cycles
* @arg ADC_SampleTime_96Cycles: Sample time equal to 96 cycles
* @arg ADC_SampleTime_192Cycles: Sample time equal to 192 cycles
* @arg ADC_SampleTime_384Cycles: Sample time equal to 384 cycles
* @retval None
*/
void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
{
uint32_t tmpreg1 = 0, tmpreg2 = 0;
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_CHANNEL(ADC_Channel));
assert_param(IS_ADC_REGULAR_RANK(Rank));
assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
/* if ADC_Channel_20 ... ADC_Channel_25 is selected */
if (ADC_Channel > ADC_Channel_19)
{
/* Get the old register value */
tmpreg1 = ADCx->SMPR1;
/* Calculate the mask to clear */
tmpreg2 = SMPR1_SMP_SET << (3 * (ADC_Channel - 20));
/* Clear the old sample time */
tmpreg1 &= ~tmpreg2;
/* Calculate the mask to set */
tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 20));
/* Set the new sample time */
tmpreg1 |= tmpreg2;
/* Store the new register value */
ADCx->SMPR1 = tmpreg1;
}
/* if ADC_Channel_10 ... ADC_Channel_19 is selected */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -