📄 stm32f2xx_adc.c
字号:
* @retval None
*/
void ADC_CommonInit(ADC_CommonInitTypeDef* ADC_CommonInitStruct)
{
uint32_t tmpreg1 = 0;
/* Check the parameters */
assert_param(IS_ADC_MODE(ADC_CommonInitStruct->ADC_Mode));
assert_param(IS_ADC_PRESCALER(ADC_CommonInitStruct->ADC_Prescaler));
assert_param(IS_ADC_DMA_ACCESS_MODE(ADC_CommonInitStruct->ADC_DMAAccessMode));
assert_param(IS_ADC_SAMPLING_DELAY(ADC_CommonInitStruct->ADC_TwoSamplingDelay));
/*---------------------------- ADC CCR Configuration -----------------*/
/* Get the ADC CCR value */
tmpreg1 = ADC->CCR;
/* Clear MULTI, DELAY, DMA and ADCPRE bits */
tmpreg1 &= CR_CLEAR_MASK;
/* Configure ADCx: Multi mode, Delay between two sampling time, ADC prescaler,
and DMA access mode for multimode */
/* Set MULTI bits according to ADC_Mode value */
/* Set ADCPRE bits according to ADC_Prescaler value */
/* Set DMA bits according to ADC_DMAAccessMode value */
/* Set DELAY bits according to ADC_TwoSamplingDelay value */
tmpreg1 |= (uint32_t)(ADC_CommonInitStruct->ADC_Mode |
ADC_CommonInitStruct->ADC_Prescaler |
ADC_CommonInitStruct->ADC_DMAAccessMode |
ADC_CommonInitStruct->ADC_TwoSamplingDelay);
/* Write to ADC CCR */
ADC->CCR = tmpreg1;
}
/**
* @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)
{
/* Initialize the ADC_Mode member */
ADC_CommonInitStruct->ADC_Mode = ADC_Mode_Independent;
/* initialize the ADC_Prescaler member */
ADC_CommonInitStruct->ADC_Prescaler = ADC_Prescaler_Div2;
/* Initialize the ADC_DMAAccessMode member */
ADC_CommonInitStruct->ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
/* Initialize the ADC_TwoSamplingDelay member */
ADC_CommonInitStruct->ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
}
/**
* @brief Enables or disables the specified ADC peripheral.
* @param ADCx: where x can be 1, 2 or 3 to select the ADC 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);
}
}
/**
* @}
*/
/** @defgroup ADC_Group2 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, 2 or 3 to select the ADC 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, 2 or 3 to select the ADC peripheral.
* @param HighThreshold: the ADC analog watchdog High threshold value.
* This parameter must be a 12-bit value.
* @param LowThreshold: the ADC analog watchdog Low threshold value.
* This parameter must be a 12-bit 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, 2 or 3 to select the ADC 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
* @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_Group3 Temperature Sensor, Vrefint (Voltage Reference internal)
* and VBAT (Voltage BATtery) management functions
* @brief Temperature Sensor, Vrefint and VBAT management functions
*
@verbatim
===============================================================================
Temperature Sensor, Vrefint and VBAT management functions
===============================================================================
This section provides functions allowing to enable/ disable the internal
connections between the ADC and the Temperature Sensor, the Vrefint and the
Vbat sources.
A typical configuration to get the Temperature sensor and Vrefint channels
voltages 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().
A typical configuration to get the VBAT channel voltage is done following
these steps :
1. Enable the internal connection of VBAT source with the ADC channel using
ADC_VBATCmd() function.
2. Select the ADC_Channel_Vbat using ADC_RegularChannelConfig() or
ADC_InjectedChannelConfig() functions
3. Get the voltage value, using ADC_GetConversionValue() or
ADC_GetInjectedConversionValue().
@endverbatim
* @{
*/
/**
* @brief Enables or disables the temperature sensor and Vrefint channels.
* @param NewState: new state of the temperature sensor and Vrefint 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);
}
}
/**
* @brief Enables or disables the VBAT (Voltage Battery) channel.
* @param NewState: new state of the VBAT channel.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void ADC_VBATCmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the VBAT channel*/
ADC->CCR |= (uint32_t)ADC_CCR_VBATE;
}
else
{
/* Disable the VBAT channel*/
ADC->CCR &= (uint32_t)(~ADC_CCR_VBATE);
}
}
/**
* @}
*/
/** @defgroup ADC_Group4 Regular Channels Configuration functions
* @brief Regular Channels Configuration functions
*
@verbatim
===============================================================================
Regular Channels Configuration functions
===============================================================================
This section provides functions allowing to manage the ADC's 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.
@note For multi ADC mode, the last ADC1, ADC2 and ADC3 regular conversions
results data (in the selected multi mode) can be returned in the same
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -