STM32F4双ADC采样ADC2无数据

2019-03-23 19:08发布

最近在调STM32F439的板子,想用双ADC同时采样,发现ADC1数据正常,ADC2无数据,求大神指点。
下边是程序代码:
uint16_t ADC_Value[8];
#define ADC1_DR_ADDR  0x4001204C      //ADC1_DR 地址
static void Init_ADC_GPIO(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    /**************************************************
                ADC1-8个通道  ADC2-8个通道
                   ADC1_IN0~IN7--PA0~PA7
                   ADC2_IN8~IN9--PB0~PB1
                   ADC2_IN10~IN15--PC0~PC5
    *************************************************/
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
   
    GPIO_InitStructure.GPIO_Pin = (uint16_t)0x00ff;//PA0~PA7
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
   
    GPIO_InitStructure.GPIO_Pin = (uint16_t)0x0003;//PB0~PB1
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
   
    GPIO_InitStructure.GPIO_Pin = (uint16_t)0x003f;//PC0~PC5
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
}
//DMA2
static void Init_DMA2_Channel0_Stream0(void)
{
    DMA_InitTypeDef DMA_InitStructure;
   
    int i = 0;   
    for(i=0;i<8;i++)   //初始化ADC_Value
        ADC_Value = 0;
   
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
    DMA_DeInit(DMA2_Stream0);
   
    DMA_InitStructure.DMA_Channel = DMA_Channel_0;
    DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_ADDR;
    DMA_InitStructure.DMA_Memory0BaseAddr = (unsigned int)ADC_Value;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
    DMA_InitStructure.DMA_BufferSize = 8;
    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_FIFOMode = DMA_FIFOMode_Disable;
    DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
    DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;   
    DMA_Init(DMA2_Stream0, &DMA_InitStructure);
    DMA_Cmd(DMA2_Stream0, ENABLE);
}
void Init_ADC(void)
{
    ADC_InitTypeDef ADC_InitStructure;
    ADC_CommonInitTypeDef ADC_CommonInitStructure;
   
    Init_ADC_GPIO();
    Init_DMA2_Channel0_Stream0();
   
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
    ADC_DeInit();
    //初始化ADC Common
    ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult;
    ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
    ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_3;
    ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
    ADC_CommonInit(&ADC_CommonInitStructure);
   
    ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b;
    ADC_InitStructure.ADC_ScanConvMode = ENABLE;
    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
    ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
    //ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfConversion = 8;
   
    ADC_Init(ADC1, &ADC_InitStructure);
   
    ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b;
    ADC_InitStructure.ADC_ScanConvMode = ENABLE;
    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
    ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
    //ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfConversion = 8;
    ADC_Init(ADC2, &ADC_InitStructure);   
   
    ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_15Cycles);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_15Cycles);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_15Cycles);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_15Cycles);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 5, ADC_SampleTime_15Cycles);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 6, ADC_SampleTime_15Cycles);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 7, ADC_SampleTime_15Cycles);
    ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 8, ADC_SampleTime_15Cycles);
   
    ADC_RegularChannelConfig(ADC2, ADC_Channel_8, 1, ADC_SampleTime_15Cycles);
    ADC_RegularChannelConfig(ADC2, ADC_Channel_9, 2, ADC_SampleTime_15Cycles);
    ADC_RegularChannelConfig(ADC2, ADC_Channel_10, 3, ADC_SampleTime_15Cycles);
    ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 4, ADC_SampleTime_15Cycles);
    ADC_RegularChannelConfig(ADC2, ADC_Channel_12, 5, ADC_SampleTime_15Cycles);
    ADC_RegularChannelConfig(ADC2, ADC_Channel_13, 6, ADC_SampleTime_15Cycles);
    ADC_RegularChannelConfig(ADC2, ADC_Channel_14, 7, ADC_SampleTime_15Cycles);
    ADC_RegularChannelConfig(ADC2, ADC_Channel_15, 8, ADC_SampleTime_15Cycles);
   
    ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);
    ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
    ADC_DMARequestAfterLastTransferCmd(ADC2, ENABLE);
   
    ADC_DMACmd(ADC1, ENABLE);//使能ADC1_DMA
    ADC_DMACmd(ADC2, ENABLE);
   
    ADC_Cmd(ADC1, ENABLE);  
    ADC_Cmd(ADC2, ENABLE);
}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
11条回答
baixichi
1楼-- · 2019-03-24 10:56
DMA2_Stream0的定义库函数里边是有的,而且F4里边现在也用的Stream
1.jpg
   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
这两句为什么用半字传输不对。我采用的是DMA mode3,ADC分辨率为8位
MCKW{AU}CXK`T{`7BS))QBM.jpg
ADC_ExternalTrigConvCmd(ADC2, ENABLE); 研究一下先。
Study_Stellaris
2楼-- · 2019-03-24 15:48
DMA_PeripheralDataSize_Word
这个数据大小应该没影响,不过按理你应该用 Half_World 才对呀,因为你的数据总共是 16 bit 的.
检查一下,看 DMA 是否正常读到了数据.
我用的标准库是 3.5 的没看到有 stream 什么的.
  1.     DMA_DeInit(DMA2_Stream0);
  2.    
  3.     DMA_InitStructure.DMA_Channel = DMA_Channel_0;
  4.     DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_ADDR;
  5.     DMA_InitStructure.DMA_Memory0BaseAddr = (unsigned int)ADC_Value;
  6.     DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  7.     DMA_InitStructure.DMA_BufferSize = 8;
复制代码DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA2 可以读取 DMA_Channel_0 的数据吗?确认一下.我用 STM32 用的是 DAM1
    DMA_InitStructure.DMA_BufferSize = 8;
怎么是 8 呢?不是应该是 16 吗?

[ 本帖最后由 Study_Stellaris 于 2013-8-20 11:19 编辑 ]
Li_Lei
3楼-- · 2019-03-24 16:52
 精彩回答 2  元偷偷看……
baixichi
4楼-- · 2019-03-24 22:17
原来是DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_ADDR;这里出错了
不应该是ADC1_DR的地址,应该是ADC1_CDR的地址
liwanxi
5楼-- · 2019-03-24 22:59
没看明白

一周热门 更多>