问答

汇集网友智慧,解决技术难题

21ic问答首页 - GD32F470双ADC采集问题

DMA ma adc采集 GD32F470 嵌入式 国产芯片

GD32F470双ADC采集问题

小炎宇2024-09-30
本帖最后由 小炎宇 于 2024-9-30 12:38 编辑

在使用GD32F470的ADC0和ADC2分别采集数据,并使能DMA传输数据时,发现采集到4个通道的数据,总会有一个通道的数据是0.但每次采集数据为0的通道并不固定,于是放大了DMA存储数据的目标地址缓冲区大小,发现,通道数据为0的位置已经固定且很有规律,不知道是哪里配置的问题,adc初始化时选择的模式是“全部ADC规则并行模式”(因为工程的宏中没有ADC0与ADC2规则并行模式),想请问出现这种情况是否是,把未使能的ADC1的通道数据也算上了,导致每次采集数据,三个通道轮番采集了一遍,还是说数据存储格式有问题,烦请各位大佬帮忙查看一下,下面是初始化代码和采集到的数据的现象。代码:

uint16_t ga_adc_dmabuf[ADC_SAMPLE_CH_BUTT] = {0};  //ADC_SAMPLE_CH_BUTT原本为4,现在为6

  void dma_config (void)
{
    /* ADC_DMA_channel configuration */
    dma_single_data_parameter_struct dma_data_parameter;

    /* enable DMA1 clock */
    rcu_periph_clock_enable (RCU_DMA1);
    dma_deinit (DMA1, DMA_CH0);

    /* initialize DMA single data mode */
    dma_data_parameter.periph_addr = (uint32_t) (&ADC_SYNCDATA);
    dma_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
    dma_data_parameter.memory0_addr = (uint32_t)ga_adc_dmabuf;
    dma_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
    dma_data_parameter.periph_memory_width = DMA_PERIPH_WIDTH_16BIT;
    dma_data_parameter.circular_mode = DMA_CIRCULAR_MODE_ENABLE;
    dma_data_parameter.direction = DMA_PERIPH_TO_MEMORY;
    dma_data_parameter.number = ADC_SAMPLE_CH_BUTT;//现在扩大为6,原本只采集4个
    dma_data_parameter.priority = DMA_PRIORITY_HIGH;
    dma_single_data_mode_init (DMA1, DMA_CH0, &dma_data_parameter);

    /* DMA channel 0 peripheral select */
    dma_channel_subperipheral_select (DMA1, DMA_CH0, DMA_SUBPERI0);

    /* enable DMA channel */
    dma_channel_enable (DMA1, DMA_CH0);
}
int cal_adc_init (void)
{
    rcu_periph_clock_enable (RCU_GPIOB);
    rcu_periph_clock_enable (RCU_GPIOF);

    cal_gpio_init (GPIOB, GPIO_PIN_0, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, 0);
    cal_gpio_init (GPIOB, GPIO_PIN_1, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, 0);
    cal_gpio_init (GPIOF, GPIO_PIN_6, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, 0);
    cal_gpio_init (GPIOF, GPIO_PIN_7, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, 0);

    /* enable ADC clock */
    rcu_periph_clock_enable (RCU_ADC0);
    rcu_periph_clock_enable (RCU_ADC2);
    adc_clock_config (ADC_ADCCK_PCLK2_DIV4);

    adc_deinit();
    adc_sync_mode_config (ADC_ALL_ROUTINE_PARALLEL);
    adc_sync_dma_config(ADC_SYNC_DMA_MODE0);
    adc_sync_dma_request_after_last_enable();

    /* ADC data alignment config */
    adc_data_alignment_config (ADC0, ADC_DATAALIGN_RIGHT);
    adc_data_alignment_config (ADC2, ADC_DATAALIGN_RIGHT);

    /* ADC SCAN function enable */
    adc_special_function_config (ADC0, ADC_SCAN_MODE, ENABLE);
    adc_special_function_config (ADC2, ADC_SCAN_MODE, ENABLE);

    /* ADC channel length config */
    adc_channel_length_config (ADC0, ADC_ROUTINE_CHANNEL, 2);
    adc_channel_length_config (ADC2, ADC_ROUTINE_CHANNEL, 2);

    /* ADC routine channel config */
    adc_routine_channel_config(ADC0, 0, ADC_CHANNEL_8, ADC_SAMPLETIME_15);
    adc_routine_channel_config(ADC0, 1, ADC_CHANNEL_9, ADC_SAMPLETIME_15);
    adc_routine_channel_config(ADC2, 0, ADC_CHANNEL_4, ADC_SAMPLETIME_15);
    adc_routine_channel_config(ADC2, 1, ADC_CHANNEL_5, ADC_SAMPLETIME_15);

    /* ADC external trigger enable */
    adc_external_trigger_config(ADC0,ADC_ROUTINE_CHANNEL,EXTERNAL_TRIGGER_RISING);
    adc_external_trigger_config(ADC2,ADC_ROUTINE_CHANNEL,EXTERNAL_TRIGGER_DISABLE);
    adc_external_trigger_source_config(ADC0,ADC_ROUTINE_CHANNEL,ADC_EXTTRIG_ROUTINE_T1_CH1);

    /* enable ADC interface */
    adc_enable(ADC0);

    /* wait for ADC stability */
    cal_time_system_sleep_ms(1);

    /* ADC calibration and reset calibration */
    adc_calibration_enable(ADC0);

    /* enable ADC interface */
    adc_enable(ADC2);

    /* wait for ADC stability */
    cal_time_system_sleep_ms(1);

    /* ADC calibration and reset calibration */
    adc_calibration_enable(ADC2);

    return 0;
}
采集出来的数据现象:
   file:///C:/Users/z01454/Documents/WXWork/1688857742517246/Cache/Image/2024-09/企业微信截图_17276583127832.png


回答 +关注 1
1038人浏览 0人回答问题 分享 举报
0 个回答

您需要登录后才可以回复 登录 | 注册