五 、配置规则通道参数
//设置指定ADC的规则组通道,一个序列,采样时间 常规通道配置
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 ); //ADC1,ADC通道,采样时间为239.5周期
。
void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime)
{
uint32_t tmpreg1 = 0, tmpreg2 = 0;
assert_param(IS_ADC_ALL_PERIPH(ADCx)); /* 检查参数 */
assert_param(IS_ADC_CHANNEL(ADC_Channel));
assert_param(IS_ADC_REGULAR_RANK(Rank));
assert_param(IS_ADC_SAMPLE_TIME(ADC_SampleTime));
if (ADC_Channel > ADC_Channel_9) /* if ADC_Channel_10 ... ADC_Channel_17 is selected */
{
tmpreg1 = ADCx->SMPR1; /* Get the old register value */
tmpreg2 = SMPR1_SMP_Set << (3 * (ADC_Channel - 10)); /* Calculate the mask to clear */
tmpreg1 &= ~tmpreg2; /* Clear the old channel sample time */
tmpreg2 = (uint32_t)ADC_SampleTime << (3 * (ADC_Channel - 10)); /* Calculate the mask to set */
tmpreg1 |= tmpreg2; /* Set the new channel sample time */
ADCx->SMPR1 = tmpreg1; /* Store the new register value */
}
else /* ADC_Channel include in ADC_Channel_[0..9] */
{
tmpreg1 = ADCx->SMPR2; /* 获取旧的寄存器值 */
tmpreg2 = SMPR2_SMP_Set << (3 * ADC_Channel); /* 计算掩码以清除 */
tmpreg1 &= ~tmpreg2; /* 清除旧通道的采样时间 */
tmpreg2 = (uint32_t)ADC_SampleTime << (3 * ADC_Channel); /* 计算要设置的掩码 */
tmpreg1 |= tmpreg2; /* 设置新通道采样时间 */
ADCx->SMPR2 = tmpreg1; /* 存储新的寄存器值 */
}
if (Rank < 7) /* For Rank 1 to 6 */
{
tmpreg1 = ADCx->SQR3;/* Get the old register value */
tmpreg2 = SQR3_SQ_Set << (5 * (Rank - 1));/* Calculate the mask to clear */
tmpreg1 &= ~tmpreg2;/* Clear the old SQx bits for the selected rank */
tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 1));/* Calculate the mask to set */
tmpreg1 |= tmpreg2;/* Set the SQx bits for the selected rank */
ADCx->SQR3 = tmpreg1;/* Store the new register value */
}
else if (Rank < 13)/* For Rank 7 to 12 */
{
tmpreg1 = ADCx->SQR2;/* Get the old register value */
tmpreg2 = SQR2_SQ_Set << (5 * (Rank - 7));/* Calculate the mask to clear */
tmpreg1 &= ~tmpreg2;/* Clear the old SQx bits for the selected rank */
tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 7));/* Calculate the mask to set */
tmpreg1 |= tmpreg2;/* Set the SQx bits for the selected rank */
ADCx->SQR2 = tmpreg1;/* Store the new register value */
}
else/* For Rank 13 to 16 */
{
tmpreg1 = ADCx->SQR1;/* Get the old register value */
tmpreg2 = SQR1_SQ_Set << (5 * (Rank - 13));/* Calculate the mask to clear */
tmpreg1 &= ~tmpreg2;/* Clear the old SQx bits for the selected rank */
tmpreg2 = (uint32_t)ADC_Channel << (5 * (Rank - 13));/* Calculate the mask to set */
tmpreg1 |= tmpreg2;/* Set the SQx bits for the selected rank */
ADCx->SQR1 = tmpreg1;/* Store the new register value */
}
}
。
。
。