下面是程序,请
论坛大神指导一下。
sta
tic void ADC2_DMA_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable ADC3, DMA2 and GPIO clocks ****************************************/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOA, ENABLE); //DMA2 GPIOA时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE); //ADC2时钟使能
/* Configure ADC3 Channel13 pin as analog input ******************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PA6
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; //模拟输入
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; //无上下拉输出
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
DMA_DeInit(DMA2_Stream2);
/* DMA2 Stream0 channel2 configuration **************************************/
DMA_InitStructure.DMA_Channel = DMA_Channel_2; //通道2
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &(ADC2->DR); //外设地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) &ADCoverVaule; //内存地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //DMA方向,外设到内存
DMA_InitStructure.DMA_BufferSize = 1; //数据量
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; //FIFO模式管
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; //FIFO半工
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; //内存BURST单次
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; //外设BURST单次
DMA_Init(DMA2_Stream2, &DMA_InitStructure); //初始化DMA
/* ADC Common Init **********************************************************/
ADC_DeInit(); //ADC复位
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div6; //6分频
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //DMA模式
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; //两次传送间隔5时钟周期
ADC_CommonInit(&ADC_CommonInitStructure); //ADC通用初始化
/* ADC2 Init ****************************************************************/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //精度12位
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //扫描模式关闭
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //连续模式关闭
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; //外设上升沿激活
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_TRGO; //T2 TRGO激活
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //右对齐
ADC_InitStructure.ADC_NbrOfConversion = 1; //1个通道
ADC_Init(ADC2, &ADC_InitStructure); //初始化ADC2
/* ADC3 regular channel13 configuration *************************************/
ADC_RegularChannelConfig(ADC2, ADC_Channel_6, 1, ADC_SampleTime_480Cycles); //ADC2通道6 规则排序1
/* Enable DMA request after last transfer (Single-ADC mode) */
/* Enable ADC3 DMA */
/* Enable ADC3 */
DMA_Cmd(DMA2_Stream2, ENABLE); //使能DMA
ADC_DMARequestAfterLastTransferCmd(ADC2, ENABLE); //转换完毕DMA自动请求
ADC_Cmd(ADC2, ENABLE); //ADC使能
ADC_DMACmd(ADC2, ENABLE); //ADC-DMA使能 这行不使能时 ADC2->DR值是刷新的,使能后就失效了
ADC_SoftwareStartConv(ADC2);
}
void Tim2_Init_TRGO(u16 Period,u16 Prescaler)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Down;
TIM_TimeBaseInitStructure.TIM_Period=Period;
TIM_TimeBaseInitStructure.TIM_Prescaler=Prescaler;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_Update);
TIM_Cmd(TIM2,ENABLE);
}
int main(void)
{
ADC2_DMA_Config();
Tim2_Init_TRGO(26250-1,0);
while (1)
{
}
}
这里是使用ADC时钟触发ADC
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_TRGO;
这里是使用TIM2时钟触发ADC
楼主你这样触发ADC能得到正确转换结果吗?
软件触发那个是我拿来测试的。 单通道已经调好了,现在在写交替模式9通道如何处理。
解决了,通道选错了。
一周热门 更多>