📄 stm32f2xx_adc.c
字号:
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_EXT_INJEC_TRIG(ADC_ExternalTrigInjecConv));
/* Get the old register value */
tmpreg = ADCx->CR2;
/* Clear the old external event selection for injected group */
tmpreg &= CR2_JEXTSEL_RESET;
/* Set the external event selection for injected group */
tmpreg |= ADC_ExternalTrigInjecConv;
/* Store the new register value */
ADCx->CR2 = tmpreg;
}
/**
* @brief Configures the ADCx external trigger edge for injected channels conversion.
* @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* @param ADC_ExternalTrigInjecConvEdge: specifies the ADC external trigger edge
* to start injected conversion.
* This parameter can be one of the following values:
* @arg ADC_ExternalTrigInjecConvEdge_None: external trigger disabled for
* injected conversion
* @arg ADC_ExternalTrigInjecConvEdge_Rising: detection on rising edge
* @arg ADC_ExternalTrigInjecConvEdge_Falling: detection on falling edge
* @arg ADC_ExternalTrigInjecConvEdge_RisingFalling: detection on both rising
* and falling edge
* @retval None
*/
void ADC_ExternalTrigInjectedConvEdgeConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConvEdge)
{
uint32_t tmpreg = 0;
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_EXT_INJEC_TRIG_EDGE(ADC_ExternalTrigInjecConvEdge));
/* Get the old register value */
tmpreg = ADCx->CR2;
/* Clear the old external trigger edge for injected group */
tmpreg &= CR2_JEXTEN_RESET;
/* Set the new external trigger edge for injected group */
tmpreg |= ADC_ExternalTrigInjecConvEdge;
/* Store the new register value */
ADCx->CR2 = tmpreg;
}
/**
* @brief Enables the selected ADC software start conversion of the injected channels.
* @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* @retval None
*/
void ADC_SoftwareStartInjectedConv(ADC_TypeDef* ADCx)
{
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
/* Enable the selected ADC conversion for injected group */
ADCx->CR2 |= (uint32_t)ADC_CR2_JSWSTART;
}
/**
* @brief Gets the selected ADC Software start injected conversion Status.
* @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* @retval The new state of ADC software start injected conversion (SET or RESET).
*/
FlagStatus ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef* ADCx)
{
FlagStatus bitstatus = RESET;
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
/* Check the status of JSWSTART bit */
if ((ADCx->CR2 & ADC_CR2_JSWSTART) != (uint32_t)RESET)
{
/* JSWSTART bit is set */
bitstatus = SET;
}
else
{
/* JSWSTART bit is reset */
bitstatus = RESET;
}
/* Return the JSWSTART bit status */
return bitstatus;
}
/**
* @brief Enables or disables the selected ADC automatic injected group
* conversion after regular one.
* @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* @param NewState: new state of the selected ADC auto injected conversion
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void ADC_AutoInjectedConvCmd(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 selected ADC automatic injected group conversion */
ADCx->CR1 |= (uint32_t)ADC_CR1_JAUTO;
}
else
{
/* Disable the selected ADC automatic injected group conversion */
ADCx->CR1 &= (uint32_t)(~ADC_CR1_JAUTO);
}
}
/**
* @brief Enables or disables the discontinuous mode for injected group
* channel for the specified ADC
* @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* @param NewState: new state of the selected ADC discontinuous mode on injected
* group channel.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void ADC_InjectedDiscModeCmd(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 selected ADC injected discontinuous mode */
ADCx->CR1 |= (uint32_t)ADC_CR1_JDISCEN;
}
else
{
/* Disable the selected ADC injected discontinuous mode */
ADCx->CR1 &= (uint32_t)(~ADC_CR1_JDISCEN);
}
}
/**
* @brief Returns the ADC injected channel conversion result
* @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* @param ADC_InjectedChannel: the converted ADC injected channel.
* This parameter can be one of the following values:
* @arg ADC_InjectedChannel_1: Injected Channel1 selected
* @arg ADC_InjectedChannel_2: Injected Channel2 selected
* @arg ADC_InjectedChannel_3: Injected Channel3 selected
* @arg ADC_InjectedChannel_4: Injected Channel4 selected
* @retval The Data conversion value.
*/
uint16_t ADC_GetInjectedConversionValue(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel)
{
__IO uint32_t tmp = 0;
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));
tmp = (uint32_t)ADCx;
tmp += ADC_InjectedChannel + JDR_OFFSET;
/* Returns the selected injected channel conversion data value */
return (uint16_t) (*(__IO uint32_t*) tmp);
}
/**
* @}
*/
/** @defgroup ADC_Group7 Interrupts and flags management functions
* @brief Interrupts and flags management functions
*
@verbatim
===============================================================================
Interrupts and flags management functions
===============================================================================
This section provides functions allowing to configure the ADC Interrupts and
to get the status and clear flags and Interrupts pending bits.
Each ADC provides 4 Interrupts sources and 6 Flags which can be divided into
3 groups:
I. Flags and Interrupts for ADC regular channels
=================================================
Flags :
----------
1. ADC_FLAG_OVR : Overrun detection when regular converted data are lost
2. ADC_FLAG_EOC : Regular channel end of conversion ==> to indicate (depending
on EOCS bit, managed by ADC_EOCOnEachRegularChannelCmd() ) the end of:
==> a regular CHANNEL conversion
==> sequence of regular GROUP conversions .
3. ADC_FLAG_STRT: Regular channel start ==> to indicate when regular CHANNEL
conversion starts.
Interrupts :
------------
1. ADC_IT_OVR : specifies the interrupt source for Overrun detection event.
2. ADC_IT_EOC : specifies the interrupt source for Regular channel end of
conversion event.
II. Flags and Interrupts for ADC Injected channels
=================================================
Flags :
----------
1. ADC_FLAG_JEOC : Injected channel end of conversion ==> to indicate at
the end of injected GROUP conversion
2. ADC_FLAG_JSTRT: Injected channel start ==> to indicate hardware when
injected GROUP conversion starts.
Interrupts :
------------
1. ADC_IT_JEOC : specifies the interrupt source for Injected channel end of
conversion event.
III. General Flags and Interrupts for the ADC
=================================================
Flags :
----------
1. ADC_FLAG_AWD: Analog watchdog ==> to indicate if the converted voltage
crosses the programmed thresholds values.
Interrupts :
------------
1. ADC_IT_AWD : specifies the interrupt source for Analog watchdog event.
The user should identify which mode will be used in his application to manage
the ADC controller events: Polling mode or Interrupt mode.
In the Polling Mode it is advised to use the following functions:
- ADC_GetFlagStatus() : to check if flags events occur.
- ADC_ClearFlag() : to clear the flags events.
In the Interrupt Mode it is advised to use the following functions:
- ADC_ITConfig() : to enable or disable the interrupt source.
- ADC_GetITStatus() : to check if Interrupt occurs.
- ADC_ClearITPendingBit() : to clear the Interrupt pending Bit
(corresponding Flag).
@endverbatim
* @{
*/
/**
* @brief Enables or disables the specified ADC interrupts.
* @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* @param ADC_IT: specifies the ADC interrupt sources to be enabled or disabled.
* This parameter can be one of the following values:
* @arg ADC_IT_EOC: End of conversion interrupt mask
* @arg ADC_IT_AWD: Analog watchdog interrupt mask
* @arg ADC_IT_JEOC: End of injected conversion interrupt mask
* @arg ADC_IT_OVR: Overrun interrupt enable
* @param NewState: new state of the specified ADC interrupts.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState)
{
uint32_t itmask = 0;
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
assert_param(IS_ADC_IT(ADC_IT));
/* Get the ADC IT index */
itmask = (uint8_t)ADC_IT;
itmask = (uint32_t)0x01 << itmask;
if (NewState != DISABLE)
{
/* Enable the selected ADC interrupts */
ADCx->CR1 |= itmask;
}
else
{
/* Disable the selected ADC interrupts */
ADCx->CR1 &= (~(uint32_t)itmask);
}
}
/**
* @brief Checks whether the specified ADC flag is set or not.
* @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* @param ADC_FLAG: specifies the flag to check.
* This parameter can be one of the following values:
* @arg ADC_FLAG_AWD: Analog watchdog flag
* @arg ADC_FLAG_EOC: End of conversion flag
* @arg ADC_FLAG_JEOC: End of injected group conversion flag
* @arg ADC_FLAG_JSTRT: Start of injected group conversion flag
* @arg ADC_FLAG_STRT: Start of regular group conversion flag
* @arg ADC_FLAG_OVR: Overrun flag
* @retval The new state of ADC_FLAG (SET or RESET).
*/
FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG)
{
FlagStatus bitstatus = RESET;
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_GET_FLAG(ADC_FLAG));
/* Check the status of the specified ADC flag */
if ((ADCx->SR & ADC_FLAG) != (uint8_t)RESET)
{
/* ADC_FLAG is set */
bitstatus = SET;
}
else
{
/* ADC_FLAG is reset */
bitstatus = RESET;
}
/* Return the ADC_FLAG status */
return bitstatus;
}
/**
* @brief Clears the ADCx's pending flags.
* @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* @param ADC_FLAG: specifies the flag to clear.
* This parameter can be any combination of the following values:
* @arg ADC_FLAG_AWD: Analog watchdog flag
* @arg ADC_FLAG_EOC: End of conversion flag
* @arg ADC_FLAG_JEOC: End of injected group conversion flag
* @arg ADC_FLAG_JSTRT: Start of injected group conversion flag
* @arg ADC_FLAG_STRT: Start of regular group conversion flag
* @arg ADC_FLAG_OVR: Overrun flag
* @retval None
*/
void ADC_ClearF
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -