GD32F450使用定时器溢出作为DMA触发源
时间: 2023-07-29 15:14:38 浏览: 136
GD32F450可以使用定时器的溢出事件作为DMA的触发源。具体实现步骤如下:
1. 配置定时器溢出事件触发DMA请求。
2. 配置DMA传输相关参数,包括源地址、目的地址、数据长度等。
3. 启动定时器和DMA传输。
代码示例:
```c
// 配置定时器溢出事件触发DMA请求
timer_oc_parameter_struct timer_ocinitpara;
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER0);
rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4); // 定时器时钟预分频
timer_deinit(TIMER0);
timer_struct_para_init(&timer_initpara);
timer_initpara.prescaler = 3999; // 计数器时钟为 108 MHz / (3999 + 1) = 27 kHz
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 1000; // 定时器溢出时间为 1000 / 27 kHz ≈ 37.04 us
timer_init(TIMER0, &timer_initpara);
timer_oc_struct_para_init(&timer_ocinitpara);
timer_ocinitpara.ocmode = TIMER_OC_MODE_TIMING;
timer_ocinitpara.pulse = 0xFFFF;
timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;
timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
timer_oc_config(TIMER0, TIMER_CH_0, &timer_ocinitpara);
dma_parameter_struct dma_initpara;
dma_struct_para_init(&dma_initpara);
rcu_periph_clock_enable(RCU_DMA0);
dma_deinit(DMA0, DMA_CH0);
dma_initpara.direction = DMA_MEMORY_TO_MEMORY;
dma_initpara.memory_addr = (uint32_t)src_buffer;
dma_initpara.memory_inc = DMA_MEMORY_INCREASE_ENABLE; // 源地址自增
dma_initpara.periph_addr = (uint32_t)dst_buffer;
dma_initpara.periph_inc = DMA_PERIPH_INCREASE_DISABLE; // 目的地址不变
dma_initpara.memory_width = DMA_MEMORY_WIDTH_8BIT;
dma_initpara.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
dma_initpara.number = sizeof(src_buffer) / sizeof(src_buffer[0]);
dma_initpara.priority = DMA_PRIORITY_ULTRA_HIGH;
dma_initpara.fifo_mode = DMA_FIFOMODE_DISABLE;
dma_initpara.periph_addr_cfg = DMA_PERIPH_ADDR_INC;
dma_initpara.circular_mode = DMA_CIRCULAR_MODE_ENABLE;
dma_initpara.memory_addr_cfg = DMA_MEMORY_ADDR_INC;
dma_initpara.direction_cfg = DMA_DIRECTION_PERIPH_TO_MEMORY;
dma_init(DMA0, DMA_CH0, &dma_initpara);
dma_circulation_disable(DMA0, DMA_CH0); // 禁用 DMA 循环传输模式
dma_memory_to_memory_disable(DMA0, DMA_CH0); // 禁用 DMA 存储器到存储器模式
dma_interrupt_enable(DMA0, DMA_CH0, DMA_INT_FTF); // 开启 DMA 传输完成中断
nvic_irq_enable(DMA0_Channel0_IRQn, 0, 0); // 开启 DMA 中断
timer_dma_transfer_config(TIMER0, TIMER_DMACFG_DMATA_CH0, TIMER_DMACFG_DMATC_UEV); // 配置定时器溢出事件触发DMA请求
timer_enable(TIMER0); // 启动定时器
dma_channel_enable(DMA0, DMA_CH0); // 启动DMA传输
```
在以上代码中,我们通过`timer_dma_transfer_config`函数配置了定时器溢出事件触发DMA请求。在定时器溢出时,DMA请求将被触发,进而启动DMA传输。DMA传输完成后,将会触发DMA传输完成中断。
阅读全文