直接下的ADC例程不知道为什么,还有用多路ADC的时候PA1的ADC接VCC或GND其他的ADC也显示3.3V或0,不知道为什么之前还好好的,不会是板子烧坏了吧?我用MINI板子试103的例程就没有问题
原子哥能解释一下吗
@正点原子
这是ADC的程序:
#include "adc.h"
#include "delay.h"
//使能GPIOA、GPIOB、GPIOC、GPIOF、ADC1和ADC3时钟
//初始化所需引脚
//初始化ADC1和ADC3
//使能ADC1和ADC3
void Adc_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB
|RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOF, ENABLE);//使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //使能ADC1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;//PA5 通道5
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//模拟输入
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//不带上下拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//不带上下拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//不带上下拉
GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//不带上下拉
GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化
ADC_DeInit();
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//两个采样阶段之间的延迟5个时钟
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //DMA失能
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//预分频4分频。ADCCLK=PCLK2/4=84/4=21Mhz,ADC时钟最好不要超过36Mhz
ADC_CommonInit(&ADC_CommonInitStructure);//初始化
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12位模式
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//非扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//关闭连续转换
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//禁止触发检测,使用软件触发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐
ADC_InitStructure.ADC_NbrOfConversion = 1;//9个转换在规则序列中 也就是只转换规则序列1
ADC_Init(ADC1, &ADC_InitStructure);//ADC初始化
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12位模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE;//非扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//关闭连续转换
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//禁止触发检测,使用软件触发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐
ADC_InitStructure.ADC_NbrOfConversion = 2;//2个转换在规则序列中 也就是只转换规则序列1
ADC_Init(ADC3, &ADC_InitStructure);//ADC初始化
ADC_Cmd(ADC1, ENABLE);
ADC_Cmd(ADC3, ENABLE);
}
//获得ADC值
//case0-10:设置不同的ADC规则通道
//分别获得ADC1和ADC3的转换结果并存入数组ADCConvertedValue
//返回值:转换结果
u16 ADCConvertedValue[11];
void Get_Adc(void)
{
int i;
for(i=0;i<11;i++)
{
switch(i)
{
case 0:
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_480Cycles );
case 1:
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_480Cycles );
case 2:
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 3, ADC_SampleTime_480Cycles );
case 3:
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 4, ADC_SampleTime_480Cycles );
case 4:
ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 5, ADC_SampleTime_480Cycles );
case 5:
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 6, ADC_SampleTime_480Cycles );
case 6:
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 7, ADC_SampleTime_480Cycles );
case 7:
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 9, ADC_SampleTime_480Cycles );
case 8:
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 8, ADC_SampleTime_480Cycles );
case 9:
ADC_RegularChannelConfig(ADC3, ADC_Channel_14, 1, ADC_SampleTime_480Cycles );
case 10:
ADC_RegularChannelConfig(ADC3, ADC_Channel_15, 2, ADC_SampleTime_480Cycles );
}
if(i<=8)
{
ADC_Cmd(ADC1, ENABLE);
ADC_SoftwareStartConv(ADC1);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//等待转换结束
ADCConvertedValue
= ADC_GetConversionValue(ADC1); //返回最近一次ADC1规则组的转换结果
ADC_ClearFlag(ADC1,ADC_FLAG_EOC);
ADC_Cmd(ADC1, DISABLE);
}
else
{
ADC_Cmd(ADC3, ENABLE);
ADC_SoftwareStartConv(ADC3); //使能指定的ADC1的软件转换启动功能
while(!ADC_GetFlagStatus(ADC3, ADC_FLAG_EOC ));//等待转换结束
ADCConvertedValue= ADC_GetConversionValue(ADC3); //返回最近一次ADC3规则组的转换结果
ADC_ClearFlag(ADC3,ADC_FLAG_EOC);
ADC_Cmd(ADC3, DISABLE);
}
}
}
一周热门 更多>