原创 STM32_DMA+ADC多路采集(错位问题)

2013-6-4 21:59 6333 9 10 分类: MCU/ 嵌入式 文集: ST-NXP

使用时遇到的问题:

 

我使用是的STM32F103ZET6,同时使用ADC1的16个通道,ADC3的5个通道,都使用DMA来传输,问题:比如ADC1_CH0通道上的值保存到了Buffer[1],其他数据往后移,ADC1_CH15的数据出现在Buffer[0]上,更不可失意的的是,有是ADC1_CH0通道的值出现在Buffer[2]上,其他的往后移,ADC1_CH14,ADC1_CH15则出现在Buffer[0],Buffer[1]上。请大家是否有朋友也碰到过。

总结:(1)一定要配置好ADC之后,才使能DMA

(2)ADC首先要进行默认初始化。

源代码:

(1)

uint16_t ADC1Buffer[16];

uint16_t ADC3Buffer[5];
 
#define ADC1_DR_Address    ((uint32_t)0x4001244C)
#define ADC3_DR_Address    ((uint32_t)0x40013C4C)
#define ADC1CounvNum (16)
#define ADC3CounvNum (5)
 
 
/**************************************************************************
**函数功能 :DMA的配置文件
**入口参数 :无
**出口参数 :无
**调用本函数:main.c
**************************************************************************/
void DMA_Config(void)
{
 
DMA_InitTypeDef DMA_InitStructure;
 
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 | RCC_AHBPeriph_DMA2, ENABLE);
 
/* DMA1 channel1 configuration ----------------------------------------------*/
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC1Buffer;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = ADC1CounvNum;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);  
  /* Enable DMA1 channel1 */
  //DMA_Cmd(DMA1_Channel1, ENABLE);
 
  /* DMA2 channel5 configuration ----------------------------------------------*/
  DMA_DeInit(DMA2_Channel5);
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC3_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC3Buffer;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = ADC3CounvNum;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA2_Channel5, &DMA_InitStructure);  
  /* Enable DMA2 channel5 */
 // DMA_Cmd(DMA2_Channel5, ENABLE);
 
}
 
(2)
#include "stm32f10x.h"
#include "ADC_Init.h"
 
 
#define ConvSuccess (1)
#define ADC1CounvNum (16)
#define ADC3CounvNum (5)
 
/**************************************************************************
**函数功能 :ADC的配置文件
**入口参数 :无
**出口参数 :无
**调用本函数:main.c
**************************************************************************/
void ADC_Config(void)
{
 
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
 
 // RCC_ADCCLKConfig(RCC_PCLK2_Div4); 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC3| RCC_APB2Periph_GPIOA
| RCC_APB2Periph_GPIOB| RCC_APB2Periph_GPIOC| RCC_APB2Periph_GPIOF, ENABLE);
 
  /* Configure PA  as analog input -------------------------*/
  GPIO_InitStructure.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_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 
 /* Configure PB  as analog input -------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
 
 /* Configure PC  as analog input -------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3
|GPIO_Pin_4|GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
 
 
 /* Configure PF  as analog input -------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9
|GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOF, &GPIO_InitStructure);
 
ADC_DeInit(ADC1);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = ADC1CounvNum;
  ADC_Init(ADC1, &ADC_InitStructure);
 
 /* ADC1 regular channel5 configuration */ 
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 5, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 6, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 7, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 8, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 9, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 10, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_10,11, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_11,12, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_12,13, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_13,14, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_14,15, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_15,16, ADC_SampleTime_55Cycles5);
 
 /* Enable ADC1 DMA */
//  ADC_DMACmd(ADC1, ENABLE);
ADC_DeInit(ADC3);
/* ADC3 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = ADC3CounvNum;
  ADC_Init(ADC3, &ADC_InitStructure);
 
  /* ADC3 regular channel14 configuration */ 
  ADC_RegularChannelConfig(ADC3, ADC_Channel_4, 1, ADC_SampleTime_7Cycles5);
ADC_RegularChannelConfig(ADC3, ADC_Channel_5, 2, ADC_SampleTime_7Cycles5);
ADC_RegularChannelConfig(ADC3, ADC_Channel_6, 3, ADC_SampleTime_7Cycles5);
ADC_RegularChannelConfig(ADC3, ADC_Channel_7, 4, ADC_SampleTime_7Cycles5);
ADC_RegularChannelConfig(ADC3, ADC_Channel_8, 5, ADC_SampleTime_7Cycles5);
 
  /* Enable ADC3 DMA */
  //ADC_DMACmd(ADC3, ENABLE);
 
 /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE);
 /* Enable ADC1 reset calibration register */   
  ADC_ResetCalibration(ADC1);
  /* Check the end of ADC1 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC1));
 
  /* Start ADC1 calibration */
  ADC_StartCalibration(ADC1);
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));
 
/* Enable ADC3 */
  ADC_Cmd(ADC3, ENABLE);
ADC_DMACmd(ADC3, ENABLE);
  /* Enable ADC3 reset calibration register */   
  ADC_ResetCalibration(ADC3);
  /* Check the end of ADC3 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC3));
 
  /* Start ADC3 calibration */
  ADC_StartCalibration(ADC3);
  /* Check the end of ADC3 calibration */
  while(ADC_GetCalibrationStatus(ADC3));
 
DMA_Cmd(DMA1_Channel1, ENABLE);
 /* Start ADC1 Software Conversion */ 
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
DMA_Cmd(DMA2_Channel5, ENABLE);
/* Start ADC3 Software Conversion */ 
  ADC_SoftwareStartConvCmd(ADC3, ENABLE);
 
}

 

文章评论1条评论)

登录后参与讨论

用户377235 2015-12-22 18:24

采样时间太短了
相关推荐阅读
用户1674306 2013-12-19 10:19
UIP最好的入门文章
一引言 近几年来,人们对连接各种装置到一个现有的IP网络例如因特网上产生了浓厚的兴趣。为了可以通过因特网通讯,一个可实现的TCP/IP协议栈是必须的。对于由32位嵌入式处理器构建的中、高端网络...
用户1674306 2013-12-18 14:12
STM32中printf函数
  标准库函数的默认输出设备是显示器,要实现在串口或LCD输出,必须重定义标准库函数里调用的与输出设备相关的函数. 例如:printf输出到串口,需...
用户1674306 2013-11-13 09:52
SourceInsight 字体间隔太大
英文版:    1.Options->Style Properties     2.在左边Style Name下找到Comment Multi Line.在其右边对应的Font属...
用户1674306 2013-08-22 18:11
肖特基二极管与普通二极管的区别
工作多年了,但对肖特基(Schottky)二极管的工作机理却不太明了,网上收集资料,整理如下: 两种二极管都是单向导电,可用于整流场合。区别是普通硅二极管的耐压可以做得较高,但是它的恢复速度低...
用户1674306 2013-08-22 11:41
晶振问题
1、单片机晶振不起振原因分析 遇到单片机晶振不起振是常见现象,那么引起晶振不起振的原因有哪些呢? (1) PCB板布线错误; (2) 单片机质量有问题; (3) 晶振质量有问题; (4)...
用户1674306 2013-06-05 20:05
离奇的上电故障雷倒众高手
有位用户,用我们的E3641A电源为被测件供电。 被测件不复杂,包括了单片机、电容和电阻等, 供电电压是25V, 稳定正常工作电流是50-55mA。用户通过GPIB接口对电源进行编程, 把电压设在...
我要评论
1
9
关闭 站长推荐上一条 /2 下一条