问答

汇集网友智慧,解决技术难题

21ic问答首页 - GD32E230的ADC可靠不?

GD32E230的ADC可靠不?

h882021-01-10
我想用 TIMER14 CH0 触发 ADC(扫描转换模式),就怎么触发不了。高手帮我看看哪错了
#include "gd32e23x_adc.h"
#include "gd32e23x_dma.h"
#include "gd32e23x_gpio.h"
#include "gd32e23x_timer.h"

static volatile uint16_t data[4];

static void rcuConfig();
static void adcConfig();
static void dmaConfig();
static void portConfig();
static void timerConfig();

void main() {
  rcuConfig();
  timerConfig();
  portConfig();
  dmaConfig();
  adcConfig();
  timer_enable(TIMER14);

  gpio_bit_set(GPIOB, GPIO_PIN_4);
  while (1)
    ;
}

void rcuConfig() {
  rcu_periph_clock_enable(RCU_GPIOA);
  rcu_periph_clock_enable(RCU_GPIOB);
  rcu_periph_clock_enable(RCU_ADC);
  rcu_periph_clock_enable(RCU_DMA);
  rcu_periph_clock_enable(RCU_TIMER14);
  rcu_adc_clock_config(RCU_ADCCK_AHB_DIV3);
}

void adcConfig() {
  adc_special_function_config(ADC_SCAN_MODE, ENABLE);
  adc_dma_mode_enable();

  adc_channel_length_config(ADC_REGULAR_CHANNEL, 4U);
  adc_regular_channel_config(1U, ADC_CHANNEL_3, ADC_SAMPLETIME_1POINT5);
  adc_regular_channel_config(2U, ADC_CHANNEL_4, ADC_SAMPLETIME_1POINT5);
  adc_regular_channel_config(3U, ADC_CHANNEL_8, ADC_SAMPLETIME_1POINT5);
  adc_regular_channel_config(4U, ADC_CHANNEL_9, ADC_SAMPLETIME_1POINT5);

  adc_external_trigger_config(ADC_REGULAR_CHANNEL, ENABLE);
  adc_external_trigger_source_config(ADC_REGULAR_CHANNEL,
                                     ADC_EXTTRIG_REGULAR_T14_CH0);

  adc_enable();
  for (uint16_t i = 0; i < 1000; ++i)
    ;
  adc_calibration_enable();
}

void dmaConfig() {
  dma_parameter_struct initParam;
  initParam.periph_addr = 0x4001244CU;  // ADC_RDATA
  initParam.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  initParam.memory_addr = (uint32_t)data;
  initParam.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  initParam.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
  initParam.memory_width = DMA_MEMORY_WIDTH_16BIT;
  initParam.direction = DMA_PERIPHERAL_TO_MEMORY;
  initParam.number = 4U;
  initParam.priority = DMA_PRIORITY_HIGH;
  dma_init(DMA_CH0, &initParam);

  dma_circulation_enable(DMA_CH0);

  dma_interrupt_enable(DMA_CH0, DMA_INT_FTF);
  dma_interrupt_flag_clear(DMA_CH0, DMA_INT_FLAG_FTF);
  nvic_irq_enable(DMA_Channel0_IRQn, 1);

  dma_channel_enable(DMA_CH0);
}

void portConfig() {
  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_4);
  gpio_mode_set(GPIOB, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_0);
  gpio_mode_set(GPIOB, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_1);
  gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_4);  // LED
}

void timerConfig() {
  timer_parameter_struct initParam;
  initParam.prescaler = 71;
  initParam.alignedmode = TIMER_COUNTER_EDGE;
  initParam.counterdirection = TIMER_COUNTER_UP;
  initParam.period = 99;
  initParam.clockdivision = TIMER_CKDIV_DIV1;
  initParam.repetitioncounter = 0U;
  timer_init(TIMER14, &initParam);

  timer_oc_parameter_struct ocParam;
  ocParam.outputstate = TIMER_CCX_ENABLE;
  ocParam.ocpolarity = TIMER_OC_POLARITY_HIGH;
  timer_channel_output_config(TIMER14, TIMER_CH_0, &ocParam);

  timer_channel_output_pulse_value_config(TIMER14, TIMER_CH_0, 2U);
  timer_channel_output_mode_config(TIMER14, TIMER_CH_0, TIMER_OC_MODE_PWM1);
}

void DMA_Channel0_IRQHandler() {
  static uint32_t i = 0;
  if (dma_interrupt_flag_get(DMA_CH0, DMA_INT_FLAG_FTF)) {
    dma_interrupt_flag_clear(DMA_CH0, DMA_INT_FLAG_FTF);
    ++i;
    if (i == 10000) {
      i = 0;
      gpio_bit_toggle(GPIOB, GPIO_PIN_4);
    }
  }
}


回答 +关注 1
792人浏览 0人回答问题 分享 举报
0 个回答

您需要登录后才可以回复 登录 | 注册