📄 stm32l1xx_adc.c
字号:
tmpreg2 = SMPR0_SMP_SET << (3 * (ADC_Channel - 30));
/* Clear the old sample time */
tmpreg1 &= ~tmpreg2;
/* Calculate the mask to set */
tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 30));
/* Set the new sample time */
tmpreg1 |= tmpreg2;
/* Store the new register value */
ADCx->SMPR0 = tmpreg1;
}
/* If ADC_Channel_20 ... ADC_Channel_29 is selected */
else 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 */
else if (ADC_Channel > ADC_Channel_9)
{
/* Get the old register value */
tmpreg1 = ADCx->SMPR2;
/* Calculate the mask to clear */
tmpreg2 = SMPR2_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->SMPR2 = tmpreg1;
}
else /* ADC_Channel include in ADC_Channel_[0..9] */
{
/* Get the old register value */
tmpreg1 = ADCx->SMPR3;
/* Calculate the mask to clear */
tmpreg2 = SMPR3_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->SMPR3 = 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 = (uint32_t)(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)(((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 to select the ADC1 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 to select the ADC1 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 to select the ADC1 peripheral.
* @param ADC_ExternalTrigInjecConv: specifies the ADC trigger to start injected
* conversion. This parameter can be one of the following values:
* @arg ADC_ExternalTrigInjecConv_T9_CC1: Timer9 capture compare1 selected
* @arg ADC_ExternalTrigInjecConv_T9_TRGO: Timer9 TRGO event selected
* @arg ADC_ExternalTrigInjecConv_T2_TRGO: Timer2 TRGO event selected
* @arg ADC_ExternalTrigInjecConv_T2_CC1: Timer2 capture compare1 selected
* @arg ADC_ExternalTrigInjecConv_T3_CC4: Timer3 capture compare4 selected
* @arg ADC_ExternalTrigInjecConv_T4_TRGO: Timer4 TRGO event 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_T10_CC1: Timer10 capture compare1 selected
* @arg ADC_ExternalTrigInjecConv_T7_TRGO: Timer7 TRGO event 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;
/* 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 to select the ADC1 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_ExternalTrigConvEdge_None: external trigger disabled for
* injected conversion.
* @arg ADC_ExternalTrigConvEdge_Rising: detection on rising edge
* @arg ADC_ExternalTrigConvEdge_Falling: detection on falling edge
* @arg ADC_ExternalTrigConvEdge_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 to select the ADC1 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 to select the ADC1 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 to select the ADC1 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 to select the ADC1 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 to select the ADC1 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_Group8 Interrupts and flags management functions
* @brief Interrupts and flags management functions.
*
@ver
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -