最近搞STM32F303的ADC1,ADC2使用DMA传输。发现只能单独使用一个,怎么配置都不对。查了手册发现ADC1ADC2其实是使用同一个32位寄存器,高半字ADC2,低半字ADC1。在这里分享下配置。
static void ADCGPIO_Config(void)
{
GPIO_InitTypeDef GPIO_ADCInitTypeDef;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_ADCInitTypeDef.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;
GPIO_ADCInitTypeDef.GPIO_Mode = GPIO_Mode_AN;
GPIO_ADCInitTypeDef.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_ADCInitTypeDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_ADCInitTypeDef);
GPIO_ADCInitTypeDef.GPIO_Pin = GPIO_Pin_14|GPIO_Pin_15;
GPIO_ADCInitTypeDef.GPIO_Mode = GPIO_Mode_AN;
GPIO_ADCInitTypeDef.GPIO_Speed= GPIO_Speed_50MHz;
GPIO_ADCInitTypeDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_ADCInitTypeDef);
}
/******************************************************************************
* Function Name: ADC_DMA_Config
* Description: DMAÅäÖÃ
* Input:
* Output:
* Return:
******************************************************************************/
void ADC_DMA_Config1(void)
{
// DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel1);
DMA_StructInit(&DMA_InitStructure_ADC1);
DMA_InitStructure_ADC1.DMA_PeripheralBaseAddr = (uint32_t)&ADC1_2->CDR; //外设地址
DMA_InitStructure_ADC1.DMA_MemoryBaseAddr = (uint32_t)&ADC_ConvertedValue_NO1; //自定义存储区,因为要存储两个AD数据,所以要定义为32位
DMA_InitStructure_ADC1.DMA_DIR = DMA_DIR_PeripheralSRC; //外设到内存
DMA_InitStructure_ADC1.DMA_BufferSize = ADCOUTNBUFFLENGTH; //缓存长度
DMA_InitStructure_ADC1.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址禁止自动增加
DMA_InitStructure_ADC1.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址自动增加
DMA_InitStructure_ADC1.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; //外设数据大小,一个字
DMA_InitStructure_ADC1.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; //内存数据打下,一个字
DMA_InitStructure_ADC1.DMA_Mode = DMA_Mode_Circular; //DMAÑ循环
DMA_InitStructure_ADC1.DMA_Priority = DMA_Priority_High; //高优先级
DMA_InitStructure_ADC1.DMA_M2M =DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure_ADC1);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
DMA_Cmd(DMA1_Channel1, ENABLE);
}
/******************************************************************************
* Function Name: ADC_Config
* Description: ADCÅäÖÃ
* Input:
* Output:
* Return:
******************************************************************************/
void ADC_Config2(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);
ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable;//Á¬Ðøģʽ
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ÓÒ¶ÔÆëÊý¾Ý
ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; //´¥·¢Ê¼þ
ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;//²»Ê¹ÓÃÈí¼þ´¥·¢
ADC_InitStructure.ADC_NbrOfRegChannel = 4; //ͨµÀÊý
ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;
ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_Init(ADC2, &ADC_InitStructure);
ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular;
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_RegSimul;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = 1;
ADC_CommonInit(ADC2,&ADC_CommonInitStructure);
/* ADC2 Regular Channel1 Configuration */
ADC_RegularChannelConfig(ADC2, ADC_Channel_1, 1, ADC_SampleTime_1Cycles5);//ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC2, ADC_Channel_2, 2, ADC_SampleTime_1Cycles5);//ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC2, ADC_Channel_3, 3, ADC_SampleTime_1Cycles5);//ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC2, ADC_Channel_4, 4, ADC_SampleTime_1Cycles5);//ADC_SampleTime_55Cycles5);
// ADC_DMA_Config2();
// ADC_DMAConfig(ADC2,ADC_DMAMode_Circular);
// ADC_DMACmd(ADC2, ENABLE); /* Enable ADC2's DMA interface */
ADC_Cmd(ADC2, ENABLE); /* Enable ADC2 */
}
/******************************************************************************
* Function Name: ADC_Config
* Description: ADCÅäÖÃ
* Input:
* Output:
* Return:
******************************************************************************/
void ADC_Config1(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);
//Ìí¼ÓÒ»²½³õʼ»¯
ADC_DeInit(ADC1);
// ADC_StructInit(&ADC_InitStructure);
// ADC_CommonStructInit(&ADC_CommonInitStructure);
/* Calibration procedure */
ADC_VoltageRegulatorCmd(ADC1, ENABLE);
/* Insert delay equal to 10 */
DelayUS(10);
ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1) != RESET );
calibration_value = ADC_GetCalibrationValue(ADC1);
// ADC_InitStructure.ADC_AutoInjMode =
ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;
ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
ADC_InitStructure.ADC_NbrOfRegChannel = 4;
ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;
ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular;
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_RegSimul;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = 1;
ADC_CommonInit(ADC1,&ADC_CommonInitStructure);
/* ADC1 Regular Channel1 Configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_1Cycles5);//ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 2, ADC_SampleTime_1Cycles5);//ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 3, ADC_SampleTime_1Cycles5);//ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 4, ADC_SampleTime_1Cycles5);//ADC_SampleTime_55Cycles5);
// ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 5, ADC_SampleTime_1Cycles5);//ADC_SampleTime_55Cycles5);
// ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 6, ADC_SampleTime_1Cycles5);//ADC_SampleTime_55Cycles5);
ADC_Config2();
ADC_DMA_Config1();
ADC_DMAConfig(ADC1,ADC_DMAMode_Circular);
ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1's DMA interface */
ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 */
DelayUS(20);
/* Start ADC1 calibaration */
// ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
//while(ADC_GetCalibrationStatus(ADC1));
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY));
DelayUS(10);
//ADC_StartConversion(ADC1);/* Start ADC1 Software Conversion*/
}
一周热门 更多>