?? stm32f10x_adc.c
字號:
/* Check the parameters */
assert(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the selected ADC conversion on external event */
ADCx->CR2 |= CR2_EXTTRIG_Set;
}
else
{
/* Disable the selected ADC conversion on external event */
ADCx->CR2 &= CR2_EXTTRIG_Reset;
}
}
/*******************************************************************************
* Function Name : ADC_GetConversionValue
* Description : Returns the last ADCx conversion result data for regular channel.
* Input : - ADCx: where x can be 1 or 2 to select the ADC peripheral.
* Output : None
* Return : The Data conversion value.
*******************************************************************************/
u16 ADC_GetConversionValue(ADC_TypeDef* ADCx)
{
/* Return the selected ADC conversion value */
return (u16) ADCx->DR;
}
/*******************************************************************************
* Function Name : ADC_GetDualModeConversionValue
* Description : Returns the last ADCs conversion result data in dual mode.
* Output : None
* Return : The Data conversion value.
*******************************************************************************/
u32 ADC_GetDualModeConversionValue(void)
{
/* Return the dual mode conversion value */
return ADC1->DR;
}
/*******************************************************************************
* Function Name : ADC_AutoInjectedConvCmd
* Description : Enables or disables the selected ADC automatic injected group
* conversion after regular one.
* Input : - ADCx: where x can be 1 or 2 to select the ADC peripheral.
* - NewState: new state of the selected ADC auto injected
* conversion
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void ADC_AutoInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
{
/* Check the parameters */
assert(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the selected ADC automatic injected group conversion */
ADCx->CR1 |= CR1_JAUTO_Set;
}
else
{
/* Disable the selected ADC automatic injected group conversion */
ADCx->CR1 &= CR1_JAUTO_Reset;
}
}
/*******************************************************************************
* Function Name : ADC_InjectedDiscModeCmd
* Description : Enables or disables the discontinuous mode for injected 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 injected group channel.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void ADC_InjectedDiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
{
/* Check the parameters */
assert(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the selected ADC injected discontinuous mode */
ADCx->CR1 |= CR1_JDISCEN_Set;
}
else
{
/* Disable the selected ADC injected discontinuous mode */
ADCx->CR1 &= CR1_JDISCEN_Reset;
}
}
/*******************************************************************************
* Function Name : ADC_ExternalTrigInjectedConvConfig
* Description : Configures the ADCx external trigger for injected channels conversion.
* Input : - ADCx: where x can be 1 or 2 to select the ADC peripheral.
* - ADC_ExternalTrigInjecConv: specifies the ADC trigger to
* start injected conversion.
* This parameter can be one of the following values:
* - ADC_ExternalTrigInjecConv_T1_TRGO: Timer1 TRGO event
* selected
* - ADC_ExternalTrigInjecConv_T1_CC4: Timer1 capture
* compare4 selected
* - ADC_ExternalTrigInjecConv_T2_TRGO: Timer2 TRGO event
* selected
* - ADC_External TrigInjecConv_T2_CC1: Timer2 capture
* compare1 selected
* - ADC_ExternalTrigInjecConv_T3_CC4: Timer3 capture
* compare4 selected
* - ADC_ExternalTrigInjecConv_T4_TRGO: Timer4 TRGO event
* selected
* - ADC_ExternalTrigInjecConv_Ext_Interrupt15: External
* interrupt 15 event selected
* - ADC_ExternalTrigInjecConv_None: Injected conversion
* started by software and not by external trigger
* Output : None
* Return : None
*******************************************************************************/
void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef* ADCx, u32 ADC_ExternalTrigInjecConv)
{
u32 tmpreg = 0;
/* Check the parameters */
assert(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;
}
/*******************************************************************************
* Function Name : ADC_ExternalTrigInjectedConvCmd
* Description : Enables or disables the ADCx injected channels conversion
* through external trigger
* Input : - ADCx: where x can be 1 or 2 to select the ADC peripheral.
* - NewState: new state of the selected ADC external trigger
* start of injected conversion.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void ADC_ExternalTrigInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
{
/* Check the parameters */
assert(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the selected ADC external event selection for injected group */
ADCx->CR2 |= CR2_JEXTTRIG_Set;
}
else
{
/* Disable the selected ADC external event selection for injected group */
ADCx->CR2 &= CR2_JEXTTRIG_Reset;
}
}
/*******************************************************************************
* Function Name : ADC_SoftwareStartInjectedConvCmd
* Description : Enables or disables the selected ADC start of the injected
* channels conversion.
* Input : - ADCx: where x can be 1 or 2 to select the ADC peripheral.
* - NewState: new state of the selected ADC software start
* injected conversion.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void ADC_SoftwareStartInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState)
{
/* Check the parameters */
assert(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the selected ADC external event selection for injected group */
/* Starts the selected ADC injected conversion */
ADCx->CR2 |= CR2_JEXTTRIG_JSWSTRT_Set;
}
else
{
/* Stops the selected ADC injected conversion */
/* Disable the selected ADC external event selection for injected group */
ADCx->CR2 &= CR2_JEXTTRIG_JSWSTRT_Reset;
}
}
/*******************************************************************************
* Function Name : ADC_GetSoftwareStartInjectedConvCmdStatus
* Description : Gets the selected ADC Software start injected 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 injected conversion (SET or RESET).
*******************************************************************************/
FlagStatus ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef* ADCx)
{
FlagStatus bitstatus = RESET;
/* Check the status of JSWSTRT bit */
if ((ADCx->CR2 & CR2_JSWSTRT_Set) != (u32)RESET)
{
/* JSWSTRT bit is set */
bitstatus = SET;
}
else
{
/* JSWSTRT bit is reset */
bitstatus = RESET;
}
/* Return the JSWSTRT bit status */
return bitstatus;
}
/*******************************************************************************
* Function Name : ADC_InjectedChannelConfig
* Description : Configures for the selected ADC injected 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_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
* - Rank: The rank in the injected group sequencer. This parameter
* must be between 1 to 4.
* - 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_InjectedChannelConfig(ADC_TypeDef* ADCx, u8 ADC_Channel, u8 Rank, u8 ADC_SampleTime)
{
u32 tmpreg1 = 0, tmpreg2 = 0;
u8 tmpreg3 = 0;
/* Check the parameters */
assert(IS_ADC_CHANNEL(ADC_Channel));
assert(IS_ADC_INJECTED_RANK(Rank));
assert(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
/* if ADC_Channel_10 ... ADC_Channel_17 is selected */
if (ADC_Channel > ADC_Channel_9)
{
/* Get the old register value */
tmpreg1 = ADCx->SMPR1;
/* Calculate the mask to clear */
tmpreg2 = (u32)SMPR1_SMP_Set << (3*(ADC_Channel - 10));
/* Clear the old discontinuous mode channel count */
tmpreg1 &= ~tmpreg2;
/* Calculate the mask to set */
tmpreg2 = (u32)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 = (u32)SMPR2_SMP_Set << (3 * ADC_Channel);
/* Clear the old discontinuous mode channel count */
tmpreg1 &= ~tmpreg2;
/* Calculate the mask to set */
tmpreg2 = (u32)ADC_SampleTime << (3 * ADC_Channel);
/* Set the discontinuous mode channel count */
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 = (u8)((tmpreg1 & (u32)~JSQR_JL_Reset)>> 20);
/* Calculate the mask to clear: ((Rank-1)+(4-JL-1)) */
tmpreg2 = (u32)JSQR_JSQ_Set << (5 * ((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 = (u32)ADC_Channel << (5 * ((Rank + 3) - (tmpreg3 + 1)));
/* Set the JSQx bits for the selected rank */
tmpreg1 |= tmpreg2;
/* Store the new register value */
ADCx->JSQR = tmpreg1;
}
/*******************************************************************************
* Function Name : ADC_InjectedSequencerLengthConfig
* Description : Configures the sequencer length for injected channels
* Input : - ADCx: where x can be 1 or 2 to select the ADC peripheral.
* - Length: The sequencer length.
* This parameter must be a number between 1 to 4.
* Output : None
* Return : None
*******************************************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -