在帖子里面找到一个12通道ADC采样+DMA传输的开源代码,我把它改成4通道的,在板子上跑是可以的。后来想自己来配置一下,不过写着写着发现问题了,哪位大神好像没有使用分频函数诶,这样的话时钟超过14MHz怎么办,不过之前改的程序又能正常运行,想不通啊?我把它配置DMA和ADC的代码贴在下面,麻烦大家能告诉我他到底为什么没有配置R[mw_shl_code=c,true]void Adc_Init(void)
{
ADC_InitTypeDef ADC_InitStructure; //ADC初始化结构体
DMA_InitTypeDef DMA_InitStructure; //DMA初始化结构体
GPIO_InitTypeDef GPIO_InitStructure; //GPIO初始化结构体
/* 使能 ADC1, DMA2 和 GPIO 时钟 ****************************************/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_GPIOA, ENABLE);
/* DMA1 Stream1 channe1 配置 **************************************/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_BufferSize=120; //缓存数据个数
DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC; //以外设为数据源
DMA_InitStructure.DMA_M2M=DMA_M2M_Disable; //不使用内存到内存
DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)ADC1ConvertedValue;; //内存中存储DMA传输过来的数据基地址
DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord; //内存数据为半字 16位(ADC精度为12位)
DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable; //内存地址要增加
DMA_InitStructure.DMA_Mode=DMA_Mode_Circular; //DMA工作在循环模式
DMA_InitStructure.DMA_PeripheralBaseAddr= (uint32_t)&ADC1->DR; //数据源地址 ADC数据寄存器地址
DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord; //DMA外设数据宽度
DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable; //外设地址增加
DMA_InitStructure.DMA_Priority=DMA_Priority_High; //DMA优先级高
DMA_Init(DMA1_Channel1, &DMA_InitStructure); //DMA初始化
DMA_Cmd(DMA1_Channel1, ENABLE); //使能DMA
/* DMA_InitStructure.DMA_Channel = DMA_Channel_1;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)ADC1ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 128;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE);
//配置ADC1通道5引脚为模拟输入模式******************************/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;//采集电压的引脚为PA0
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN; //模拟输入模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;//采集电压的引脚为PA0
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN; //模拟输入模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度为50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化输入端口PA0
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;//采集电压的引脚为PA0
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN; //模拟输入模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度为50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化输入端口PA0
/****************************************************************************
PCLK2 = HCLK / 2
下面选择的是2分频
ADCCLK = PCLK2 /2 = HCLK / 4 = 168 / 4 = 42M
ADC采样频率: Sampling Time + Conversion Time = 3 + 12 cycles = 15cyc
Conversion Time = 42MHz / 15cyc = 2.8Mbps.
*****************************************************************************/
/* ADC公共部分初始化**********************************************************/
/*ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
//ADC1 初始化 ****************************************************************/
ADC_InitStructure.ADC_ContinuousConvMode=ENABLE; //ADC连续模式开
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right; //12位数据右对齐
ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None; //外部开启关 由软件开启
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent; //ADC工作在独立模式
ADC_InitStructure.ADC_NbrOfChannel=12; //开启通道数为1
ADC_InitStructure.ADC_ScanConvMode=ENABLE; //扫描模式关
ADC_Init(ADC1, &ADC_InitStructure); //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器
/*ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
//ADC1 规则 channel5 配置 *************************************/
// ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_84Cycles);
ADC_RegularChannelConfig(ADC1,ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_1, 2, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_2, 3, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_3, 4, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_4, 5, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_5, 6, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_6, 7, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_7, 8, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_9, 9, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_10,10, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_11,11, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_12,12, ADC_SampleTime_55Cycles5);
//ADC_RegularChannelConfig(ADC1,ADC_Channel_3, 3, ADC_SampleTime_55Cycles5);
/* 使能DMA请求(单ADC模式) */
//ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
/* 使能 ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* 使能 ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1); //使能复位校准
while(ADC_GetResetCalibrationStatus(ADC1)); //等待复位校准结束
ADC_StartCalibration(ADC1); //开启AD校准
while(ADC_GetCalibrationStatus(ADC1)); //等待校准结束
/* 软件启动ADC转换 */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
[/mw_shl_code]CC_ADCCLKConfig(RCC_PCLK2_Div6); //设置ADC分频因子6 72M/6=12,ADC最大时间不能超过14M
你好,后来我验证了一下,去掉分频函数和保留分频函数对程序结果都没有影响,不清楚是什么原因?另外你上面说的那个函数该怎么找?
一周热门 更多>