GD32E230+ADC+DMA 多通道采集

ADC+DMA 单通道采集看这里

ADC.h


#ifndef __ADC_H
#define __ADC_H

#include "gd32e23x.h"

void       UserADC_init(void) ;
uint16_t   GetADCVal(uint8_t adcindex);
 
#endif //__ADC_H

ADC.c

#include "adc.h"

__IO  uint16_t ADCConvertedValue[5];


void ADC_IO_Init(void)  
{
  	rcu_periph_clock_enable(RCU_GPIOA);
    /* ADCCLK = PCLK2/6 */
    rcu_adc_clock_config(RCU_ADCCK_APB2_DIV6);// ADC时钟为 72/6 = 12MHz
	
     gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_0);
     gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_1);
     gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_4);
     gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_5);
     gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_6);
}

 
void   ADC_DMAConfig(void)  
{
  /* ADC_DMA_channel configuration */
    dma_parameter_struct dma_data_parameter;

	  rcu_periph_clock_enable(RCU_DMA);
	    /* enable ADC clock */
    rcu_periph_clock_enable(RCU_ADC);
	
    /* ADC DMA_channel configuration */
    dma_deinit(DMA_CH0);
    
    /* initialize DMA single data mode */
    dma_data_parameter.periph_addr  = (uint32_t)(&ADC_RDATA);
    dma_data_parameter.periph_inc   = DMA_PERIPH_INCREASE_DISABLE;
    dma_data_parameter.memory_addr  = (uint32_t)(&ADCConvertedValue);
    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       = 5U;
    dma_data_parameter.priority     = DMA_PRIORITY_HIGH;
    dma_init(DMA_CH0, &dma_data_parameter);

    dma_circulation_enable(DMA_CH0);
    dma_memory_to_memory_disable(DMA_CH0);
    /* enable DMA channel */
    dma_channel_enable(DMA_CH0);
}


void   ADC_Config(void)
{ 
  /* ADC contineous function enable */
    adc_special_function_config(ADC_SCAN_MODE, ENABLE);
	  adc_special_function_config(ADC_CONTINUOUS_MODE, ENABLE);
    /* ADC trigger config */
    adc_external_trigger_source_config(ADC_REGULAR_CHANNEL, ADC_EXTTRIG_REGULAR_NONE); 
    /* ADC data alignment config */
    adc_data_alignment_config(ADC_DATAALIGN_RIGHT);
		/* ADC分辨率 12B */
	  adc_resolution_config(ADC_RESOLUTION_12B);
    /* ADC channel length config */
    adc_channel_length_config(ADC_REGULAR_CHANNEL, 5U);
 
    /* ADC regular channel config */

     adc_regular_channel_config(0U, ADC_CHANNEL_0, ADC_SAMPLETIME_55POINT5);
		 adc_regular_channel_config(1U, ADC_CHANNEL_1, ADC_SAMPLETIME_55POINT5);
		 adc_regular_channel_config(2U, ADC_CHANNEL_4, ADC_SAMPLETIME_55POINT5);
		 adc_regular_channel_config(3U, ADC_CHANNEL_5, ADC_SAMPLETIME_55POINT5);
		 adc_regular_channel_config(4U, ADC_CHANNEL_6, ADC_SAMPLETIME_55POINT5);
	
    adc_external_trigger_config(ADC_REGULAR_CHANNEL, DISABLE);
    /* enable ADC interface */
    adc_enable();
    delay_1ms(1U);
    /* ADC calibration and reset calibration */
    adc_calibration_enable();

    /* ADC DMA function enable */
    adc_dma_mode_enable();
    /* ADC software trigger enable */
	 adc_software_trigger_enable(ADC_REGULAR_CHANNEL);
}

void   UserADC_init(void)
{
	ADC_IO_Init();
	ADC_DMAConfig();
	ADC_Config();
}


uint16_t   GetADCVal(uint8_t adcindex) //任意地方调用 获取ADC值
{
	return  ADCConvertedValue[adcindex];
}

调用UserADC_init 进行初始化,
在定时器中断里面按需间隔一定时间使能采样,

			if(++trigger_adc_cnt>=50)//50ms
			{
			  trigger_adc_cnt = 0;
			  adc_software_trigger_enable(ADC_REGULAR_CHANNEL);
			}

然后在主循环任意地方调用GetADCVal(uint8_t adcindex);获取ADC值就可以了。
ADC滤波算法另加。

你可能感兴趣的:(GD32MCU,单片机,stm32,c语言)