GD32fxx+TIM定时器(4)

GD32fxx+TIM定时器(4)

上文记录了常用的pwm波的输出。但在某些情况下,我们需要使用一个定时器输出不同的频率。

定时器之同一定时器pwm输出不同频率

环境:GD32f107vct6+TIMER2

PWM1 PC9 Remap: TIMER2_CH3

PWM2 PC8 Remap: TIMER2_CH2

io口初始化

上文有解释,不用多说。

  rcu_periph_clock_enable(RCU_GPIOC);
  rcu_periph_clock_enable(RCU_AF);
  gpio_pin_remap_config(GPIO_TIMER2_FULL_REMAP,ENABLE);
  gpio_init(GPIOC,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_9 | GPIO_PIN_8);

定时器初始化

  timer_parameter_struct initpara;
  timer_oc_parameter_struct ocpara;
  rcu_periph_clock_enable(RCU_TIMER2);
  
  timer_deinit(TIMER2);
  initpara.alignedmode = TIMER_COUNTER_EDGE;
  initpara.clockdivision = TIMER_CKDIV_DIV1;
  initpara.counterdirection = TIMER_COUNTER_UP;
  initpara.period = 65535;
  initpara.prescaler = 107;//手册有写 +1
  initpara.repetitioncounter = 0;//重复计数器(高级定时器有)
  timer_init(TIMER2,&initpara);
//pwm1  
  ocpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  ocpara.ocnidlestate = TIMER_OCN_IDLE_STATE_HIGH;
  ocpara.ocnpolarity = TIMER_OCN_POLARITY_LOW;
  ocpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  ocpara.outputnstate = TIMER_CCXN_DISABLE;
  ocpara.outputstate = TIMER_CCX_ENABLE;
  
  timer_channel_output_config(TIMER2,TIMER_CH_3,&ocpara);
  timer_channel_output_mode_config(TIMER2,TIMER_CH_3,TIMER_OC_MODE_TOGGLE);//输出比较反转模式
  timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_3,pulseout1);

  //2
  timer_channel_output_config(TIMER2,TIMER_CH_2,&ocpara);
  timer_channel_output_mode_config(TIMER2,TIMER_CH_2,TIMER_OC_MODE_TOGGLE);
  timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_2,pulseout2);
  
  nvic_irq_enable(TIMER2_IRQn,1,1);
  timer_interrupt_enable(TIMER2,TIMER_INT_CH3);
  timer_interrupt_enable(TIMER2,TIMER_INT_CH2);
  timer_auto_reload_shadow_enable(TIMER2);
  timer_enable(TIMER2);

timer_channel_output_mode_config(TIMER2,TIMER_CH_3,TIMER_OC_MODE_TOGGLE);//输出比较反转模式

这里所用的是定时器输出比较模式。

在这里插入图片描述

在这里插入图片描述
通过手册可知,当计数器的值和CxCv寄存器的值,也就是pulseout1、pulseout2匹配时,软件可以选择通道输出的电平变化。参数就是TIMER_OC_MODE_TOGGLE。选择电平反转。

重点

开启通道中断,当计数器的值和CxCv寄存器的值,也就是pulseout1、pulseout2的值一样时,就会触发中断。
void TIMER2_IRQHandler(void)
{
  if(SET == timer_interrupt_flag_get(TIMER2,TIMER_INT_FLAG_CH3))
    {
      timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_CH3);
      capture_val = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_3);
      timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_3,pulseout1+capture_val);
    }
  if(SET == timer_interrupt_flag_get(TIMER2,TIMER_INT_FLAG_CH2))
    {
      timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_CH2);
      capture_val = timer_channel_capture_value_register_read(TIMER2,TIMER_CH_2);
      timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_2,pulseout2+capture_val);
    }
}

timer_channel_capture_value_register_read()读取当前和计数器比较的值。
timer_channel_output_pulse_value_config()设置下一次和计数器比较的值。
这样就会输出频率为
fre1= 1000000/pulseout1*2(Hz)
fre2= 1000000/pulseout2*2(Hz)
不同频率的两种波了。

总结

如有错误,请大家多多指正,谢谢!
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值