GD32F450设置定时器输入捕获下降沿触发DMA
时间: 2023-07-07 18:20:25 浏览: 274
首先,需要初始化定时器和DMA。具体代码如下:
```c
/* 使能定时器时钟 */
rcu_periph_clock_enable(RCU_TIMER0);
/* 使能DMA时钟 */
rcu_periph_clock_enable(RCU_DMA0);
/* 配置定时器 */
timer_deinit(TIMER0);
timer_parameter_struct timer_initpara;
timer_struct_para_init(&timer_initpara);
timer_initpara.prescaler = 71;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 0xFFFF;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_init(TIMER0, &timer_initpara);
/* 配置DMA */
dma_deinit(DMA0, DMA_CH0);
dma_parameter_struct dma_initpara;
dma_struct_para_init(&dma_initpara);
dma_initpara.direction = DMA_PERIPHERAL_TO_MEMORY;
dma_initpara.memory_addr = (uint32_t)&capture_value;
dma_initpara.memory_inc = DMA_MEMORY_INC_ENABLE;
dma_initpara.memory_width = DMA_MEMORY_WIDTH_32BIT;
dma_initpara.number = 1;
dma_initpara.periph_addr = (uint32_t)&TIMER_CAR(TIMER0);
dma_initpara.periph_inc = DMA_PERIPH_INC_DISABLE;
dma_initpara.periph_width = DMA_PERIPHERAL_WIDTH_32BIT;
dma_initpara.priority = DMA_PRIORITY_ULTRA_HIGH;
dma_init(DMA0, DMA_CH0, &dma_initpara);
/* 配置定时器输入捕获 */
timer_input_capture_parameter_struct timer_icinitpara;
timer_ic_struct_para_init(&timer_icinitpara);
timer_icinitpara.icpolarity = TIMER_IC_POLARITY_FALLING;
timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
timer_icinitpara.icfilter = 0x0;
timer_input_capture_init(TIMER0, TIMER_CH_0, &timer_icinitpara);
```
接着,需要开启DMA传输和定时器。具体代码如下:
```c
/* 使能DMA通道 */
dma_channel_enable(DMA0, DMA_CH0);
/* 使能定时器 */
timer_enable(TIMER0);
```
最后,在定时器中断处理函数中,读取捕获值并重新配置DMA传输。具体代码如下:
```c
void TIMER0_IRQHandler(void)
{
if (timer_interrupt_flag_get(TIMER0, TIMER_INT_CH0) != RESET)
{
/* 清除定时器中断标志 */
timer_interrupt_flag_clear(TIMER0, TIMER_INT_CH0);
/* 获取捕获值 */
uint32_t capture_value = timer_channel_capture_value_register_get(TIMER0, TIMER_CH_0);
/* 配置DMA传输 */
dma_memory0_address_config(DMA0, DMA_CH0, (uint32_t)&capture_value);
dma_channel_enable(DMA0, DMA_CH0);
}
}
```
这样,当定时器0的输入捕获通道0检测到下降沿时,就会触发DMA传输了。需要注意的是,上述代码仅供参考,具体实现还需要根据实际需求进行调整。
阅读全文