assert_param(IS_ADC_ALL_PERIPH(ADCx));
/* Check the status of SWSTART bit */
if ((ADCx->CR2 & CR2_SWSTART_Set) != (u32)RESET)// 0x00400000 {
/* SWSTART bit is set */ bitstatus = SET; } else {
/* SWSTART bit is reset */ bitstatus = RESET; }
/* Return the SWSTART bit status */ return bitstatus; }
【13】函数ADC_DiscModeChannelCountConfig
Table 23. 函数 ADC_DiscModeChannelCountConfig 函数名 ADC_DiscModeChannelCountConfig 函数原形 void ADC_DiscModeChannelCountConfig(ADC_TypeDef* ADCx, u8 Number) 功能描述 对ADC规则组通道配置间断模式 输入参数1 ADCx:x =1/2, 来选择ADC外设ADC1或ADC2 输入参数2 Number:间断模式规则组通道计数器的值。这个值得范围为1到8。 输出参数 无 返回值 无 先决条件 无 被调用函数 无 例:
/* Set the discontinuous mode channel count to 2 for ADC1 */ ADC_DiscModeChannelCountConfig(ADC1, 2); 函数原型如下:
void ADC_DiscModeChannelCountConfig(ADC_TypeDef* ADCx, u8 Number) {
u32 tmpreg1 = 0; u32 tmpreg2 = 0;
/* Check the parameters */
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_REGULAR_DISC_NUMBER(Number));//Number = 1-8
/* Get the old register value */ tmpreg1 = ADCx->CR1;
/* Clear the old discontinuous mode channel count */
tmpreg1 &= CR1_DISCNUM_Reset;// 0xFFFF1FFF,bit15-13:DISCUM[2:0], 间断模式通道计数 /* Set the discontinuous mode channel count */
tmpreg2 = Number - 1;//通道数-1就是要写入DISCUM[2:0]的值。 tmpreg1 |= tmpreg2 << 13;
/* Store the new register value */ ADCx->CR1 = tmpreg1; }
【14】函数ADC_DiscModeCmd
Table 24. 函数ADC_DiscModeCmd 函数名 ADC_DiscModeCmd 函数原形 void ADC_DiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState) 功能描述 输入参数 1 输入参数 2 输出参数 返回值 先决条件 被调用函数 例: 使能或者失能指定的ADC规则组通道的间断模式 ADCx:x =1/2, 来选择ADC外设ADC1或ADC2 NewState:ADC规则组通道上间断模式的新状态(ENABLE或DISABLE) 无 无 无 无 /* Disable the discontinuous mode for ADC1 regular group channel */ ADC_DiscModeCmd(ADC1, ENABLE); 函数原型如下:
void ADC_DiscModeCmd(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 regular discontinuous mode */
ADCx->CR1 |= CR1_DISCEN_Set;// 0x00000800,bit11: DISCEN,在规则通道上的间断模式开启 } else {
/* Disable the selected ADC regular discontinuous mode */ ADCx->CR1 &= CR1_DISCEN_Reset;// 0xFFFFF7FF } }
【15】函数ADC_RegularChannelConfig
Table 25. 函数ADC_RegularChannelConfig 函数名 ADC_RegularChannelConfig void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, u8 ADC_Channel, u8 函数原形 Rank, u8 ADC_SampleTime) 功能描述 设置指定ADC的规则组通道,设置它们的转化顺序和采样时间 输入参数1 ADCx:x =1/2, 来选择ADC外设ADC1 或ADC2 输入参数2 ADC_Channel:被设置的ADC通道 输入参数3 Rank:规则组采样顺序。取值范围1-16。 输入参数4 ADC_SampleTime:指定ADC通道的采样时间值 输出参数 无 返回值 无 先决条件 无 被调用函数 无 ADC_Channel :参数ADC_Channel指定了通过调用函数ADC_RegularChannelConfig来设置的ADC通道。 Table 26. ADC_Channel值 ADC_Channel 描述 编号值 Rank值 ADC_Channel_0 选择ADC通道0 0x00 1 ADC_Channel_1 选择ADC通道1 0x01 2 ADC_Channel_2 选择ADC通道2 0x02 3 ADC_Channel_3 选择ADC通道3 0x03 4 ADC_Channel_4 选择ADC通道4 0x04 5 ADC_Channel_5 选择ADC通道5 0x05 6 ADC_Channel_6 选择ADC通道6 0x06 7 ADC_Channel_7 选择ADC通道7 0x07 8 ADC_Channel_8 ADC_Channel_9 ADC_Channel_10 ADC_Channel_11 ADC_Channel_12 ADC_Channel_13 ADC_Channel_14 ADC_Channel_15 ADC_Channel_16 ADC_Channel_17 选择ADC通道8 选择ADC通道9 选择ADC通道10 选择ADC通道11 选择ADC通道12 选择ADC通道13 选择ADC通道14 选择ADC通道15 选择ADC通道16 选择ADC通道17 0x08 0x08 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F 0x10 0x11 9 10 11 12 13 14 15 16 -- -- Rank:取值为1-16。 ADC_SampleTime:设定了选中通道的ADC采样时间。Table 27. 列举了ADC_SampleTime可取的值:
Table 27. ADC_SampleTime值: ADC_SampleTime 描述 ADC_SampleTime_1Cycles5 采样时间为1.5周期 ADC_SampleTime_7Cycles5 采样时间为7.5周期 ADC_SampleTime_13Cycles5 采样时间为13.5周期 ADC_SampleTime_28Cycles5 采样时间为28.5周期 ADC_SampleTime_41Cycles5 采样时间为41.5周期 ADC_SampleTime_55Cycles5 采样时间为55.5周期 ADC_SampleTime_71Cycles5 采样时间为71.5周期 ADC_SampleTime_239Cycles5 采样时间为239.5周期 例: #Value 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 /* Configures ADC1 Channel2 as: first converted channel with an 7.5 cycles sample time */ ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_7Cycles5);
/* Configures ADC1 Channel8 as: second converted channel with an 1.5 cycles sample time */ ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 2, ADC_SampleTime_1Cycles5); 函数原型如下:
void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, u8 ADC_Channel, u8 Rank, u8 ADC_SampleTime) {
u32 tmpreg1 = 0, tmpreg2 = 0;
/* Check the parameters */
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_10 ... ADC_Channel_17 is selected */
if (ADC_Channel > ADC_Channel_9)//SMP10-SMP17在SMPR1寄存器中 {
/* Get the old register value */ tmpreg1 = ADCx->SMPR1;
/* Calculate the mask to clear */
tmpreg2 = SMPR1_SMP_Set << (3 * (ADC_Channel - 10));
//SMPR1_SMP_SET = 0x00000007。一个SMPx占用SMPRy的3位 /* Clear the old discontinuous mode channel count */ tmpreg1 &= ~tmpreg2;//清除目标通道上的原SMPx[2:0]值 /* 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;//写入寄存器。不用“|”:否则以前的配置可能影响本次配置值。
//假设用“|”:原先的值为0b010,目标要设置为0b100,“|”后结果为0b110。通道设置偏差了。
}
else /* ADC_Channel include in ADC_Channel_[0..9] *///SMP0-SMP9在SMPR2寄存器内。 {
/* 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 = (u32)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)//SQz,当z=1-6时,SQz在SQR3寄存器内。 {
/* Get the old register value */ tmpreg1 = ADCx->SQR3;
/* Calculate the mask to clear */
tmpreg2 = SQR3_SQ_Set << (5 * (Rank - 1));//每个SQz的占SQR寄存器5位。 /* Clear the old SQx bits for the selected rank */ tmpreg1 &= ~tmpreg2;
/* Calculate the mask to set */
tmpreg2 = (u32)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) //SQz,当z=7-12时,SQz在SQR2寄存器内。 {
/* 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 = (u32)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//SQz,当z=13-16时,SQz在SQR1寄存器内(只有4个,其他SQR各有6个)。 {
/* 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 = (u32)ADC_Channel << (5 * (Rank - 13));
相关推荐: