OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 4914|回复: 0

多个DAC同时使用,DMA怎样配置,缓冲区的大小该给多大,怎样计算

[复制链接]

7

主题

38

帖子

0

精华

初级会员

Rank: 2

积分
134
金钱
134
注册时间
2013-10-6
在线时间
15 小时
发表于 2015-1-21 22:34:49 | 显示全部楼层 |阅读模式
5金钱
麻烦群主和各位热心的朋友了,我最近在学习STM32的ADC,现在想使用ADC1的0 1 2 3 4通道同时工作,然后通过DMA把采样结果送到定义的数组中adc_buf[3],然后显示在TFT上,但是结果只能显示adc_buf[0],且显示的数值乱跳(模拟输入电压没有变),看了半天的数据手册还没有搞懂,不知道DMA中DMA_BufferSize大小到底该设置成多大,该怎样计算,我只是使用一个通道时是正常的

下面这是我的ADC初始化函数和DMA初始化函数
void  Adc_Init(void)
{
ADC_InitTypeDef ADC_InitStructure; 
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1 , ENABLE );  //使能ADC1通道时钟
 

RCC_ADCCLKConfig(RCC_PCLK2_Div6);   //设置ADC分频因子6 72M/6=12,ADC最大时间不能超过14M

//PA1 作为模拟通道输入引脚                         
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入引脚
GPIO_Init(GPIOA, &GPIO_InitStructure);

ADC_DeInit(ADC1);  //复位ADC1,将外设 ADC1 的全部寄存器重设为缺省值

ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC工作模式:ADC1和ADC2工作在独立模式
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数据右对齐
ADC_InitStructure.ADC_NbrOfChannel =4; //顺序进行规则转换的ADC通道的数目
ADC_Init(ADC1, &ADC_InitStructure); //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器   

  
ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1

ADC_ResetCalibration(ADC1); //使能复位校准  
 
while(ADC_GetResetCalibrationStatus(ADC1)); //等待复位校准结束

ADC_StartCalibration(ADC1); //开启AD校准
 
while(ADC_GetCalibrationStatus(ADC1)); //等待校准结束
  ADC_RegularChannelConfig(ADC1,ADC_Channel_0, 1, ADC_SampleTime_239Cycles5 ); //ADC1,ADC通道,采样时间为239.5周期      
  ADC_RegularChannelConfig(ADC1,ADC_Channel_1, 2, ADC_SampleTime_239Cycles5 ); //ADC1,ADC通道,采样时间为239.5周期      
  ADC_RegularChannelConfig(ADC1,ADC_Channel_2, 3, ADC_SampleTime_239Cycles5 ); //ADC1,ADC通道,采样时间为239.5周期      
  ADC_RegularChannelConfig(ADC1,ADC_Channel_3, 4, ADC_SampleTime_239Cycles5 ); //ADC1,ADC通道,采样时间为239.5周期      
  
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //使能指定的ADC1的软件转换启动功能

}   

void Adc_DMAinit()
{

    DMA_InitTypeDef DMA_InitStruct;
  //打开DMA的时钟
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
//
  //参数初始化 )
  DMA_InitStruct.DMA_PeripheralBaseAddr=(0x40012400+0x4c); /*!< Specifies the peripheral base address for DMAy Channelx. */

  DMA_InitStruct.DMA_MemoryBaseAddr=(u32)&adc_buf;     /*!< Specifies the memory base address for DMAy Channelx. */

  DMA_InitStruct.DMA_DIR=DMA_DIR_PeripheralSRC;                /*!< Specifies if the peripheral is the source or destination.
                                        This parameter can be a value of @ref DMA_data_transfer_direction */

  DMA_InitStruct.DMA_BufferSize=64;         /*!< Specifies the buffer size, in data unit, of the specified Channel. 
                                        The data unit is equal to the configuration set in DMA_PeripheralDataSize
                                        or DMA_MemoryDataSize members depending in the transfer direction. */

  DMA_InitStruct.DMA_PeripheralInc=DMA_PeripheralInc_Disable;      /*!< Specifies whether the Peripheral address register is incremented or not.
                                        This parameter can be a value of @ref DMA_peripheral_incremented_mode */
   //DMA_PeripheralInc_Disable;
  DMA_InitStruct.DMA_MemoryInc=DMA_PeripheralInc_Enable;          /*!< Specifies whether the memory address register is incremented or not.
                                        This parameter can be a value of @ref DMA_memory_incremented_mode */

  DMA_InitStruct.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord; /*!< Specifies the Peripheral data width.
                                        This parameter can be a value of @ref DMA_peripheral_data_size */

  DMA_InitStruct.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;     /*!< Specifies the Memory data width.
                                        This parameter can be a value of @ref DMA_memory_data_size */

  DMA_InitStruct.DMA_Mode=DMA_Mode_Circular;               /*!< Specifies the operation mode of the DMAy Channelx.
                                        This parameter can be a value of @ref DMA_circular_normal_mode.
                                        @note: The circular buffer mode cannot be used if the memory-to-memory
                                              data transfer is configured on the selected Channel */

  DMA_InitStruct.DMA_Priority=DMA_Priority_Medium;           /*!< Specifies the software priority for the DMAy Channelx.
                                        This parameter can be a value of @ref DMA_priority_level */

  DMA_InitStruct.DMA_M2M=DMA_M2M_Disable;

  DMA_Init(DMA1_Channel1,&DMA_InitStruct);
 


  ADC_DMACmd(ADC1,ENABLE );
  DMA_Cmd(DMA1_Channel1,ENABLE);
 
}


麻烦原子哥和其他高手们指教

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-10-15 09:57

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表