📄 stm32f2xx_adc.c
字号:
* Data[31:16]: these bits contain the regular data of ADC2.
* @note In triple mode, the value returned by this function is as following
* Data[15:0] : these bits contain alternatively the regular data of ADC1, ADC3 and ADC2.
* Data[31:16]: these bits contain alternatively the regular data of ADC2, ADC1 and ADC3.
*/
uint32_t ADC_GetMultiModeConversionValue(void)
{
/* Return the multi mode conversion value */
return (*(__IO uint32_t *) CDR_ADDRESS);
}
/**
* @}
*/
/** @defgroup ADC_Group5 Regular Channels DMA Configuration functions
* @brief Regular Channels DMA Configuration functions
*
@verbatim
===============================================================================
Regular Channels DMA Configuration functions
===============================================================================
This section provides functions allowing to configure the DMA for ADC regular
channels.
Since converted regular channel values are stored into a unique data register,
it is useful to use DMA for conversion of more than one regular channel. This
avoids the loss of the data already stored in the ADC Data register.
When the DMA mode is enabled (using the ADC_DMACmd() function), after each
conversion of a regular channel, a DMA request is generated.
Depending on the "DMA disable selection for Independent ADC mode"
configuration (using the ADC_DMARequestAfterLastTransferCmd() function),
at the end of the last DMA transfer, two possibilities are allowed:
- No new DMA request is issued to the DMA controller (feature DISABLED)
- Requests can continue to be generated (feature ENABLED).
Depending on the "DMA disable selection for multi ADC mode" configuration
(using the void ADC_MultiModeDMARequestAfterLastTransferCmd() function),
at the end of the last DMA transfer, two possibilities are allowed:
- No new DMA request is issued to the DMA controller (feature DISABLED)
- Requests can continue to be generated (feature ENABLED).
@endverbatim
* @{
*/
/**
* @brief Enables or disables the specified ADC DMA request.
* @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* @param NewState: new state of the selected ADC DMA transfer.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void ADC_DMACmd(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 DMA request */
ADCx->CR2 |= (uint32_t)ADC_CR2_DMA;
}
else
{
/* Disable the selected ADC DMA request */
ADCx->CR2 &= (uint32_t)(~ADC_CR2_DMA);
}
}
/**
* @brief Enables or disables the ADC DMA request after last transfer (Single-ADC mode)
* @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* @param NewState: new state of the selected ADC DMA request after last transfer.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void ADC_DMARequestAfterLastTransferCmd(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 DMA request after last transfer */
ADCx->CR2 |= (uint32_t)ADC_CR2_DDS;
}
else
{
/* Disable the selected ADC DMA request after last transfer */
ADCx->CR2 &= (uint32_t)(~ADC_CR2_DDS);
}
}
/**
* @brief Enables or disables the ADC DMA request after last transfer in multi ADC mode
* @param NewState: new state of the selected ADC DMA request after last transfer.
* This parameter can be: ENABLE or DISABLE.
* @note if Enabled, DMA requests are issued as long as data are converted and
* DMA mode for multi ADC mode (selected using ADC_CommonInit() function
* by ADC_CommonInitStruct.ADC_DMAAccessMode structure member) is
* ADC_DMAAccessMode_1, ADC_DMAAccessMode_2 or ADC_DMAAccessMode_3.
* @retval None
*/
void ADC_MultiModeDMARequestAfterLastTransferCmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the selected ADC DMA request after last transfer */
ADC->CCR |= (uint32_t)ADC_CCR_DDS;
}
else
{
/* Disable the selected ADC DMA request after last transfer */
ADC->CCR &= (uint32_t)(~ADC_CCR_DDS);
}
}
/**
* @}
*/
/** @defgroup ADC_Group6 Injected channels Configuration functions
* @brief Injected channels Configuration functions
*
@verbatim
===============================================================================
Injected channels Configuration functions
===============================================================================
This section provide functions allowing to configure the ADC Injected channels,
it is composed of 2 sub sections :
1. Configuration functions for Injected channels: This subsection provides
functions allowing to configure the ADC injected channels :
- Configure the rank in the injected group sequencer for each channel
- Configure the sampling time for each channel
- Activate the Auto injected Mode
- Activate the Discontinuous Mode
- scan mode activation
- External/software trigger source
- External trigger edge
- injected channels sequencer.
2. Get the Specified Injected channel conversion data: This subsection
provides an important function in the ADC peripheral since it returns the
converted data of the specific injected channel.
@endverbatim
* @{
*/
/**
* @brief Configures for the selected ADC injected channel its corresponding
* rank in the sequencer and its sample time.
* @param ADCx: where x can be 1, 2 or 3 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
* @param Rank: The rank in the injected group sequencer.
* This parameter must be between 1 to 4.
* @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_3Cycles: Sample time equal to 3 cycles
* @arg ADC_SampleTime_15Cycles: Sample time equal to 15 cycles
* @arg ADC_SampleTime_28Cycles: Sample time equal to 28 cycles
* @arg ADC_SampleTime_56Cycles: Sample time equal to 56 cycles
* @arg ADC_SampleTime_84Cycles: Sample time equal to 84 cycles
* @arg ADC_SampleTime_112Cycles: Sample time equal to 112 cycles
* @arg ADC_SampleTime_144Cycles: Sample time equal to 144 cycles
* @arg ADC_SampleTime_480Cycles: Sample time equal to 480 cycles
* @retval None
*/
void ADC_InjectedChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
{
uint32_t tmpreg1 = 0, tmpreg2 = 0, tmpreg3 = 0;
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_CHANNEL(ADC_Channel));
assert_param(IS_ADC_INJECTED_RANK(Rank));
assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
/* if ADC_Channel_10 ... ADC_Channel_18 is selected */
if (ADC_Channel > ADC_Channel_9)
{
/* Get the old register value */
tmpreg1 = ADCx->SMPR1;
/* Calculate the mask to clear */
tmpreg2 = SMPR1_SMP_SET << (3*(ADC_Channel - 10));
/* Clear the old sample time */
tmpreg1 &= ~tmpreg2;
/* Calculate the mask to set */
tmpreg2 = (uint32_t)ADC_SampleTime << (3*(ADC_Channel - 10));
/* Set the new sample time */
tmpreg1 |= tmpreg2;
/* Store the new register value */
ADCx->SMPR1 = tmpreg1;
}
else /* ADC_Channel include in ADC_Channel_[0..9] */
{
/* Get the old register value */
tmpreg1 = ADCx->SMPR2;
/* Calculate the mask to clear */
tmpreg2 = SMPR2_SMP_SET << (3 * ADC_Channel);
/* Clear the old sample time */
tmpreg1 &= ~tmpreg2;
/* Calculate the mask to set */
tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel);
/* Set the new sample time */
tmpreg1 |= tmpreg2;
/* Store the new register value */
ADCx->SMPR2 = tmpreg1;
}
/* Rank configuration */
/* Get the old register value */
tmpreg1 = ADCx->JSQR;
/* Get JL value: Number = JL+1 */
tmpreg3 = (tmpreg1 & JSQR_JL_SET)>> 20;
/* Calculate the mask to clear: ((Rank-1)+(4-JL-1)) */
tmpreg2 = JSQR_JSQ_SET << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
/* Clear the old JSQx bits for the selected rank */
tmpreg1 &= ~tmpreg2;
/* Calculate the mask to set: ((Rank-1)+(4-JL-1)) */
tmpreg2 = (uint32_t)ADC_Channel << (5 * (uint8_t)((Rank + 3) - (tmpreg3 + 1)));
/* Set the JSQx bits for the selected rank */
tmpreg1 |= tmpreg2;
/* Store the new register value */
ADCx->JSQR = tmpreg1;
}
/**
* @brief Configures the sequencer length for injected channels
* @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* @param Length: The sequencer length.
* This parameter must be a number between 1 to 4.
* @retval None
*/
void ADC_InjectedSequencerLengthConfig(ADC_TypeDef* ADCx, uint8_t Length)
{
uint32_t tmpreg1 = 0;
uint32_t tmpreg2 = 0;
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_INJECTED_LENGTH(Length));
/* Get the old register value */
tmpreg1 = ADCx->JSQR;
/* Clear the old injected sequence length JL bits */
tmpreg1 &= JSQR_JL_RESET;
/* Set the injected sequence length JL bits */
tmpreg2 = Length - 1;
tmpreg1 |= tmpreg2 << 20;
/* Store the new register value */
ADCx->JSQR = tmpreg1;
}
/**
* @brief Set the injected channels conversion value offset
* @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* @param ADC_InjectedChannel: the ADC injected channel to set its offset.
* 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
* @param Offset: the offset value for the selected ADC injected channel
* This parameter must be a 12bit value.
* @retval None
*/
void ADC_SetInjectedOffset(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel, uint16_t Offset)
{
__IO uint32_t tmp = 0;
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));
assert_param(IS_ADC_OFFSET(Offset));
tmp = (uint32_t)ADCx;
tmp += ADC_InjectedChannel;
/* Set the selected injected channel data offset */
*(__IO uint32_t *) tmp = (uint32_t)Offset;
}
/**
* @brief Configures the ADCx external trigger for injected channels conversion.
* @param ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* @param ADC_ExternalTrigInjecConv: specifies the ADC trigger to start injected conversion.
* This parameter can be one of the following values:
* @arg ADC_ExternalTrigInjecConv_T1_CC4: Timer1 capture compare4 selected
* @arg ADC_ExternalTrigInjecConv_T1_TRGO: Timer1 TRGO event selected
* @arg ADC_ExternalTrigInjecConv_T2_CC1: Timer2 capture compare1 selected
* @arg ADC_ExternalTrigInjecConv_T2_TRGO: Timer2 TRGO event selected
* @arg ADC_ExternalTrigInjecConv_T3_CC2: Timer3 capture compare2 selected
* @arg ADC_ExternalTrigInjecConv_T3_CC4: Timer3 capture compare4 selected
* @arg ADC_ExternalTrigInjecConv_T4_CC1: Timer4 capture compare1 selected
* @arg ADC_ExternalTrigInjecConv_T4_CC2: Timer4 capture compare2 selected
* @arg ADC_ExternalTrigInjecConv_T4_CC3: Timer4 capture compare3 selected
* @arg ADC_ExternalTrigInjecConv_T4_TRGO: Timer4 TRGO event selected
* @arg ADC_ExternalTrigInjecConv_T5_CC4: Timer5 capture compare4 selected
* @arg ADC_ExternalTrigInjecConv_T5_TRGO: Timer5 TRGO event selected
* @arg ADC_ExternalTrigInjecConv_T8_CC2: Timer8 capture compare2 selected
* @arg ADC_ExternalTrigInjecConv_T8_CC3: Timer8 capture compare3 selected
* @arg ADC_ExternalTrigInjecConv_T8_CC4: Timer8 capture compare4 selected
* @arg ADC_ExternalTrigInjecConv_Ext_IT15: External interrupt line 15 event selected
* @retval None
*/
void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConv)
{
uint32_t tmpreg = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -