STM32f030使用DMA进行ADC采集问题

2019-07-21 02:43发布

主控制芯片采用STM32f030C8T6进行三个通道ADC采集使用的端口分别是PA1,PA4,PA5
采集出来数据为两个最大值,这三个值都不正确,PA4调节电压,打印出来的数据还是可以波动的。
[14:45:37.612]收←◆PA0:4095,PA4:2718,PA5:4095
[14:45:38.112]收←◆PA0:4095,PA4:2714,PA5:4095
[14:45:38.613]收←◆PA0:4095,PA4:2711,PA5:4095
[14:45:39.112]收←◆PA0:4095,PA4:2708,PA5:4095
[14:45:39.612]收←◆PA0:4095,PA4:2719,PA5:4095
[14:45:40.112]收←◆PA0:4095,PA4:2709,PA5:4095
[14:45:40.612]收←◆PA0:4095,PA4:2707,PA5:4095
[14:45:41.112]收←◆PA0:4095,PA4:2713,PA5:4095
[14:45:41.612]收←◆PA0:4095,PA4:2715,PA5:4095
[14:45:42.112]收←◆PA0:4095,PA4:2714,PA5:4095
[14:45:42.612]收←◆PA0:4095,PA4:2716,PA5:4095
[14:45:43.112]收←◆PA0:4095,PA4:2708,PA5:4095
[14:45:43.612]收←◆PA0:4095,PA4:2713,PA5:4095
[14:45:44.112]收←◆PA0:4095,PA4:2715,PA5:4095
[14:45:44.612]收←◆PA0:4095,PA4:2715,PA5:4095
[14:45:45.112]收←◆PA0:4095,PA4:2710,PA5:4095
[14:45:45.612]收←◆PA0:4095,PA4:2710,PA5:4095

具体端口配置代码如下:

/* Private typedef -----------------------------------------------------------*/
/*                  ADC读取                                                   */
/* Private define ------------------------------------------------------------*/

#include "adc.h"
#include "ADC_libr.h"
//#include "algorithm.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC1_DR_Address                ((uint32_t)0x40012440)


__IO uint16_t Read_ADC[3] = {0};

// 局部变量,用于存从flash读到的电压值            
__IO float ADC_ConvertedValueLocal;  

void ADC1_DMA_Init(void)
{
      ADC_InitTypeDef     ADC_InitStruct;
  DMA_InitTypeDef     DMA_InitStruct;
    GPIO_InitTypeDef    GPIO_InitStruct;
      /* ADC1 DeInit */  
  ADC_DeInit(ADC1);

    /* Enable  GPIOA clock */
      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  /* ADC1 Periph clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  /* DMA1 clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);

  /* Configure PA.01  as analog input */
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_4|GPIO_Pin_5;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOA, &GPIO_InitStruct);               


  /* DMA1 Channel1 Config */
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
  DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)Read_ADC;
  DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStruct.DMA_BufferSize = 3;
  DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStruct.DMA_Priority = DMA_Priority_High;
  DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStruct);
  
  /* DMA1 Channel1 enable */
  DMA_Cmd(DMA1_Channel1, ENABLE);
  
//   /* ADC DMA request in circular mode */
  ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
  
  /* Enable ADC_DMA */
  ADC_DMACmd(ADC1, ENABLE);  
  
  /* Initialize ADC structure */
  ADC_StructInit(&ADC_InitStruct);
  
  /* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits  */
  ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
//  ADC_InitStruct.ADC_ExternalTrigConv =  ADC_ExternalTrigConvEdge_None;
  ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStruct.ADC_ScanDirection = ADC_ScanDirection_Backward;
  ADC_Init(ADC1, &ADC_InitStruct);

//   /* Convert the ADC1 temperature sensor  with 55.5 Cycles as sampling time */
//   ADC_ChannelConfig(ADC1, ADC_Channel_TempSensor , ADC_SampleTime_55_5Cycles);  
//   ADC_TempSensorCmd(ENABLE);
  
  /* Convert the ADC1 Vref  with 55.5 Cycles as sampling time */
  ADC_ChannelConfig(ADC1, ADC_Channel_1|ADC_Channel_4|ADC_Channel_5,ADC_SampleTime_55_5Cycles);
