打印
[Cortex-M0技术交流]

STM32F051 多通道AD读取不正常

[复制链接]
2098|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
风詺祈|  楼主 | 2015-5-12 14:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
大家帮我看看   小弟我单独设置一路AD通道  读取的值是OK的,  但是我多添加几路AD后,读取的AD值就不停的跳变   是不是我读取的方法有问题   请大家帮我指正一下   谢谢!


#include "ADC.h"

__IO uint16_t ADC1ConvertedValue1 = 0,ADC1ConvertedValue2 = 0,
                                                        ADC1ConvertedValue3 = 0,ADC1ConvertedValue4 = 0,
                                                        ADC1ConvertedValue5 = 0;


void Get_ADC_Data(void)
{
        static __IO uint16_t count;
        uint16_t temp;
        if(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) != RESET)
        {                temp= ADC_GetConversionValue(ADC1);
               
                        if((++count)>5)
                                count=1;
                        switch(count)
                        {        case 1:
                                        ADC1ConvertedValue1=temp;
                                break;               
                                case 2:
                                        ADC1ConvertedValue2=temp;                       
                                break;       
                                case 3:
                                        ADC1ConvertedValue3=temp;
                                break;       
                                case 4:       
                                        ADC1ConvertedValue4=temp;
                                break;                       
                                case 5:       
                                        ADC1ConvertedValue5=temp;
                                break;                       
                                default:
                                        ADC1ConvertedValue1=0;
                                        ADC1ConvertedValue2=0;
                                        ADC1ConvertedValue3=0;
                                        ADC1ConvertedValue4=0;
                                        ADC1ConvertedValue5=0;
                                break;
                        }
        }
}


void ADC1_Config(void)
{
  ADC_InitTypeDef          ADC_InitStructure;
  GPIO_InitTypeDef         GPIO_InitStructure;

        /* ADC1 Configuration *******************************************************/
  /* ADCs DeInit */  
  ADC_DeInit(ADC1);
       
  /* GPIOA Periph clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  
  /* ADC1 Periph clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  
  /* Configure ADC Channel_11 as analog input */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  
/* Initialize ADC structure */
  ADC_StructInit(&ADC_InitStructure);
  
  /* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits*/
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;   
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; //ÏòǰɨÃè  0-18
  ADC_Init(ADC1, &ADC_InitStructure);
  
  /* Convert the ADC1 Channel 11 with 13.5 Cycles as sampling time */
  ADC_ChannelConfig(ADC1, ADC_Channel_0, ADC_SampleTime_28_5Cycles);   
        ADC_ChannelConfig(ADC1, ADC_Channel_1, ADC_SampleTime_28_5Cycles);
        ADC_ChannelConfig(ADC1, ADC_Channel_2, ADC_SampleTime_28_5Cycles);
        ADC_ChannelConfig(ADC1, ADC_Channel_3, ADC_SampleTime_28_5Cycles);
  ADC_ChannelConfig(ADC1, ADC_Channel_4, ADC_SampleTime_28_5Cycles);   
       
  /* ADC Calibration */
  ADC_GetCalibrationFactor(ADC1);                //adjust adc

  /* Enable ADCperipheral[PerIdx] */
  ADC_Cmd(ADC1, ENABLE);     
  
  /* Wait the ADCEN falg */
  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
  
  /* ADC1 regular Software Start Conv */
  ADC_StartOfConversion(ADC1);
}

相关帖子

沙发
lvyunhua| | 2015-5-14 08:51 | 只看该作者
你添加的几路AD实际电压有没有变化啊?

使用特权

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

本版积分规则

6

主题

8

帖子

0

粉丝