?? stm32f10x_adc.c
字號:
/*******************************************************************************
* Function Name : ADC_ResetCalibration
* Description : Resets the ADC calibration registers.
* Input : - ADCx: where x can be 1 or 2 to select the ADC peripheral.
* Output : None
* Return : None
*******************************************************************************/
void ADC_ResetCalibration(ADC_TypeDef* ADCx)
{
/* Resets the selected ADC calibartion registers */
ADCx->CR2 |= CR2_RSTCAL_Set;
}
/*******************************************************************************
* Function Name : ADC_GetResetCalibrationStatus
* Description : Gets the ADC reset calibration registers status.
* Input : - ADCx: where x can be 1 or 2 to select the ADC peripheral.
* Output : None
* Return : The new state of ADC Reset Calibration registers (SET or RESET).
*******************************************************************************/
FlagStatus ADC_GetResetCalibrationStatus(ADC_TypeDef* ADCx)
{
FlagStatus bitstatus = RESET;
/* Check the status of RSTCAL bit */
if ((ADCx->CR2 & CR2_RSTCAL_Set) != RESET)
{
/* RSTCAL bit is set */
bitstatus = SET;
}
else
{
/* RSTCAL bit is reset */
bitstatus = RESET;
}
/* Return the RSTCAL bit status */
return bitstatus;
}
/*******************************************************************************
* Function Name : ADC_StartCalibration
* Description : Starts the calibration process.
* Input : - ADCx: where x can be 1 or 2 to select the ADC peripheral.
* Output : None
* Return : None
*******************************************************************************/
void ADC_StartCalibration(ADC_TypeDef* ADCx)
{
/* Enable the selected ADC calibration process */
ADCx->CR2 |= CR2_CAL_Set;
}
/*******************************************************************************
* Function Name : ADC_GetCalibrationStatus
* Description : Gets the ADC calibration status.
* Input : - ADCx: where x can be 1 or 2 to select the ADC peripheral.
* Output : None
* Return : The new state of ADC calibration (SET or RESET).
*******************************************************************************/
FlagStatus ADC_GetCalibrationStatus(ADC_TypeDef* ADCx)
{
FlagStatus bitstatus = RESET;
/* Check the status of CAL bit */
if ((ADCx->CR2 & CR2_CAL_Set) != RESET)
{
/* CAL bit is set: calibration on going */
bitstatus = SET;
}
else
{
/* CAL bit is reset: end of calibration */
bitstatus = RESET;
}
/* Return the CAL bit status */
return bitstatus;
}
/*******************************************************************************
* Function Name : ADC_SoftwareStartConvCmd
* Description : Enables or disables the ADC software start conversion .
* Input : - ADCx: where x can be 1 or 2 to select the ADC peripheral.
* - NewState: new state of the selected ADC software start conversion.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void ADC_SoftwareStartConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
{
if (NewState != DISABLE)
{
/* Starts the selected ADC conversion */
ADCx->CR2 |= CR2_SWSTRT_Set;
}
else
{
/* Stops the selected ADC conversion */
ADCx->CR2 &= CR2_SWSTRT_Reset;
}
}
/*******************************************************************************
* Function Name : ADC_GetSoftwareStartConvStatus
* Description : Gets the ADC Software start conversion Status.
* Input : - ADCx: where x can be 1 or 2 to select the ADC peripheral.
* Output : None
* Return : The new state of ADC software start conversion (SET or RESET).
*******************************************************************************/
FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* ADCx)
{
FlagStatus bitstatus = RESET;
/* Check the status of SWSTRT bit */
if ((ADCx->CR2 & CR2_SWSTRT_Set) != RESET)
{
/* SWSTRT bit is set */
bitstatus = SET;
}
else
{
/* SWSTRT bit is reset */
bitstatus = RESET;
}
/* Return the SWSTRT bit status */
return bitstatus;
}
/*******************************************************************************
* Function Name : ADC_DiscModeChannelCountConfig
* Description : Configures the discontinuous mode for the selected ADC regular
* group channel.
* Input : - ADCx: where x can be 1 or 2 to select the ADC peripheral.
* - Number: specifies the discontinuous mode regular channel
* count value. This mumber must be between 1 and 8.
* Output : None
* Return : None
*******************************************************************************/
void ADC_DiscModeChannelCountConfig(ADC_TypeDef* ADCx, u8 Number)
{
u32 tmpreg1 = 0;
u8 tmpreg2 = 0;
/* Get the old register value */
tmpreg1 = ADCx->CR1;
/* Clear the old discontinuous mode channel count */
tmpreg1 &= CR1_DISCNUM_Reset;
/* Set the discontinuous mode channel count */
tmpreg2 = Number - 1;
tmpreg1 |= tmpreg2 << 13;
/* Store the new register value */
ADCx->CR1 = tmpreg1;
}
/*******************************************************************************
* Function Name : ADC_DiscModeCmd
* Description : Enables or disables the discontinuous mode on regular group
* channel for the specified ADC
* Input : - ADCx: where x can be 1 or 2 to select the ADC peripheral.
* - NewState: new state of the selected ADC discontinuous mode
* on regular group channel.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void ADC_DiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
{
if (NewState != DISABLE)
{
/* Enable the selected ADC regular discontinuous mode */
ADCx->CR1 |= CR1_DISCEN_Set;
}
else
{
/* Disable the selected ADC regular discontinuous mode */
ADCx->CR1 &= CR1_DISCEN_Reset;
}
}
/*******************************************************************************
* Function Name : ADC_RegularChannelConfig
* Description : Configures for the selected ADC regular channel its corresponding
* rank in the sequencer and its sample time.
* Input : - ADCx: where x can be 1 or 2 to select the ADC peripheral.
* - ADC_Channel: the ADC channel to configure.
* This parameter can be one of the following values:
* - ADC_Channel0: ADC Channel0 selected
* - ADC_Channel1: ADC Channel1 selected
* - ADC_Channel2: ADC Channel2 selected
* - ADC_Channel3: ADC Channel3 selected
* - ADC_Channel4: ADC Channel4 selected
* - ADC_Channel5: ADC Channel5 selected
* - ADC_Channel6: ADC Channel6 selected
* - ADC_Channel7: ADC Channel7 selected
* - ADC_Channel8: ADC Channel8 selected
* - ADC_Channel9: ADC Channel9 selected
* - ADC_Channel10: ADC Channel10 selected
* - ADC_Channel11: ADC Channel11 selected
* - ADC_Channel12: ADC Channel12 selected
* - ADC_Channel13: ADC Channel13 selected
* - ADC_Channel14: ADC Channel14 selected
* - ADC_Channel15: ADC Channel15 selected
* - ADC_Channel16: ADC Channel16 selected
* - ADC_Channel17: ADC Channel17 selected
* - Rank: The rank in the regular group sequencer. This parameter
* must be between 1 to 16.
* - ADC_SampleTime: The sample time value to be set for the
* selected channel.
* This parameter can be one of the following values:
* - ADC_SampleTime_1Cycles5: Sample time equal to 1.5 cycles
* - ADC_SampleTime_7Cycles5: Sample time equal to 7.5 cycles
* - ADC_SampleTime_13Cycles5: Sample time equal to 13.5 cycles
* - ADC_SampleTime_28Cycles5: Sample time equal to 28.5 cycles
* - ADC_SampleTime_41Cycles5: Sample time equal to 41.5 cycles
* - ADC_SampleTime_55Cycles5: Sample time equal to 55.5 cycles
* - ADC_SampleTime_71Cycles5: Sample time equal to 71.5 cycles
* - ADC_SampleTime_239Cycles5: Sample time equal to 239.5 cycles
* Output : None
* Return : None
*******************************************************************************/
void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, u8 ADC_Channel, u8 Rank, u8 ADC_SampleTime)
{
u32 tmpreg1 = 0, tmpreg2 = 0;
/* if ADC_Channel10 ... ADC_Channel17 is selected */
if (ADC_Channel > ADC_Channel9)
{
/* Get the old register value */
tmpreg1 = ADCx->SMPR1;
/* Calculate the mask to clear */
tmpreg2 = SMPR1_SMP_Set << (3 * (ADC_Channel - 10));
/* Clear the old discontinuous mode channel count */
tmpreg1 &= ~tmpreg2;
/* Calculate the mask to set */
tmpreg2 = ADC_SampleTime << (3 * (ADC_Channel - 10));
/* Set the discontinuous mode channel count */
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 discontinuous mode channel count */
tmpreg1 &= ~tmpreg2;
/* Calculate the mask to set */
tmpreg2 = ADC_SampleTime << (3 * ADC_Channel);
/* Set the discontinuous mode channel count */
tmpreg1 |= tmpreg2;
/* Store the new register value */
ADCx->SMPR2 = tmpreg1;
}
/* For Rank 1 to 6 */
if (Rank < 7)
{
/* Get the old register value */
tmpreg1 = ADCx->SQR3;
/* Calculate the mask to clear */
tmpreg2 = SQR3_SQ_Set << (5 * (Rank - 1));
/* Clear the old SQx bits for the selected rank */
tmpreg1 &= ~tmpreg2;
/* Calculate the mask to set */
tmpreg2 = ADC_Channel << (5 * (Rank - 1));
/* Set the SQx bits for the selected rank */
tmpreg1 |= tmpreg2;
/* Store the new register value */
ADCx->SQR3 = tmpreg1;
}
/* For Rank 7 to 12 */
else if (Rank < 13)
{
/* Get the old register value */
tmpreg1 = ADCx->SQR2;
/* Calculate the mask to clear */
tmpreg2 = SQR2_SQ_Set << (5 * (Rank - 7));
/* Clear the old SQx bits for the selected rank */
tmpreg1 &= ~tmpreg2;
/* Calculate the mask to set */
tmpreg2 = ADC_Channel << (5 * (Rank - 7));
/* Set the SQx bits for the selected rank */
tmpreg1 |= tmpreg2;
/* Store the new register value */
ADCx->SQR2 = tmpreg1;
}
/* For Rank 13 to 16 */
else
{
/* Get the old register value */
tmpreg1 = ADCx->SQR1;
/* Calculate the mask to clear */
tmpreg2 = SQR1_SQ_Set << (5 * (Rank - 13));
/* Clear the old SQx bits for the selected rank */
tmpreg1 &= ~tmpreg2;
/* Calculate the mask to set */
tmpreg2 = ADC_Channel << (5 * (Rank - 13));
/* Set the SQx bits for the selected rank */
tmpreg1 |= tmpreg2;
/* Store the new register value */
ADCx->SQR1 = tmpreg1;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -