打印
[STM32F3]

STM32F303RE官方开发板ADC+DMA异常

[复制链接]
1063|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wzrde21|  楼主 | 2016-8-9 21:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
现象为ADC1DualConvertedValue【0】与ADC1DualConvertedValue【1】数据正常
ADC1DualConvertedValue【3】也就是ADC1通道4数据异常
请各位大神帮帮忙
/* 宏定义 --------------------------------------------------------------------*/
#define ADC1_CDR_ADDRESS    ((uint32_t)&ADC1->DR)
#define ADC2_CDR_ADDRESS    ((uint32_t)&ADC2->DR)

/* 变量 ----------------------------------------------------------------------*/  
extern        u16 ADC1DualConvertedValue[3];
extern        u16 ADC2DualConvertedValue[3];

void K_DMA_Configuration(void)
{
        DMA_InitTypeDef DMA_InitStructure;
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);

  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_CDR_ADDRESS;                                                                        //Specifies the peripheral base address for DMAy Channelx.
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC1DualConvertedValue;                        //Specifies the memory base address for DMAy Channelx
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;                                                                                                                //Specifies if the peripheral is the source or destination
  DMA_InitStructure.DMA_BufferSize = 3;                                                                                                                                                                        //Specifies the buffer size, in data unit, of the specified Channel
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;                                                        //Specifies whether the Peripheral address register is incremented or not.
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;                                                                                                //Specifies whether the memory address register is incremented or not.
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;                //Specifies the Peripheral data width
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;                                                //Specifies the Memory data width.
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;                                                                                                                                //Specifies the operation mode of the DMAy Channelx.
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;                                                                                                                //Specifies the software priority for the DMAy Channelx
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;                                                                                                                                        //Specifies if the DMAy Channelx will be used in memory-to-memory transfer.
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
        
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC2_CDR_ADDRESS;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC2DualConvertedValue;
        DMA_Init(DMA2_Channel1, &DMA_InitStructure);

  DMA_Cmd(DMA1_Channel1, ENABLE);
  DMA_Cmd(DMA2_Channel1, ENABLE);
}  


void  K_Adc_Init(void)
{         
        ADC_CommonInitTypeDef ADC_CommonInitStructure;
        ADC_InitTypeDef       ADC_InitStructure;         
        GPIO_InitTypeDef                         GPIO_InitStructure;
        
        RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div1);                //配置ADC时钟
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);
                     
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_3|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;        //ADC1 CH124 ADC2 CH234
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;                //模拟输入引脚
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
        GPIO_Init(GPIOA, &GPIO_InitStructure);        
        
  ADC_StructInit(&ADC_InitStructure);

  ADC_VoltageRegulatorCmd(ADC1, ENABLE);
  ADC_VoltageRegulatorCmd(ADC2, ENABLE);
  delay_ms(10);
  
  ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);
  ADC_StartCalibration(ADC1);
  ADC_SelectCalibrationMode(ADC2, ADC_CalibrationMode_Single);
  ADC_StartCalibration(ADC2);
  
  while(ADC_GetCalibrationStatus(ADC1) != RESET );
//  calibration_value_1 = ADC_GetCalibrationValue(ADC1);
  while(ADC_GetCalibrationStatus(ADC2) != RESET );
//  calibration_value_2 = ADC_GetCalibrationValue(ADC2);
   
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;            //ADC independent mode                                                     
  ADC_CommonInitStructure.ADC_Clock = ADC_Clock_SynClkModeDiv1;                                //Synchronous clock mode divided by 1                    
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;          //DMA12~10位分辨率         
  ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular;         //DMA连续模式         
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = 2;                                                                          //采样延迟0~f      
  ADC_CommonInit(ADC1, &ADC_CommonInitStructure);
        ADC_CommonInit(ADC2, &ADC_CommonInitStructure);
  
  ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable;                                //连续转换
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;                                                                                                         //12位精度
  ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;                        //ADC external trigger event 0
  ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;                //ADC No external trigger for regular conversion
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;                                                                                                        //ADC Data alignment right                                                                                                        
  ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;                                                                                //ADC Overrun Mode disable   
  ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;                                                                                        //ADC Auto injected Mode disable  
  ADC_InitStructure.ADC_NbrOfRegChannel = 3;
  ADC_Init(ADC1, &ADC_InitStructure);
  ADC_Init(ADC2, &ADC_InitStructure);
  
  ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_1Cycles5);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 2, ADC_SampleTime_1Cycles5);
        ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 3, ADC_SampleTime_1Cycles5);
        
        ADC_RegularChannelConfig(ADC2, ADC_Channel_2, 1, ADC_SampleTime_1Cycles5);
        ADC_RegularChannelConfig(ADC2, ADC_Channel_3, 2, ADC_SampleTime_1Cycles5);
        ADC_RegularChannelConfig(ADC2, ADC_Channel_4, 3, ADC_SampleTime_1Cycles5);

  ADC_Cmd(ADC1, ENABLE);
  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY));
  ADC_Cmd(ADC2, ENABLE);
  while(!ADC_GetFlagStatus(ADC2, ADC_FLAG_RDY));
        
  ADC_DMACmd(ADC1, ENABLE);
  ADC_DMAConfig(ADC1, ADC_DMAMode_Circular);
        ADC_DMACmd(ADC2, ENABLE);
        ADC_DMAConfig(ADC2, ADC_DMAMode_Circular);

  ADC_StartConversion(ADC1);
        ADC_StartConversion(ADC2);        
}        

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

7

主题

9

帖子

0

粉丝