GD32F405RGT6ADC外部触发DMA(固件库)
李某人话不多说,上代码:哈哈 1.adc.h文件
#ifndef _adc_H
#define _adc_H
#include "gd32f4xx.h"
void adc_init(void);
#endif
2.adc.c文件
#include "gd32f4xx.h"
#include "systick.h"
#include "led.h"
#include "usart.h"
#include "adc.h"
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
uint32_t adc_value[2];
void adc_init(void)
{
dma_single_data_parameter_struct dma_single_data_parameter;
rcu_periph_clock_enable(RCU_GPIOC);
rcu_periph_clock_enable(RCU_DMA1);
rcu_periph_clock_enable(RCU_ADC0);
rcu_periph_clock_enable(RCU_ADC1);
adc_clock_config(ADC_ADCCK_PCLK2_DIV4);
gpio_mode_set(GPIOC,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_3);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
gpio_mode_set(GPIOC,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_5);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
dma_deinit(DMA1,DMA_CH0);
dma_single_data_parameter.periph_addr = (uint32_t)(&ADC_SYNCDATA);
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_32BIT;
dma_single_data_parameter.circular_mode = DMA_CIRCULAR_MODE_ENABLE;
dma_single_data_parameter.direction = DMA_PERIPH_TO_MEMORY;
dma_single_data_parameter.number = 2;
dma_single_data_parameter.priority = DMA_PRIORITY_HIGH;
dma_single_data_mode_init(DMA1,DMA_CH0, &dma_single_data_parameter);
dma_channel_subperipheral_select(DMA1,DMA_CH0,DMA_SUBPERI0);
dma_channel_enable(DMA1,DMA_CH0);
adc_channel_length_config(ADC0,ADC_REGULAR_CHANNEL,2);
adc_channel_length_config(ADC1,ADC_REGULAR_CHANNEL,2);
adc_regular_channel_config(ADC0,0,ADC_CHANNEL_13,ADC_SAMPLETIME_144);
adc_regular_channel_config(ADC0,1,ADC_CHANNEL_15,ADC_SAMPLETIME_144);
adc_regular_channel_config(ADC1,1,ADC_CHANNEL_13,ADC_SAMPLETIME_144);
adc_regular_channel_config(ADC1,0,ADC_CHANNEL_15,ADC_SAMPLETIME_144);
adc_external_trigger_config(ADC0,ADC_REGULAR_CHANNEL,EXTERNAL_TRIGGER_RISING);
adc_external_trigger_config(ADC1,ADC_REGULAR_CHANNEL,EXTERNAL_TRIGGER_DISABLE);
adc_external_trigger_source_config(ADC0,ADC_REGULAR_CHANNEL,ADC_EXTTRIG_REGULAR_T1_CH1);
adc_data_alignment_config(ADC0,ADC_DATAALIGN_RIGHT);
adc_data_alignment_config(ADC1,ADC_DATAALIGN_RIGHT);
adc_sync_mode_config(ADC_DAUL_REGULAL_PARALLEL);
adc_sync_dma_config(ADC_SYNC_DMA_MODE1);
adc_sync_dma_request_after_last_enable();
adc_special_function_config(ADC0,ADC_SCAN_MODE,ENABLE);
adc_special_function_config(ADC1,ADC_SCAN_MODE,ENABLE);
adc_enable(ADC0);
adc_calibration_enable(ADC0);
adc_enable(ADC1);
adc_calibration_enable(ADC1);
}
因为keil乱码的原因,详细的工程注释可以下载文末的链接工程。 因为为外部触发的DMA工作模式,所以相关的定时器配置如下,上代码: 3.timer.h文件
#ifndef _TIMER_H_
#define _TIMER_H_
#include "gd32f4xx.h"
void timer_config(void);
#endif
4.timer.c文件
#include "gd32f4xx.h"
#include "systick.h"
#include "timer.h"
void timer_config(void)
{
timer_oc_parameter_struct timer_ocintpara;
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER1);
rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
timer_initpara.prescaler = 19999;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 9999;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 0;
timer_init(TIMER1,&timer_initpara);
timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara);
timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,3999);
timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM0);
timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
timer_enable(TIMER1);
}
目前的配置,运行起来AD的状态貌似有点不太精确,有待后续再啃一啃用户手册进一步改善。
附上完整工程代码(免费)GD32F405RGT6ADC外部触发DMA(固件库)https://download.csdn.net/download/tiange1996/84405482
|