本帖最后由 杨文天 于 2023-10-8 12:24 编辑
源代码如下:
#include "gd32e23x.h"
#include <stdio.h>
#include "gd32e230c_eval.h"
#define TIMER16_CH0CV ((uint32_t)0x40014834)
uint16_t TX_buffer[3];
void dma_config(void)
{
dma_parameter_struct dma_init_struct;
rcu_periph_clock_enable(RCU_DMA);
syscfg_dma_remap_enable(SYSCFG_DMA_REMAP_TIMER16);
/* initialize DMA channel1 */
dma_deinit(DMA_CH1);
/* DMA channel4 initialize */
dma_init_struct.periph_addr = (uint32_t)TIMER16_CH0CV;
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_init_struct.memory_addr = (uint32_t)TX_buffer;
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
dma_init_struct.number = 3;
dma_init_struct.priority = DMA_PRIORITY_HIGH;
dma_init( DMA_CH1, &dma_init_struct);
//dma_memory_to_memory_disable(DMA_CH1); // 关闭内存到内存模式
/* enable DMA circulation mode */
dma_circulation_enable(DMA_CH1);
/* enable DMA channel4 */
dma_channel_enable(DMA_CH1);
// nvic_irq_enable(DMA_Channel3_4_IRQn, 1);
}
void timer_config(void)
{
uint16_t period = (72000/TX_Frequency_KHZ)-1;
timer_oc_parameter_struct timer_ocinitpara;
timer_parameter_struct timer_initpara;
/* enable TIMER clock */
rcu_periph_clock_enable(RCU_TIMER16);
timer_deinit(TIMER16);
/* initialize TIMER init parameter struct */
timer_struct_para_init(&timer_initpara);
/* TIMER0 configuration */
timer_initpara.prescaler = 0;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = period;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_initpara.repetitioncounter = 5;
timer_init(TIMER16, &timer_initpara);
/* initialize TIMER channel output parameter struct */
timer_channel_output_struct_para_init(&timer_ocinitpara);
/* CH0 configuration in PWM1 mode */
timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;
//timer_ocinitpara.outputnstate = TIMER_CCXN_DISABLE;
timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
// timer_ocinitpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
timer_ocinitpara.ocidlestate = TIMER_OC_IDLE_STATE_HIGH;
// timer_ocinitpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
timer_channel_output_config(TIMER16, TIMER_CH_0, &timer_ocinitpara);
/* configure TIMER channel output pulse value */
timer_channel_output_pulse_value_config(TIMER16, TIMER_CH_0, period>>1);
/* configure TIMER channel output compare mode */
timer_channel_output_mode_config(TIMER16, TIMER_CH_0, TIMER_OC_MODE_PWM0);
/* disable TIMER channel output shadow function */
timer_channel_output_shadow_config(TIMER16, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
/* TIMER0 primary output enable */
timer_primary_output_config(TIMER16, ENABLE);
/* TIMER0 update DMA request enable */
timer_dma_enable(TIMER16, TIMER_DMA_UPD);
/* auto-reload preload enable */
timer_auto_reload_shadow_enable(TIMER16);
/* TIMER0 counter enable */
timer_enable(TIMER16);
}
int main(void)
{
uint16_t period = 500;
TX_buffer[0] = 250;
TX_buffer[1] = 100;
TX_buffer[2] = 50;
rcu_periph_clock_enable(RCU_GPIOA);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_7);
gpio_af_set(GPIOA, GPIO_AF_5, GPIO_PIN_7);
/* configure the DMA peripheral */
dma_config();
/* configure the TIMER peripheral */
timer_config();
while (1);
}
|