想问下 GD32F407 怎么配置定时器发出来PWM,单次触发模式

[复制链接]
1788|1
 楼主| 793040564 发表于 2024-8-3 19:00 | 显示全部楼层 |阅读模式
想问下 GD32F407VE 怎么配置定时器发出来PWM,单次触发模式,就是每次发出来固定数量的方波
  1. rcu_periph_clock_enable(RCU_GPIOE);
  2.         rcu_periph_clock_enable(RCU_GPIOB);
  3.         rcu_periph_clock_enable(RCU_GPIOA);
  4.        
  5.         //ch0
  6.         gpio_af_set(GPIOE, GPIO_AF_1, GPIO_PIN_9);
  7.         gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_PULLDOWN, GPIO_PIN_9);
  8.         gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
  9.        
  10.         //ch1
  11.         gpio_af_set(GPIOE, GPIO_AF_1, GPIO_PIN_11);
  12.         gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_PULLDOWN, GPIO_PIN_11);
  13.         gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
  14.        
  15.         //ch2
  16.         gpio_af_set(GPIOE, GPIO_AF_1, GPIO_PIN_13);
  17.         gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_PULLDOWN, GPIO_PIN_13);
  18.         gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
  19.        
  20.         //ch3
  21.         gpio_af_set(GPIOE, GPIO_AF_1, GPIO_PIN_14);
  22.         gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_PULLDOWN, GPIO_PIN_14);
  23.         gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_14);
  1. timer_parameter_struct    timer_initpara;
  2.         timer_oc_parameter_struct timer_ocintpara;
  3.        
  4.         rcu_periph_clock_enable(RCU_TIMER0);
  5.         timer_internal_clock_config(TIMER0);
  6.        
  7.          /* 定时器0 配置 */
  8.     timer_initpara.prescaler         = 167;     // 预分频器配置
  9.     timer_initpara.alignedmode       = TIMER_COUNTER_EDGE; // 边沿对齐模式
  10.     timer_initpara.period            = 999;               // 溢出值
  11.         timer_initpara.counterdirection  = TIMER_COUNTER_UP;  // 向上计数
  12.         timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
  13.         timer_initpara.repetitioncounter = 0;
  14.     timer_init(TIMER0, &timer_initpara);   // 定时器0 初始化
  15.        
  16.     //PWM输出配置
  17.     timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
  18.         timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
  19.         timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_LOW;
  20.     timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_LOW;
  21.     timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
  22.     timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  23.        
  24.         timer_counter_value_config(TIMER0, 0);
  25.        
  26.         /* 对PWM模式中的通道0 进行配置 */
  27.     timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocintpara);
  28.     /* 通道0 的占空比设置 */
  29.     timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, 499);// 50%占空比
  30.     timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM0);
  31.     timer_channel_output_shadow_config(TIMER0, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
  32.        
  33.         /* 对PWM模式中的通道1 进行配置 */
  34.     timer_channel_output_config(TIMER0, TIMER_CH_1, &timer_ocintpara);
  35.     /* 通道1 的占空比设置 */
  36.     timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_1, 499);// 50%占空比
  37.     timer_channel_output_mode_config(TIMER0, TIMER_CH_1,TIMER_OC_MODE_PWM0);
  38.     timer_channel_output_shadow_config(TIMER0, TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
  39.        
  40.         /* 对PWM模式中的通道2 进行配置 */
  41.     timer_channel_output_config(TIMER0,TIMER_CH_2, &timer_ocintpara);
  42.     /* 通道2 的占空比设置 */
  43.     timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_2, 499);// 50%占空比
  44.     timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_PWM0);
  45.     timer_channel_output_shadow_config(TIMER0, TIMER_CH_2, TIMER_OC_SHADOW_DISABLE);
  46.        
  47.         /* 对PWM模式中的通道3 进行配置 */
  48.     timer_channel_output_config(TIMER0, TIMER_CH_3, &timer_ocintpara);
  49.     /* 通道3 的占空比设置 */
  50.     timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, 499);// 50%占空比
  51.     timer_channel_output_mode_config(TIMER0, TIMER_CH_3, TIMER_OC_MODE_PWM0);
  52.     timer_channel_output_shadow_config(TIMER0, TIMER_CH_3, TIMER_OC_SHADOW_DISABLE);

  53.         timer_single_pulse_mode_config(TIMER0, TIMER_SP_MODE_SINGLE);
  54.     /* auto-reload preload enable */
  55.     timer_auto_reload_shadow_enable(TIMER0);
  1. timer_autoreload_value_config(TIMER0, cycle);
  2.         timer_repetition_value_config(TIMER0, pulse_num);
  3.         timer_prescaler_config(TIMER0, psc, TIMER_PSC_RELOAD_NOW);
  4.        
  5.         /* 通道0 的占空比设置 */
  6.     timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, ch0_duty);// 50%占空比
  7.         /* 通道1 的占空比设置 */
  8.     timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_1, ch1_duty);// 50%占空比
  9.         /* 通道2 的占空比设置 */
  10.     timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_2, ch2_duty);// 50%占空比
  11.         /* 通道3 的占空比设置 */
  12.     timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, ch3_duty);// 50%占空比
  13.        
  14.         timer_single_pulse_mode_config(TIMER0, TIMER_SP_MODE_SINGLE);
  15.        
  16.     timer_enable(TIMER0);   // 使能定时器0
程序配置如上,不能发出波形
stormwind123 发表于 2024-8-14 16:25 | 显示全部楼层
参考GD32F407的参考手册和库函数手册
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

8

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部