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
} |
|