📄 stm32f10x_adc.c
字号:
* Function Name : ADC_InjectedSequencerLengthConfig
* Description : Configures the sequencer length for injected channels
* Input : - ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* - Length: The sequencer length.
* This parameter must be a number between 1 to 4.
* Output : None
* Return : None
*******************************************************************************/
void ADC_InjectedSequencerLengthConfig(ADC_TypeDef* ADCx, u8 Length)
{
u32 tmpreg1 = 0;
u32 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 sequnence lenght JL bits */
tmpreg1 &= JSQR_JL_Reset;
/* Set the injected sequnence lenght JL bits */
tmpreg2 = Length - 1;
tmpreg1 |= tmpreg2 << 20;
/* Store the new register value */
ADCx->JSQR = tmpreg1;
}
/*******************************************************************************
* Function Name : ADC_SetInjectedOffset
* Description : Set the injected channels conversion value offset
* Input : - ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* - ADC_InjectedChannel: the ADC injected channel to set its
* offset.
* This parameter can be one of the following values:
* - ADC_InjectedChannel_1: Injected Channel1 selected
* - ADC_InjectedChannel_2: Injected Channel2 selected
* - ADC_InjectedChannel_3: Injected Channel3 selected
* - ADC_InjectedChannel_4: Injected Channel4 selected
* - Offset: the offset value for the selected ADC injected channel
* This parameter must be a 12bit value.
* Output : None
* Return : None
*******************************************************************************/
void ADC_SetInjectedOffset(ADC_TypeDef* ADCx, u8 ADC_InjectedChannel, u16 Offset)
{
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));
assert_param(IS_ADC_OFFSET(Offset));
/* Set the selected injected channel data offset */
*((vu32 *)((*(u32*)&ADCx) + ADC_InjectedChannel)) = (u32)Offset;
}
/*******************************************************************************
* Function Name : ADC_GetInjectedConversionValue
* Description : Returns the ADC injected channel conversion result
* Input : - ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* - ADC_InjectedChannel: the converted ADC injected channel.
* This parameter can be one of the following values:
* - ADC_InjectedChannel_1: Injected Channel1 selected
* - ADC_InjectedChannel_2: Injected Channel2 selected
* - ADC_InjectedChannel_3: Injected Channel3 selected
* - ADC_InjectedChannel_4: Injected Channel4 selected
* Output : None
* Return : The Data conversion value.
*******************************************************************************/
u16 ADC_GetInjectedConversionValue(ADC_TypeDef* ADCx, u8 ADC_InjectedChannel)
{
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_INJECTED_CHANNEL(ADC_InjectedChannel));
/* Returns the selected injected channel conversion data value */
return (u16) (*(vu32*) (((*(u32*)&ADCx) + ADC_InjectedChannel + JDR_Offset)));
}
/*******************************************************************************
* Function Name : ADC_AnalogWatchdogCmd
* Description : Enables or disables the analog watchdog on single/all regular
* or injected channels
* Input : - ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* - ADC_AnalogWatchdog: the ADC analog watchdog configuration.
* This parameter can be one of the following values:
* - ADC_AnalogWatchdog_SingleRegEnable: Analog watchdog on
* a single regular channel
* - ADC_AnalogWatchdog_SingleInjecEnable: Analog watchdog on
* a single injected channel
* - ADC_AnalogWatchdog_SingleRegOrInjecEnable: Analog
* watchdog on a single regular or injected channel
* - ADC_AnalogWatchdog_AllRegEnable: Analog watchdog on
* all regular channel
* - ADC_AnalogWatchdog_AllInjecEnable: Analog watchdog on
* all injected channel
* - ADC_AnalogWatchdog_AllRegAllInjecEnable: Analog watchdog
* on all regular and injected channels
* - ADC_AnalogWatchdog_None: No channel guarded by the
* analog watchdog
* Output : None
* Return : None
*******************************************************************************/
void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, u32 ADC_AnalogWatchdog)
{
u32 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, AWDENJ and AWDSGL bits */
tmpreg &= CR1_AWDMode_Reset;
/* Set the analog watchdog enable mode */
tmpreg |= ADC_AnalogWatchdog;
/* Store the new register value */
ADCx->CR1 = tmpreg;
}
/*******************************************************************************
* Function Name : ADC_AnalogWatchdogThresholdsConfig
* Description : Configures the high and low thresholds of the analog watchdog.
* Input : - ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* - HighThreshold: the ADC analog watchdog High threshold value.
* This parameter must be a 12bit value.
* - LowThreshold: the ADC analog watchdog Low threshold value.
* This parameter must be a 12bit value.
* Output : None
* Return : None
*******************************************************************************/
void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, u16 HighThreshold,
u16 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;
}
/*******************************************************************************
* Function Name : ADC_AnalogWatchdogSingleChannelConfig
* Description : Configures the analog watchdog guarded single channel
* Input : - ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* - ADC_Channel: the ADC channel to configure for the analog
* watchdog.
* This parameter can be one of the following values:
* - ADC_Channel_0: ADC Channel0 selected
* - ADC_Channel_1: ADC Channel1 selected
* - ADC_Channel_2: ADC Channel2 selected
* - ADC_Channel_3: ADC Channel3 selected
* - ADC_Channel_4: ADC Channel4 selected
* - ADC_Channel_5: ADC Channel5 selected
* - ADC_Channel_6: ADC Channel6 selected
* - ADC_Channel_7: ADC Channel7 selected
* - ADC_Channel_8: ADC Channel8 selected
* - ADC_Channel_9: ADC Channel9 selected
* - ADC_Channel_10: ADC Channel10 selected
* - ADC_Channel_11: ADC Channel11 selected
* - ADC_Channel_12: ADC Channel12 selected
* - ADC_Channel_13: ADC Channel13 selected
* - ADC_Channel_14: ADC Channel14 selected
* - ADC_Channel_15: ADC Channel15 selected
* - ADC_Channel_16: ADC Channel16 selected
* - ADC_Channel_17: ADC Channel17 selected
* Output : None
* Return : None
*******************************************************************************/
void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, u8 ADC_Channel)
{
u32 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;
}
/*******************************************************************************
* Function Name : ADC_TempSensorVrefintCmd
* Description : Enables or disables the temperature sensor and Vrefint channel.
* Input : - NewState: new state of the temperature sensor.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : 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*/
ADC1->CR2 |= CR2_TSVREFE_Set;
}
else
{
/* Disable the temperature sensor and Vrefint channel*/
ADC1->CR2 &= CR2_TSVREFE_Reset;
}
}
/*******************************************************************************
* Function Name : ADC_GetFlagStatus
* Description : Checks whether the specified ADC flag is set or not.
* Input : - ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* - ADC_FLAG: specifies the flag to check.
* This parameter can be one of the following values:
* - ADC_FLAG_AWD: Analog watchdog flag
* - ADC_FLAG_EOC: End of conversion flag
* - ADC_FLAG_JEOC: End of injected group conversion flag
* - ADC_FLAG_JSTRT: Start of injected group conversion flag
* - ADC_FLAG_STRT: Start of regular group conversion flag
* Output : None
* Return : The new state of ADC_FLAG (SET or RESET).
*******************************************************************************/
FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, u8 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) != (u8)RESET)
{
/* ADC_FLAG is set */
bitstatus = SET;
}
else
{
/* ADC_FLAG is reset */
bitstatus = RESET;
}
/* Return the ADC_FLAG status */
return bitstatus;
}
/*******************************************************************************
* Function Name : ADC_ClearFlag
* Description : Clears the ADCx's pending flags.
* Input : - ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* - ADC_FLAG: specifies the flag to clear.
* This parameter can be any combination of the following values:
* - ADC_FLAG_AWD: Analog watchdog flag
* - ADC_FLAG_EOC: End of conversion flag
* - ADC_FLAG_JEOC: End of injected group conversion flag
* - ADC_FLAG_JSTRT: Start of injected group conversion flag
* - ADC_FLAG_STRT: Start of regular group conversion flag
* Output : None
* Return : None
*******************************************************************************/
void ADC_ClearFlag(ADC_TypeDef* ADCx, u8 ADC_FLAG)
{
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_CLEAR_FLAG(ADC_FLAG));
/* Clear the selected ADC flags */
ADCx->SR = ~(u32)ADC_FLAG;
}
/*******************************************************************************
* Function Name : ADC_GetITStatus
* Description : Checks whether the specified ADC interrupt has occurred or not.
* Input : - ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* - ADC_IT: specifies the ADC interrupt source to check.
* This parameter can be one of the following values:
* - ADC_IT_EOC: End of conversion interrupt mask
* - ADC_IT_AWD: Analog watchdog interrupt mask
* - ADC_IT_JEOC: End of injected conversion interrupt mask
* Output : None
* Return : The new state of ADC_IT (SET or RESET).
*******************************************************************************/
ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, u16 ADC_IT)
{
ITStatus bitstatus = RESET;
u32 itmask = 0, enablestatus = 0;
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_GET_IT(ADC_IT));
/* Get the ADC IT index */
itmask = ADC_IT >> 8;
/* Get the ADC_IT enable bit status */
enablestatus = (ADCx->CR1 & (u8)ADC_IT) ;
/* Check the status of the specified ADC interrupt */
if (((ADCx->SR & itmask) != (u32)RESET) && enablestatus)
{
/* ADC_IT is set */
bitstatus = SET;
}
else
{
/* ADC_IT is reset */
bitstatus = RESET;
}
/* Return the ADC_IT status */
return bitstatus;
}
/*******************************************************************************
* Function Name : ADC_ClearITPendingBit
* Description : Clears the ADCx抯 interrupt pending bits.
* Input : - ADCx: where x can be 1, 2 or 3 to select the ADC peripheral.
* - ADC_IT: specifies the ADC interrupt pending bit to clear.
* This parameter can be any combination of the following values:
* - ADC_IT_EOC: End of conversion interrupt mask
* - ADC_IT_AWD: Analog watchdog interrupt mask
* - ADC_IT_JEOC: End of injected conversion interrupt mask
* Output : None
* Return : None
*******************************************************************************/
void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, u16 ADC_IT)
{
u8 itmask = 0;
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_IT(ADC_IT));
/* Get the ADC IT index */
itmask = (u8)(ADC_IT >> 8);
/* Clear the selected ADC interrupt pending bits */
ADCx->SR = ~(u32)itmask;
}
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -