void ADC_Init(void)
{
rcu_periph_clock_enable(RCU_ADC);
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOC);
rcu_adc_clock_config(RCU_ADCCK_APB2_DIV8); // 10MHz
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_2);
gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_3);
gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_5);
gpio_mode_set(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_0);
gpio_mode_set(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_1);
gpio_mode_set(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_2);
gpio_mode_set(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_3);
// 规则排序可以改变每个通道采回来的值,对应DMA数组的下标
adc_channel_length_config(ADC_REGULAR_CHANNEL, 9); // 规则组
adc_regular_channel_config(0, ADC_CHANNEL_10, ADC_SAMPLETIME_239POINT5); // 通道10: PC0
adc_regular_channel_config(1, ADC_CHANNEL_11, ADC_SAMPLETIME_239POINT5); // 通道11: PC1
adc_regular_channel_config(2, ADC_CHANNEL_12, ADC_SAMPLETIME_239POINT5); // 通道12: PC2
adc_regular_channel_config(3, ADC_CHANNEL_13, ADC_SAMPLETIME_239POINT5); // 通道13: PC3 3.3v
adc_regular_channel_config(4, ADC_CHANNEL_0, ADC_SAMPLETIME_239POINT5); // 通道00: PA0
adc_regular_channel_config(5, ADC_CHANNEL_1, ADC_SAMPLETIME_239POINT5); // 通道01: PA1
adc_regular_channel_config(6, ADC_CHANNEL_2, ADC_SAMPLETIME_239POINT5); // 通道02: PA2
adc_regular_channel_config(7, ADC_CHANNEL_3, ADC_SAMPLETIME_239POINT5); // 通道04: PA3
adc_regular_channel_config(8, ADC_CHANNEL_5, ADC_SAMPLETIME_239POINT5); // 通道05: PA5 3.3V
//12位ADC的转换时间为(239.5+12.5)/10 = 25.2us added by eagle
adc_external_trigger_config(ADC_REGULAR_CHANNEL, ENABLE); // 规则组触发使能
adc_external_trigger_source_config(ADC_REGULAR_CHANNEL, ADC_EXTTRIG_REGULAR_NONE);
adc_data_alignment_config(ADC_DATAALIGN_RIGHT); // 数据右对齐
adc_enable();
//delay_1ms(1);
adc_calibration_enable();
adc_special_function_config(ADC_SCAN_MODE, ENABLE); // 扫描模式使能
adc_special_function_config(ADC_CONTINUOUS_MODE, ENABLE); // 连续转换模式使能
adc_dma_mode_enable(); // DMA请求
ADC_DMAInit();
dma_channel_enable(DMA_CH0);
adc_interrupt_flag_clear(ADC_INT_FLAG_EOC);
nvic_priority_group_set(NVIC_PRIGROUP);
nvic_irq_enable(ADC_CMP_IRQn, PRI_ADC);
adc_interrupt_enable(ADC_INT_EOC); // added by eagle
adc_software_trigger_enable(ADC_REGULAR_CHANNEL);
}
void ADC_DMAInit(void)
{
dma_parameter_struct dma_init_struct;
rcu_periph_clock_enable(RCU_DMA);
dma_deinit(DMA_CH0);
dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY; // 外设到内存
dma_init_struct.memory_addr = (uint32)Adc_HwValue; // 内存地址
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE; // 内存地址增量
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT; // 内存地址长度
dma_init_struct.number = FILTER_SAMPLES*ADC_CHN_SIZE; // 传输数据总长度
dma_init_struct.periph_addr = (uint32)&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_MEDIUM; // 优先级
dma_init(DMA_CH0, &dma_init_struct);
dma_circulation_enable(DMA_CH0);
dma_memory_to_memory_disable(DMA_CH0);
}