在进行ADC转换时,进行四个通道的转换,发现读取数据时第一次采样读数变了第二位,第二个数据变成第三位,第三位数据变成第四位,第四位数据变成了第一位。那么奇怪的?不知哪里出现了问题。我想的是需要的时候才进行ADC转换。
int main(void)
{
u16 num;
//配置系统时钟
RCC_Configuration();
// GPIO_Configuration();
ADC_Configuration();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 1, ADC_SampleTime_55Cycles5);
while(1)
{
ADC1->SQR3=0x0c;
ADC1->CR2 |= 0x01;
while((ADC1->SR&0x02 )!=0x02);
num=ADC_GetConversionValue(ADC1);
Display_LCD5bit(0,0,num,0);
ADC1->SQR3=0x0d;
ADC1->CR2 |= 0x01;
while((ADC1->SR&0x02 )!=0x02);
num=ADC_GetConversionValue(ADC1);
Display_LCD5bit(0,2,num,0);
ADC1->SQR3=0x2;
ADC1->CR2 |= 0x01;
while((ADC1->SR&0x02 )!=0x02);
num=ADC_GetConversionValue(ADC1);
Display_LCD5bit(0,4,num,0);
ADC1->SQR3=0x3;
ADC1->CR2 |= 0x01;
while((ADC1->SR&0x02 )!=0x02);
num=ADC_GetConversionValue(ADC1);
Display_LCD5bit(0,6,num,0);
delay_nms(300);
}
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立A/D
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //连续转换模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //数据右对齐
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel14 configuration */
//ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_55Cycles5);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
/* Enable ADC1 DMA */
// ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
} |