GD32F405RGT6定时器固件库(所有定时器的配置(12个))

GD32F405RGT6所有定时器的配置

GD32F4XXX系列拥有12个定时器,定时器的类型如下表:

在这里插入图片描述

一般我们可以根据定时器的作用以及类型选取合适的定时器,在这次对GD的单片机而言我就将它所拥有的12个定时器撸了一遍。通用定时器以及高级定时器的配置我都配置成为了PWM输出模式,对于其他功能的配置后续会持续更新。上代码:
1.定时器0(高级定时器):

void Timer0_Init(unsigned short arr, unsigned short psc)
{
	timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;

	rcu_periph_clock_enable(RCU_GPIOA);

    gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_8);

    gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_8);
	
    rcu_periph_clock_enable(RCU_TIMER0);
	
    rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);

    timer_deinit(TIMER0);
	
	timer_initpara.prescaler         = psc;  	
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;	
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;	
    timer_initpara.period            = arr; 	
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
	timer_initpara.repetitioncounter = 0; 
    timer_init(TIMER0,&timer_initpara);

    timer_ocintpara.ocpolarity  = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;

    timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_ocintpara);

    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,arr/2);
	
    timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0);
	
    timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);

    timer_auto_reload_shadow_enable(TIMER0);
	
    timer_enable(TIMER0);
	
	timer_interrupt_flag_clear(TIMER0,TIMER_INT_UP);
	
	timer_interrupt_enable(TIMER0, TIMER_INT_UP);
	
	nvic_irq_enable(TIMER0_UP_TIMER9_IRQn, 0, 2); 
	
}

//中断服务函数 
void TIMER0_UP_TIMER9_IRQHandler(void)
{
	if(timer_interrupt_flag_get(TIMER0,TIMER_INT_FLAG_UP) != RESET)
	{
		 timer_interrupt_flag_clear(TIMER0,TIMER_INT_FLAG_UP);	
	}
	else if(timer_interrupt_flag_get(TIMER9,TIMER_INT_FLAG_UP) != RESET)
	{
		 timer_interrupt_flag_clear(TIMER9,TIMER_INT_FLAG_UP);
	}
}


2.定时器2:通用定时器

void Timer2_Init(unsigned short arr, unsigned short psc)
{
	timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;

	rcu_periph_clock_enable(RCU_GPIOC);
    gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_9);
    gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_9);

    gpio_af_set(GPIOC, GPIO_AF_1, GPIO_PIN_9);
	
    rcu_periph_clock_enable(RCU_TIMER2);
	
    rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);

    timer_deinit(TIMER2);
	
	timer_initpara.prescaler         = psc;  	
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;	
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;	
    timer_initpara.period            = arr; 	
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
	timer_initpara.repetitioncounter = 0; 
    timer_init(TIMER2,&timer_initpara);

    timer_ocintpara.ocpolarity  = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
	timer_channel_output_config(TIMER2,TIMER_CH_3,&timer_ocintpara);
	timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_3,arr/2);
	timer_channel_output_mode_config(TIMER2,TIMER_CH_3,TIMER_OC_MODE_PWM0);timer_channel_output_shadow_config(TIMER2,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
    timer_auto_reload_shadow_enable(TIMER2);
    timer_enable(TIMER2);
	timer_interrupt_flag_clear(TIMER2,TIMER_INT_UP);
	timer_interrupt_enable(TIMER2, TIMER_INT_UP);
	nvic_irq_enable(TIMER2_IRQn, 0, 4); 
}

//中断服务函数
void TIMER1_IRQHandler(void)
{
	if(timer_interrupt_flag_get(TIMER1,TIMER_INT_FLAG_UP) != RESET)
	{
		 timer_interrupt_flag_clear(TIMER1,TIMER_INT_FLAG_UP);
	}
}


3.基本定时器:定时器5

void Timer5_Init(unsigned short arr, unsigned short psc)
{
	timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;
	rcu_periph_clock_enable(RCU_TIMER5);
    rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
    timer_deinit(TIMER5);
	timer_initpara.prescaler         = psc;  	
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;	
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;	
    timer_initpara.period            = arr; 	
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
	timer_initpara.repetitioncounter = 0; 
    timer_init(TIMER5,&timer_initpara);
	timer_auto_reload_shadow_enable(TIMER5);
    timer_enable(TIMER5);
	timer_interrupt_flag_clear(TIMER5,TIMER_INT_UP);
	timer_interrupt_enable(TIMER5, TIMER_INT_UP);
	nvic_irq_enable(TIMER5_DAC_IRQn, 0, 7); 
}

定时器2、3、4、8、11、9、10、12皆为通用定时器,配置与定时器1大体相似。定时器6也是基本定时器,其中配置与定时器5大体相同。有不懂的道友,可以参考文末附上的工程链接。

附上完整工程链接:GD32F405RGT6定时器固件库(所有定时器的配置(12个))

  • 1
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

修才生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值