//   ADC_VrefintCmd(ENABLE);
  
  /* ADC Calibration */
  ADC_GetCalibrationFactor(ADC1);
   ADC_DMACmd(ADC1, ENABLE);
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);     
  
  /* Wait the ADCEN falg */
  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
  
  /* ADC1 regular Software Start Conv */
  ADC_StartOfConversion(ADC1);
}


/***
ADC读取测试函数
DS 1 读取PT100的程序
DS 2 读取热电偶的程序
*/

WORD _Read_Temp(BYTE DS)
{
    static float read_adc = 100;
    static WORD RES = 0;
    static WORD Temp_return = 0;
    switch(DS)
    {   
        case 1:
            read_adc = Read_ADC[0];
            Temp_return = read_adc;
        break;
        case 2:
            read_adc = Read_ADC[1];
            Temp_return = read_adc;            
        break;
        case 3:
            read_adc = Read_ADC[2];
            Temp_return = read_adc;
        break;
        default:
            
        break;
    }
   
    return Temp_return;
   
}







友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
8条回答
edmund1234
1楼-- · 2019-07-21 06:21
 精彩回答 2  元偷偷看……
peng1554
2楼-- · 2019-07-21 10:32
 精彩回答 2  元偷偷看……
yklstudent
3楼-- · 2019-07-21 11:03
ADC_ChannelConfig(ADC1, ADC_Channel_1|ADC_Channel_4|ADC_Channel_5,ADC_SampleTime_55_5Cycles);
拆开:
ADC_ChannelConfig(ADC1, ADC_Channel_1,ADC_SampleTime_55_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_4,ADC_SampleTime_55_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_5,ADC_SampleTime_55_5Cycles);
何伦
4楼-- · 2019-07-21 15:22
yklstudent 发表于 2019-4-10 20:41
ADC_ChannelConfig(ADC1, ADC_Channel_1|ADC_Channel_4|ADC_Channel_5,ADC_SampleTime_55_5Cycles);
拆开 ...

拆开后依然无法解决
何伦
5楼-- · 2019-07-21 19:04
yklstudent 发表于 2019-4-10 20:41
ADC_ChannelConfig(ADC1, ADC_Channel_1|ADC_Channel_4|ADC_Channel_5,ADC_SampleTime_55_5Cycles);
拆开 ...

[11:17:57.303]收←◆PA0:4092,PA4:2653,PA5:4095
[11:17:57.803]收←◆PA0:4091,PA4:2655,PA5:4095
[11:17:58.303]收←◆PA0:4092,PA4:2657,PA5:4095
[11:17:58.803]收←◆PA0:4088,PA4:2652,PA5:4095
[11:17:59.303]收←◆PA0:4091,PA4:2658,PA5:4095
[11:17:59.804]收←◆PA0:4093,PA4:2656,PA5:4095
[11:18:00.304]收←◆PA0:4088,PA4:2658,PA5:4095
[11:18:00.804]收←◆PA0:4094,PA4:2654,PA5:4095
[11:18:01.303]收←◆PA0:4092,PA4:2658,PA5:4095
[11:18:01.804]收←◆PA0:4092,PA4:2661,PA5:4095
[11:18:02.303]收←◆PA0:4091,PA4:2661,PA5:4095
[11:18:02.803]收←◆PA0:4092,PA4:2661,PA5:4095
[11:18:03.303]收←◆PA0:4094,PA4:2660,PA5:4095
[11:18:03.803]收←◆PA0:4089,PA4:2661,PA5:4095
[11:18:04.303]收←◆PA0:4092,PA4:2657,PA5:4095


拆开后打印出来的数据,正常输出应该为PA0:0,PA4:2000多,PA5:2000多
何伦
6楼-- · 2019-07-22 00:14
 精彩回答 2  元偷偷看……

一周热门 更多>