官方资料太少,手册和参考代码都没有关于一次采集多路的介绍,又不想用DMA,我就自己摸索了一个办法和大家分享,简单粗暴了点,有更好的办法请赐教!
初始化代码(局部)
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //开GPIO时钟
//端口配置这里只贴一个通道
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //pin,
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Output speed
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //GPIO mode
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //启用ADC时钟
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_PRESCARE = ADC_PCLK2_PRESCARE_16; //ADC prescale factor ADC预分频系数
ADC_InitStructure.ADC_Mode = ADC_Mode_Single_Period ; //将ADC模式设置为连续转换模式/或单周期
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //AD data right-justified 数据右对齐
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; // ADC外部触发源,用于常规通道转换
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 4, ADC_SampleTime_239_5Cycles);//通道注入组规则
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 5, ADC_SampleTime_239_5Cycles);//
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 9, ADC_SampleTime_239_5Cycles);//
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 14, ADC_SampleTime_239_5Cycles);//
ADC_TempSensorCmd(ENABLE); //内置温度,14通道
ADC_Cmd(ADC1, ENABLE);
手动软件启动
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
定期查询方式获取4路AD值
if(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) != 0)
{
ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
puiADData[4] = (*(volatile unsigned int *)0x40012428)&0xFFF;//直接取地址
puiADData[5] = (*(volatile unsigned int *)0x4001242C)&0xFFF;
puiADData[9] = (*(volatile unsigned int *)0x4001243C)&0XFFF;
puiADData[14] = (*(volatile unsigned int *)0x40012450)&0xFFF;
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//再次启动转换
printf("[4]=%d,[5]=%d,[9]=%d,[14]=%d\r\n",puiADData[4],puiADData[5],puiADData[9],puiADData[14]);//打印结果
}
|