打印

STM32F303 OPAMP+ADC需要怎么配置才能使用?

[复制链接]
7027|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
qycc566|  楼主 | 2013-11-15 21:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我使用STM32F303的OPAMP1至4(配置为跟随器模式)连接到ADC1至ADC4,OPAMP的同相输入分别为PA1, PD14, PB0, PB11。但ADC的结果总是不会随输入的变化而变化。配置代码如下:

OPAMP_InitTypeDef OPAMP_InitStructure;
        ADC_InitTypeDef ADC_InitStructure;
        ADC_CommonInitTypeDef ADC_CommonInitStructure;
        /* OPAMP Peripheral clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);       
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
        GPIO_Init(GPIOC, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_8;
        GPIO_Init(GPIOD, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_10 | GPIO_Pin_10;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
       
        /* OPAMP1 config */
  OPAMP_InitStructure.OPAMP_NonInvertingInput = OPAMP_NonInvertingInput_IO4;
  OPAMP_InitStructure.OPAMP_InvertingInput =  OPAMP_InvertingInput_Vout;
  OPAMP_Init(OPAMP_Selection_OPAMP1, &OPAMP_InitStructure);

  /* Configure OPAMP1 in PGA mode with gain set to 2 */
  //OPAMP_PGAConfig(OPAMP_Selection_OPAMP1, OPAMP_OPAMP_PGAGain_2, OPAMP_PGAConnect_No);

  /* Enable OPAMP1 */
  OPAMP_Cmd(OPAMP_Selection_OPAMP1, ENABLE);
       
        OPAMP_InitStructure.OPAMP_NonInvertingInput = OPAMP_NonInvertingInput_IO1;
        OPAMP_Init(OPAMP_Selection_OPAMP2, &OPAMP_InitStructure);
        //OPAMP_PGAConfig(OPAMP_Selection_OPAMP2, OPAMP_OPAMP_PGAGain_2, OPAMP_PGAConnect_No);
        OPAMP_Cmd(OPAMP_Selection_OPAMP2, ENABLE);
       
        OPAMP_InitStructure.OPAMP_NonInvertingInput = OPAMP_NonInvertingInput_IO4;
        OPAMP_Init(OPAMP_Selection_OPAMP3, &OPAMP_InitStructure);
        //OPAMP_PGAConfig(OPAMP_Selection_OPAMP3, OPAMP_OPAMP_PGAGain_2, OPAMP_PGAConnect_No);
        OPAMP_Cmd(OPAMP_Selection_OPAMP3, ENABLE);
       
        OPAMP_InitStructure.OPAMP_NonInvertingInput = OPAMP_NonInvertingInput_IO2;
        OPAMP_Init(OPAMP_Selection_OPAMP4, &OPAMP_InitStructure);
        //OPAMP_PGAConfig(OPAMP_Selection_OPAMP4, OPAMP_OPAMP_PGAGain_2, OPAMP_PGAConnect_No);
        OPAMP_Cmd(OPAMP_Selection_OPAMP4, ENABLE);
       
        /* Configure the ADC clock */
  RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div1);  
        RCC_ADCCLKConfig(RCC_ADC34PLLCLK_Div1);  
  /* Enable ADC clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC34, ENABLE);
       
        /* ADC DMA Configuration */
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
       
        DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(ADC1->DR));
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)(AdcConvertedValues.u16AdcBuff);
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  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_Word;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
       
        DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(ADC2->DR));
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&(AdcConvertedValues.u16AdcBuff[1]);
        DMA_Init(DMA2_Channel1, &DMA_InitStructure);
       
        DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(ADC3->DR));
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&(AdcConvertedValues.u16AdcBuff[2]);       
        DMA_Init(DMA2_Channel5, &DMA_InitStructure);
       
        DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(ADC4->DR));
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&(AdcConvertedValues.u16AdcBuff[3]);
        DMA_Init(DMA2_Channel2, &DMA_InitStructure);

        ADC_StructInit(&ADC_InitStructure);

  /* ADC Calibration procedure */
  ADC_VoltageRegulatorCmd(ADC1, ENABLE);
  ADC_VoltageRegulatorCmd(ADC2, ENABLE);
        ADC_VoltageRegulatorCmd(ADC3, ENABLE);
        ADC_VoltageRegulatorCmd(ADC4, ENABLE);
        DelayUs(1000);
       
        ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);
  ADC_StartCalibration(ADC1);

  ADC_SelectCalibrationMode(ADC2, ADC_CalibrationMode_Single);
  ADC_StartCalibration(ADC2);
       
        ADC_SelectCalibrationMode(ADC3, ADC_CalibrationMode_Single);
  ADC_StartCalibration(ADC3);
       
        ADC_SelectCalibrationMode(ADC4, ADC_CalibrationMode_Single);
  ADC_StartCalibration(ADC4);
  
  while(ADC_GetCalibrationStatus(ADC1) != RESET );
  calibration_value_1 = ADC_GetCalibrationValue(ADC1);

  while(ADC_GetCalibrationStatus(ADC2) != RESET );
  calibration_value_2 = ADC_GetCalibrationValue(ADC2);
       
        while(ADC_GetCalibrationStatus(ADC3) != RESET );
  calibration_value_3 = ADC_GetCalibrationValue(ADC3);
       
        while(ADC_GetCalibrationStatus(ADC4) != RESET );
  calibration_value_4 = ADC_GetCalibrationValue(ADC4);

        ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;                    
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;            
  ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular;                  
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = 10;         
  
  ADC_CommonInit(ADC1, &ADC_CommonInitStructure);
        ADC_CommonInit(ADC2, &ADC_CommonInitStructure);
        ADC_CommonInit(ADC3, &ADC_CommonInitStructure);
        ADC_CommonInit(ADC4, &ADC_CommonInitStructure);
  
  ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable;
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;         
  ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;   
  ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;  
  ADC_InitStructure.ADC_NbrOfRegChannel = 1;
  ADC_Init(ADC1, &ADC_InitStructure);
        ADC_Init(ADC2, &ADC_InitStructure);
        ADC_Init(ADC3, &ADC_InitStructure);
        ADC_Init(ADC4, &ADC_InitStructure);
       
        ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 1, ADC_SampleTime_7Cycles5);
        ADC_RegularChannelConfig(ADC2, ADC_Channel_3, 1, ADC_SampleTime_7Cycles5);
        ADC_RegularChannelConfig(ADC3, ADC_Channel_1, 1, ADC_SampleTime_7Cycles5);
        ADC_RegularChannelConfig(ADC4, ADC_Channel_3, 1, ADC_SampleTime_7Cycles5);
       
       
        ADC_DMAConfig(ADC1, ADC_DMAMode_Circular);  
        ADC_DMAConfig(ADC2, ADC_DMAMode_Circular);
        ADC_DMAConfig(ADC3, ADC_DMAMode_Circular);  
        ADC_DMAConfig(ADC4, ADC_DMAMode_Circular);
  ADC_DMACmd(ADC1, ENABLE);
        ADC_DMACmd(ADC2, ENABLE);
        ADC_DMACmd(ADC3, ENABLE);
        ADC_DMACmd(ADC4, ENABLE);
       
        ADC_Cmd(ADC1, ENABLE);
        ADC_Cmd(ADC2, ENABLE);
        ADC_Cmd(ADC3, ENABLE);
        ADC_Cmd(ADC4, ENABLE);
       
       
       
  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY));  
  while(!ADC_GetFlagStatus(ADC2, ADC_FLAG_RDY));
        while(!ADC_GetFlagStatus(ADC3, ADC_FLAG_RDY));
        while(!ADC_GetFlagStatus(ADC4, ADC_FLAG_RDY));
  
  /* Enable the DMA channel */
  DMA_Cmd(DMA1_Channel1, ENABLE);
        DMA_Cmd(DMA2_Channel1, ENABLE);
        DMA_Cmd(DMA2_Channel2, ENABLE);
        DMA_Cmd(DMA2_Channel5, ENABLE);
  
  ADC_StartConversion(ADC1);
        ADC_StartConversion(ADC2);
        ADC_StartConversion(ADC3);
        ADC_StartConversion(ADC4);


沙发
linfeng24| | 2013-11-16 00:40 | 只看该作者
OPMAP?不好意思,额,表示没用F303系列的,帮你顶下。

使用特权

评论回复
板凳
ccsiu_dx| | 2016-3-3 10:21 | 只看该作者
请问你这问题解决了吗?

使用特权

评论回复
地板
haozhengnaxi| | 2017-11-20 11:40 | 只看该作者
请问解决了吗?

使用特权

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

本版积分规则

3

主题

3

帖子

0

粉丝