打印

gd32电机控制配置AD的模式

[复制链接]
878|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
hua_ren|  楼主 | 2020-12-22 11:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
gd32配置为电机采样,2个插入通道,用于采样3相电路,4个规则通道用于采样监控通道。自由度比较高。适合有2个AD的芯片。已经测试,可以使用。


void adc_config(void)   /*配置为2个AD,2个插入通道、4个规则通道*/
{
            /* reset ADC */
    adc_deinit(ADC0);
          adc_deinit(ADC1);
    /* ADC mode config */
    adc_mode_config(ADC_DAUL_REGULAL_PARALLEL_INSERTED_PARALLEL);//ADC_DAUL_INSERTED_PARALLEL );//
    /* ADC continous function enable */
    adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
          adc_special_function_config(ADC1, ADC_SCAN_MODE, ENABLE);
    /* ADC data alignment config */
    adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
    adc_data_alignment_config(ADC1, ADC_DATAALIGN_RIGHT);
    /* ADC channel length config */
    adc_channel_length_config(ADC0, ADC_INSERTED_CHANNEL, 1);
          adc_channel_length_config(ADC1, ADC_INSERTED_CHANNEL, 1);
    /* ADC inserted channel config */
    adc_inserted_channel_config(ADC0, 0, ADC_CHANNEL_2, ADC_SAMPLETIME_55POINT5);
    adc_inserted_channel_config(ADC1, 0, ADC_CHANNEL_0, ADC_SAMPLETIME_55POINT5);
//    adc_inserted_channel_config(ADC0, 2, ADC_CHANNEL_2, ADC_SAMPLETIME_55POINT5);
//    adc_inserted_channel_config(ADC0, 3, ADC_CHANNEL_3, ADC_SAMPLETIME_55POINT5);
    /* ADC trigger config */
    adc_external_trigger_source_config(ADC0,ADC_INSERTED_CHANNEL, ADC0_1_EXTTRIG_INSERTED_T0_CH3);        
    adc_external_trigger_source_config(ADC1,ADC_INSERTED_CHANNEL,ADC0_1_EXTTRIG_INSERTED_NONE ); //        ADC0_1_EXTTRIG_INSERTED_T0_CH3);         //
    /* ADC external trigger enable */
    adc_external_trigger_config(ADC0, ADC_INSERTED_CHANNEL, ENABLE);
                adc_external_trigger_config(ADC1, ADC_INSERTED_CHANNEL, ENABLE);
    /* clear the ADC flag */
    adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOC);
    adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOIC);
                adc_interrupt_flag_clear(ADC1, ADC_INT_FLAG_EOC);
    adc_interrupt_flag_clear(ADC1, ADC_INT_FLAG_EOIC);
    /* enable ADC interrupt */
    adc_interrupt_enable(ADC0, ADC_INT_EOIC);
   
    /* ADC channel length config */
    adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 2);
                adc_channel_length_config(ADC1, ADC_REGULAR_CHANNEL, 2);
    /* ADC regular channel config */
    adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_4, ADC_SAMPLETIME_55POINT5);
    adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_5, ADC_SAMPLETIME_55POINT5);
    adc_regular_channel_config(ADC1, 0, ADC_CHANNEL_6, ADC_SAMPLETIME_55POINT5);
    adc_regular_channel_config(ADC1, 1, ADC_CHANNEL_7, ADC_SAMPLETIME_55POINT5);
    /* ADC trigger config */
    adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_T0_CH0 );
          adc_external_trigger_source_config(ADC1, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_NONE );
                               
    adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
                adc_external_trigger_config(ADC1, ADC_REGULAR_CHANNEL, ENABLE);
    /* ADC DMA function enable */
    adc_dma_mode_enable(ADC0);
   
    /* enable ADC interface */
    adc_enable(ADC0);
                  adc_enable(ADC1);
//    delay_1ms(1);
    /* ADC calibration and reset calibration */
    adc_calibration_enable(ADC0);
            adc_calibration_enable(ADC1);
          nvic_irq_enable(ADC0_1_IRQn, 3, 0);
       
       
//     /* reset ADC */
//    adc_deinit(ADC0);
//    adc_deinit(ADC1);
//    /* ADC mode config */
//    adc_mode_config(ADC_DAUL_REGULAL_PARALLEL);
//    /* ADC continous function enable */
//    adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
//    adc_special_function_config(ADC1, ADC_SCAN_MODE, ENABLE);   
//
//    /* ADC data alignment config */
//    adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
//    adc_data_alignment_config(ADC1, ADC_DATAALIGN_RIGHT);  

//             /* ADC channel length config */
//    adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 4);
//    adc_channel_length_config(ADC1, ADC_REGULAR_CHANNEL, 4);
//  
//    /* ADC regular channel config   6channel input */
//          
//   adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_0, ADC_SAMPLETIME_55POINT5 );//U  In
//   adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_2, ADC_SAMPLETIME_55POINT5 );
//   adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_4, ADC_SAMPLETIME_13POINT5 );
//         adc_regular_channel_config(ADC0, 3, ADC_CHANNEL_8, ADC_SAMPLETIME_71POINT5 );
//   
//   adc_regular_channel_config(ADC1, 0, ADC_CHANNEL_1, ADC_SAMPLETIME_55POINT5 );
//   adc_regular_channel_config(ADC1, 1, ADC_CHANNEL_3, ADC_SAMPLETIME_55POINT5 );
//   adc_regular_channel_config(ADC1, 2, ADC_CHANNEL_0, ADC_SAMPLETIME_13POINT5 );
//         adc_regular_channel_config(ADC1, 3, ADC_CHANNEL_9, ADC_SAMPLETIME_71POINT5 );


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

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

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

  
}

void dma_config(void)
{
    /* ADC_DMA_channel configuration */
    dma_parameter_struct dma_data_parameter;
   
    /* 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_32BIT;
    dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_32BIT;  
    dma_data_parameter.direction    = DMA_PERIPHERAL_TO_MEMORY;
    dma_data_parameter.number       = 4;//192;//3;//1536;//768;/*768*2=1536*/
    dma_data_parameter.priority     = DMA_PRIORITY_HIGH;
    dma_init(DMA0, DMA_CH0, &dma_data_parameter);

    dma_circulation_enable(DMA0, DMA_CH0);//dma_circulation_disable(DMA0, DMA_CH0);//
  

                dma_interrupt_enable(DMA0,  DMA_CH0, DMA_INT_FTF );
    /* enable DMA channel */
    dma_channel_enable(DMA0, DMA_CH0);
               
                 /**/

     nvic_irq_enable(DMA0_Channel0_IRQn,1,0);//DMA0_Channel0_IRQHandler

}

使用特权

评论回复
沙发
hzmagnet| | 2021-4-16 16:46 | 只看该作者
同样的代码我用T0_TRGO触发就不行,这是为什么

使用特权

评论回复
板凳
lidi911| | 2021-4-18 15:23 | 只看该作者
学习了,谢谢楼主分享。

使用特权

评论回复
地板
yangxiaor520| | 2021-4-19 07:20 | 只看该作者
感谢楼主的经验分享

使用特权

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

本版积分规则

2

主题

84

帖子

1

粉丝