/** * @file adc.c * @author Ethan.Tao (tzj0429@163.com) * @brief * @version 0.1 * @date 2020-12-13 * * @copyright Copyright (c) * */ #include "adc.h" #include "gpio.h" #include "gd32e23x_adc.h" static __IO uint16_t ad_value[2]; static void ADC_GPIOCfg(void) { /* enable DMA clock */ rcu_periph_clock_enable(RCU_DMA); /* enable ADC clock */ rcu_periph_clock_enable(RCU_ADC); gpio_mode_set(CURRENT_AI_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, CURRENT_AI_PIN); gpio_mode_set(VOLTAGE_AI_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, VOLTAGE_AI_PIN); } static void ADC_DMAconfig(void) { dma_parameter_struct dma_init_struct; /* initialize DMA channel0 */ dma_deinit(DMA_CH0); dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY; dma_init_struct.memory_addr = (uint32_t)ad_value; dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE; dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT; dma_init_struct.number = 2; dma_init_struct.periph_addr = (uint32_t) & (ADC_RDATA); dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE; dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT; dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH; dma_init(DMA_CH0, &dma_init_struct); /* configure DMA mode */ dma_circulation_enable(DMA_CH0); dma_memory_to_memory_disable(DMA_CH0); /* enable DMA channel0 */ dma_channel_enable(DMA_CH0); } extern void ADC_Init(void) { ADC_GPIOCfg(); ADC_DMAconfig(); /* ADC channel length config */ adc_channel_length_config(ADC_REGULAR_CHANNEL, 2); //¹æÔò×é ¹²2¸öͨµÀ /* ADC regular channel config */ adc_regular_channel_config(0, ADC_CHANNEL_1, ADC_SAMPLETIME_7POINT5); adc_regular_channel_config(1, ADC_CHANNEL_2, ADC_SAMPLETIME_7POINT5); /* ADC external trigger enable */ adc_external_trigger_config(ADC_REGULAR_CHANNEL, ENABLE); /* ADC external trigger source config */ adc_external_trigger_source_config(ADC_REGULAR_CHANNEL, ADC_EXTTRIG_REGULAR_T0_CH2); /* ADC data alignment config */ adc_data_alignment_config(ADC_DATAALIGN_RIGHT); /* enable ADC interface */ adc_enable(); /* ADC calibration and reset calibration */ adc_calibration_enable(); /* ADC DMA function enable */ adc_dma_mode_enable(); } extern uint16_t ADC_GetChannleValue(uint8_t ch) { while (!dma_flag_get(DMA_CH0, DMA_FLAG_FTF)) ; /* clear channel0 transfer complete flag */ dma_flag_clear(DMA_CH0, DMA_FLAG_FTF); return ad_value[ch]; }