DMA程序示例
- uint16_t AD_Value[4];
- void AD_Init(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
-
- RCC_ADCCLKConfig(RCC_PCLK2_Div6);
-
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- 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_InitTypeDef ADC_InitStructure;
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//连续转换
- ADC_InitStructure.ADC_ScanConvMode = ENABLE;//扫描模式
- ADC_InitStructure.ADC_NbrOfChannel = 4;//四个通道
- ADC_Init(ADC1, &ADC_InitStructure);
-
- DMA_InitTypeDef DMA_InitStructure;
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;//源地址
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//半字长
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//不自增
- DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)AD_Value;//目标地址
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//半字长
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//地址自增
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//外设到存储器
- DMA_InitStructure.DMA_BufferSize = 4;//传输计数器为4
- DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//循环模式
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//硬件触发
- DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//中等优先级
- DMA_Init(DMA1_Channel1, &DMA_InitStructure);
-
- DMA_Cmd(DMA1_Channel1, ENABLE);
- ADC_DMACmd(ADC1, ENABLE);//打通ADC到DMA的通道
- ADC_Cmd(ADC1, ENABLE);
-
- ADC_ResetCalibration(ADC1);
- while (ADC_GetResetCalibrationStatus(ADC1) == SET);
- ADC_StartCalibration(ADC1);
- while (ADC_GetCalibrationStatus(ADC1) == SET);
-
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
- }
|