打印
[研电赛技术支持]

GD32F103使用DMA方式实现ADC0数据循环采集

[复制链接]
1065|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
慢醇|  楼主 | 2022-1-25 22:15 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
[color=rgba(0, 0, 0, 0.75)]选择合适的DMA通道
[color=rgba(0, 0, 0, 0.75)]ADC0使用ch0

使用特权

评论回复
沙发
慢醇|  楼主 | 2022-1-25 22:16 | 只看该作者
/*!
    \brief      configure the DMA peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
void dma_config(void)
{
    /* ADC_DMA_channel configuration */
    dma_parameter_struct dma_data_parameter;

    /* enable DMA0 clock */
    rcu_periph_clock_enable(RCU_DMA0);  
  
    /* ADC DMA_channel configuration */
    dma_deinit(DMA0, DMA_CH0);
   
    /* initialize DMA data mode */
    dma_data_parameter.periph_addr  = (uint32_t)(&ADC_RDATA(ADC0));
    dma_data_parameter.periph_inc   = DMA_PERIPH_INCREASE_DISABLE;
    dma_data_parameter.memory_addr  = (uint32_t)(&adc_value);
    dma_data_parameter.memory_inc   = DMA_MEMORY_INCREASE_ENABLE;
    dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
    dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_16BIT;  
    dma_data_parameter.direction    = DMA_PERIPHERAL_TO_MEMORY;
    dma_data_parameter.number       = 100;               
    dma_data_parameter.priority     = DMA_PRIORITY_ULTRA_HIGH;
    dma_init(DMA0, DMA_CH0, &dma_data_parameter);

    dma_circulation_enable(DMA0, DMA_CH0);        //采集完100个之后,循环
   
    /* enable DMA transfer complete interrupt */
    dma_interrupt_enable(DMA0, DMA_CH0, DMA_INT_FTF);        //打开全部完成中断
    dma_interrupt_enable(DMA0, DMA_CH0, DMA_INT_HTF);        //打开一半完成中断
  
    /* enable DMA channel */
    dma_channel_enable(DMA0, DMA_CH0);
   
    nvic_irq_enable(DMA0_Channel0_IRQn, 0, 0);
}

/*!
    \brief      configure the TIMER peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
void timer1_config(void)        //1ms采集一次
{
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;

    /* enable timer1 clock */
    rcu_periph_clock_enable(RCU_TIMER1);
  
    /* TIMER1 configuration */
    timer_initpara.prescaler         = 107;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 999;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER1,&timer_initpara);

    /* CH0 configuration in PWM mode1 */
    timer_ocintpara.ocpolarity  = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
    timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocintpara);

    timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, 499);
    timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM1);
    timer_channel_output_shadow_config(TIMER1, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
}

/*!
    \brief      configure the ADC peripheral
    \param[in]  none
    \param[out] none
    \retval     none
*/
void adc_config(void)
{
    /* enable ADC0 clock */
    rcu_periph_clock_enable(RCU_ADC0);
   /* reset ADC */
    adc_deinit(ADC0);
    /* ADC mode config */
    adc_mode_config(ADC_MODE_FREE);
    /* ADC continous function enable */
    adc_special_function_config(ADC0, ADC_SCAN_MODE, DISABLE);

    /* ADC data alignment config */
    adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
    /* ADC channel length config */
    adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 1);
  
  
    /* ADC regular channel config */
    adc_regular_channel_config(ADC0, 0, AD0_CH, ADC_SAMPLETIME_55POINT5);

    /* ADC trigger config */
    adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_T1_CH1);
        /* ADC external trigger enable */
    adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);

    /* enable ADC interface */
    adc_enable(ADC0);  
    delay_1ms(1);
    /* ADC calibration and reset calibration */
    adc_calibration_enable(ADC0);  

    /* ADC DMA function enable */
    adc_dma_mode_enable(ADC0);
}

/*!
    \brief      this function handles DMA_Channel3_IRQHandler interrupt
    \param[in]  none
    \param[out] none
    \retval     none
*/
void DMA0_Channel0_IRQHandler(void)
{
  if(dma_interrupt_flag_get(DMA0, DMA_CH0, DMA_INT_FLAG_FTF))
  {     
    dma_interrupt_flag_clear(DMA0, DMA_CH0, DMA_INT_FLAG_FTF);
//    dma_interrupt_flag_clear(DMA0, DMA_CH0, DMA_INT_FLAG_G);
   
  }
  else if(dma_interrupt_flag_get(DMA0, DMA_CH0, DMA_INT_FLAG_HTF))
  {
    dma_interrupt_flag_clear(DMA0, DMA_CH0, DMA_INT_FLAG_HTF);
//    dma_interrupt_flag_clear(DMA0, DMA_CH0, DMA_INT_FLAG_G);
    dmaHalfIntCnt ++;
//    uart_dma_start();

  }
}

void dma_start(void)
{
  timer1_config();
  dma_config();
  adc_config();
  /* enable TIMER1 */
  timer_enable(TIMER1);

}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

126

主题

1255

帖子

5

粉丝