本帖最后由 lqwuliang 于 2022-5-28 17:18 编辑
#include "gd32f4xx.h"
#include "adc.h"
#include
__IO uint32_t g_dmacomplete_flag;
uint32_t FAN_BAD1;
uint32_t FAN_BAD2;
uint32_t BUS700V;
uint32_t ADC_MQ;
uint32_t ADC_Water1;
uint32_t ADC_Water2;
uint16_t adc_value[8];
/***************************************************************************
* @retval 无
**************************************************************************/
void ADC_config(void)
{
dma_single_data_parameter_struct dma_single_data_parameter;
//ADC相关时钟配置
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_DMA1);
rcu_periph_clock_enable(RCU_ADC0);
adc_clock_config(ADC_ADCCK_PCLK2_DIV6);
//PA1 PA2 PA3 PA5 PA6 PA7
gpio_mode_set(GPIOA,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_1 //配置引脚为模拟输入状态
|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
/* DMA相关配置 DMA1 CH0*/
dma_deinit(DMA1, DMA_CH0);
dma_single_data_parameter.periph_addr = (uint32_t)(&ADC_RDATA(ADC0));
dma_single_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_single_data_parameter.memory0_addr = (uint32_t)(&adc_value);
dma_single_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE; // 存储器地址是否为增量模式
dma_single_data_parameter.periph_memory_width = DMA_PERIPH_WIDTH_16BIT;
dma_single_data_parameter.circular_mode = DMA_CIRCULAR_MODE_ENABLE; // 循环模式
dma_single_data_parameter.direction = DMA_PERIPH_TO_MEMORY; // 外设到存储器
dma_single_data_parameter.number = 6; // DMA缓存大小
dma_single_data_parameter.priority = DMA_PRIORITY_HIGH;
dma_single_data_mode_init(DMA1,DMA_CH0,&dma_single_data_parameter);
dma_channel_enable(DMA1, DMA_CH0);
dma_circulation_enable(DMA1, DMA_CH0);
//ADC相关配置
adc_deinit(); // 复位ADC
adc_sync_mode_config(ADC_SYNC_MODE_INDEPENDENT); //独立模式
adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE); // 扫描模式
adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE); // 连续模式
adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT); // 右对齐
adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 6); //读取6通信
/* ADC regular channel config */
adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_1, ADC_SAMPLETIME_56);
adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_2, ADC_SAMPLETIME_56);
adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_3, ADC_SAMPLETIME_56);
adc_regular_channel_config(ADC0, 3, ADC_CHANNEL_5, ADC_SAMPLETIME_56);
adc_regular_channel_config(ADC0, 4, ADC_CHANNEL_6, ADC_SAMPLETIME_56);
adc_regular_channel_config(ADC0, 5, ADC_CHANNEL_7, ADC_SAMPLETIME_56);
adc_external_trigger_config(ADC0,ADC_REGULAR_CHANNEL,EXTERNAL_TRIGGER_DISABLE);
adc_dma_request_after_last_enable(ADC0);
adc_dma_mode_enable(ADC0);
adc_enable(ADC0);
adc_calibration_enable(ADC0);
adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
}