- void ADC_Configuration_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- ADC_CommonInitTypeDef ADC_CommonInitStructure;
- ADC_InitTypeDef ADC_InitStructure;
- DMA_InitTypeDef DMA_InitStructure;
- TIM3_CH1_Init(); //timer trigger Init
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOA时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_ADC2,ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2
- |GPIO_Pin_3|GPIO_Pin_4 ; //ADC_IN1/IN2/IN3/IN4
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN ; //模拟模式
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; //浮空
- GPIO_Init (GPIOA ,&GPIO_InitStructure );
-
- DMA_Cmd(DMA2_Stream0, DISABLE); //DMA2_Stream0对应ADC1
- DMA_InitStructure.DMA_Channel = DMA_Channel_0; //DMA_Stream0-7
- DMA_InitStructure.DMA_PeripheralBaseAddr = (unsigned int)&(ADC->CDR); //0x40012308=(addr_base(0x40012000)+0x300+0x08)
- DMA_InitStructure.DMA_Memory0BaseAddr = (unsigned int)&ADCValue; //32-bit storage addr
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //外设为来源
- DMA_InitStructure.DMA_BufferSize = 2; //DMA缓存大小
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址不变
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址递增
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; //32-bit data width
- DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Word; //32-bit data width
- 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);
- ADC_DeInit();
- ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult;//ADC1,2双同步规则模式
- ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//两个采样阶段之间的延迟5个时钟
- ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2; //ADC_DMA模式2
- ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//预分频4分频。ADCCLK=PCLK2/4=84/4=21Mhz,ADC时钟最好不要超过36Mhz
- ADC_CommonInit(&ADC_CommonInitStructure);//初始化
- /****************ADC1 Configure PA1 PA3**********************/
- ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12位模式
- ADC_InitStructure.ADC_ScanConvMode = ENABLE;//扫描模式
- ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//连续转换 ***
- ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;//禁止触发检测,使用软件触发***
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_CC1;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐
- ADC_InitStructure.ADC_NbrOfConversion = 2;
- ADC_Init(ADC1, &ADC_InitStructure);//ADC初始?
-
- ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_3Cycles); //PA1 0.42us interval
- ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 2, ADC_SampleTime_3Cycles); //PA3
- ADC_DMACmd(ADC1, ENABLE);
- ADC_Cmd(ADC1, ENABLE);//开启AD转换器
- /*****************ADC2 Configure PA2 PA4***********************/
- ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12位模式
- ADC_InitStructure.ADC_ScanConvMode = ENABLE;//扫描模式
- ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//连续转换
- ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;//禁止触发检测,使用软件触发
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_CC1;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐
- ADC_InitStructure.ADC_NbrOfConversion = 2;
- ADC_Init(ADC2, &ADC_InitStructure);//ADC初始化
- //ADC_ExternalTrigConvCmd(ADC2, ENABLE);
- ADC_RegularChannelConfig(ADC2, ADC_Channel_2, 1, ADC_SampleTime_3Cycles); //PA2
- ADC_RegularChannelConfig(ADC2, ADC_Channel_4, 2, ADC_SampleTime_3Cycles); //PA4
- //ADC_DMACmd(ADC2, ENABLE);
- ADC_Cmd(ADC2, ENABLE);//开启AD转换器
- /*****************************************************/
- //ADC_SoftwareStartConv(ADC1); //开启软件触发 ADC1
- //ADC_ExternalTrigConvCmd(ADC1, ENABLE);; //设置外部触发模式使能
- ADC_ExternalTrigInjectedConvEdgeConfig(ADC1,ADC_ExternalTrigInjecConvEdge_Rising);
- ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);
- // ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE); //ADC中断
- // ADC_ITConfig(ADC2, ADC_IT_EOC, ENABLE);
- }
复制代码
|
|
2021-8-8 18:29:13
评论
举报
|
|
|