鸿 网 互 联 www.68idc.cn

记STM32F030多通道ADC DMA读取乱序问题

来源:互联网 作者:佚名 时间:2018-02-03 17:15
问题描述 通过 uint16_t ConvData[8]保存DMA搬运的ADC转换数值,但是这个数组数值的顺序总是和ADC不是顺序对应的。比如用7个通道的ADC,当设置ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward,是对应顺序是:0-0,1-7,2-67-1 ; 当设置AD
  • 问题描述
    通过 uint16_t ConvData[8]保存DMA搬运的ADC转换数值,但是这个数组数值的顺序总是和ADC不是顺序对应的。比如用7个通道的ADC,当设置ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward,是对应顺序是:0->0,1->7,2->6…7->1 ; 当设置ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward,是对应顺序是:0->7,1->0,2->1…7->6 。
  • 问题原因
    F0的ADC在使用之前需要校准。这个7位的校准值也是放在ADC_DR中的,它也会触发DMA请求。
    可以参照F0的ADC-DMA例程先做ADC校准、然后再设置DMA,再使能ADC的DMA。
  • 实例代码
void ADC1_DMA_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;
    ADC_InitTypeDef ADC_InitStructure;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

    GPIO_InitStructure.GPIO_Pin = 0x00ff;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

    ADC_DeInit(ADC1); //ADC恢复默认设置

    ADC_StructInit(&ADC_InitStructure); //初始化ADC结构

    ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //12位精度
    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; // ADC_ScanDirection_Backward; //ADC的扫描方向
    ADC_Init(ADC1, &ADC_InitStructure);

    ADC_ChannelConfig(ADC1, ADC_Channel_0, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */
    ADC_ChannelConfig(ADC1, ADC_Channel_1, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */
    ADC_ChannelConfig(ADC1, ADC_Channel_2, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */
    ADC_ChannelConfig(ADC1, ADC_Channel_3, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */
    ADC_ChannelConfig(ADC1, ADC_Channel_4, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */
    ADC_ChannelConfig(ADC1, ADC_Channel_5, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */
    ADC_ChannelConfig(ADC1, ADC_Channel_6, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */
    ADC_ChannelConfig(ADC1, ADC_Channel_7, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */

    ADC_GetCalibrationFactor(ADC1); /* ADC Calibration */
    ADC_Cmd(ADC1, ENABLE); /* Enable ADCperipheral[PerIdx] */
    while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY))
        ; /* Wait the ADCEN falg */

    //设置DMA要在校准ADC之后
    DMA_DeInit(DMA1_Channel1); /* DMA1 Channel1 Config */
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) 0x40012440; //ADC1->DR; //外设地址
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) RegularConvData_Tab; //内存地址
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //外设作为数据传输的来源
    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; //数据宽度为16位
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //数据宽度为16位
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //DMA_Mode_Circular;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA_Priority设定DMA通道x的软件优先级
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA通道x没有设置为内存到内存传输
    DMA_Init(DMA1_Channel1, &DMA_InitStructure);

    DMA_Cmd(DMA1_Channel1, ENABLE);/* DMA1 Channel1 enable */
    DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
    ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); /* Enable ADC_DMA */
    ADC_DMACmd(ADC1, ENABLE);

    ADC_StartOfConversion(ADC1); /* ADC1 regular Software Start Conv */

}
网友评论
<