4504|3

85

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

【GD32E231_DIY】-04:6路PWM输出 [复制链接]

基于TIMER2 CH0\CH1\CH2\CH3、TIMER15 CH0_N、TIMER16 CH0_N 6路PWM输出
  1. void PWM_GPIO_config(void)
  2. {
  3.     rcu_periph_clock_enable(RCU_GPIOA);
  4.     /* configure PA6 7(TIMER2 CH0 CH1) as alternate function */
  5.     gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6 | GPIO_PIN_7);
  6.     gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_6 | GPIO_PIN_7);
  7.     gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_6 | GPIO_PIN_7);
  8.     rcu_periph_clock_enable(RCU_GPIOB);
  9.     /* configure PB0 1(TIMER2 CH2 CH3) as alternate function */
  10.     gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_0 | GPIO_PIN_1);
  11.     gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_0 | GPIO_PIN_1);
  12.     gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_0|GPIO_PIN_1);

  13.     rcu_periph_clock_enable(RCU_GPIOB);
  14.     gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_2);
  15.     gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
  16.     gpio_bit_reset(GPIOA,GPIO_PIN_2);

  17.     /* configure PB6 7 8(TIMER15 CH0_ON TIMER16_CH0_ON TIMER15 CH0) as alternate function */
  18.     gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6 | GPIO_PIN_7 );
  19.     gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_6 | GPIO_PIN_7);
  20.     gpio_af_set(GPIOB, GPIO_AF_2, GPIO_PIN_6 | GPIO_PIN_7);
  21. }

  22. void TIM2_Config(void)
  23. {
  24.     timer_oc_parameter_struct timer_ocintpara;
  25.     timer_parameter_struct timer_initpara;
  26.    
  27.     PWM_GPIO_config();

  28.     rcu_periph_clock_enable(RCU_TIMER2);

  29.     timer_deinit(TIMER2);

  30.     /* TIMER configuration */
  31.     timer_initpara.prescaler         = 500;
  32.     timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
  33.     timer_initpara.counterdirection  = TIMER_COUNTER_UP;
  34.     timer_initpara.period                       = 5999;
  35.     timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
  36.     timer_initpara.repetitioncounter = 0;
  37.     timer_init(TIMER2,&timer_initpara);

  38.     /* configurate CH0 in PWM mode0 */
  39.     timer_ocintpara.ocpolarity  = TIMER_OC_POLARITY_LOW;
  40.     timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  41.     timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
  42.     timer_channel_output_config(TIMER2,TIMER_CH_0,&timer_ocintpara);

  43.     timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_0,632);
  44.     timer_channel_output_mode_config(TIMER2,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  45.     timer_channel_output_shadow_config(TIMER2,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);

  46.     /* configurate CH1 in PWM mode0 */
  47.     timer_channel_output_config(TIMER2,TIMER_CH_1,&timer_ocintpara);

  48.     timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_1,940);
  49.     timer_channel_output_mode_config(TIMER2,TIMER_CH_1,TIMER_OC_MODE_PWM0);
  50.     timer_channel_output_shadow_config(TIMER2,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);

  51.     /* configurate CH2 in PWM mode0 */
  52.     timer_channel_output_config(TIMER2,TIMER_CH_2,&timer_ocintpara);

  53.     timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_2,1500);
  54.     timer_channel_output_mode_config(TIMER2,TIMER_CH_2,TIMER_OC_MODE_PWM0);
  55.     timer_channel_output_shadow_config(TIMER2,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);

  56.     /* configurate CH3 in PWM mode0 */
  57.     timer_channel_output_config(TIMER2,TIMER_CH_3,&timer_ocintpara);

  58.     timer_channel_output_pulse_value_config(TIMER2,TIMER_CH_3,632);
  59.     timer_channel_output_mode_config(TIMER2,TIMER_CH_3,TIMER_OC_MODE_PWM0);
  60.     timer_channel_output_shadow_config(TIMER2,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);

  61.     timer_primary_output_config(TIMER2,ENABLE);
  62.     /* auto-reload preload enable */
  63.     timer_auto_reload_shadow_enable(TIMER2);
  64.     /* auto-reload preload enable */
  65.     timer_enable(TIMER2);

  66.     //timer_channel_output_state_config(TIMER2, TIMER_CH_0, TIMER_CCX_DISABLE);
  67. }

  68. void TIM15_Config(void)
  69. {
  70.     timer_oc_parameter_struct timer_ocintpara;
  71.     timer_parameter_struct timer_initpara;
  72.    
  73.     rcu_periph_clock_enable(RCU_TIMER15);

  74.     timer_deinit(TIMER15);

  75.     /* TIMER configuration */
  76.     timer_initpara.prescaler         = 0;
  77.     timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
  78.     timer_initpara.counterdirection  = TIMER_COUNTER_UP;
  79.     timer_initpara.period                       = 1895;
  80.     timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
  81.     timer_initpara.repetitioncounter = 0;
  82.     timer_init(TIMER15,&timer_initpara);

  83.     /* configurate CH0 in PWM mode0 */
  84.     timer_ocintpara.outputstate  = TIMER_CCX_DISABLE;
  85.     timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
  86.     timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
  87.     timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
  88.     timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
  89.     timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  90.     timer_channel_output_config(TIMER15,TIMER_CH_0,&timer_ocintpara);

  91.     timer_channel_output_pulse_value_config(TIMER15,TIMER_CH_0,632);
  92.     timer_channel_output_mode_config(TIMER15,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  93.     timer_channel_output_shadow_config(TIMER15,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);

  94.     timer_primary_output_config(TIMER15,ENABLE);
  95.     /* auto-reload preload enable */
  96.     timer_auto_reload_shadow_enable(TIMER15);
  97.     /* auto-reload preload enable */
  98.     timer_enable(TIMER15);
  99. }


  100. void TIM16_Config(void)
  101. {
  102.     timer_oc_parameter_struct timer_ocintpara;
  103.     timer_parameter_struct timer_initpara;
  104.    
  105.     rcu_periph_clock_enable(RCU_TIMER16);

  106.     timer_deinit(TIMER16);

  107.     /* TIMER configuration */
  108.     timer_initpara.prescaler         = 0;
  109.     timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
  110.     timer_initpara.counterdirection  = TIMER_COUNTER_UP;
  111.     timer_initpara.period                       = 1895;
  112.     timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
  113.     timer_initpara.repetitioncounter = 0;
  114.     timer_init(TIMER16,&timer_initpara);

  115.     /* configurate CH0 in PWM mode0 */
  116.     timer_ocintpara.outputstate  = TIMER_CCX_DISABLE;
  117.     timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
  118.     timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
  119.     timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
  120.     timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
  121.     timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  122.     timer_channel_output_config(TIMER16,TIMER_CH_0,&timer_ocintpara);

  123.     timer_channel_output_pulse_value_config(TIMER16,TIMER_CH_0,500);
  124.     timer_channel_output_mode_config(TIMER16,TIMER_CH_0,TIMER_OC_MODE_PWM0);
  125.     timer_channel_output_shadow_config(TIMER16,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);

  126.     timer_primary_output_config(TIMER16,ENABLE);
  127.     /* auto-reload preload enable */
  128.     timer_auto_reload_shadow_enable(TIMER16);
  129.     /* auto-reload preload enable */
  130.     timer_enable(TIMER16);
  131. }
复制代码



此帖出自GD32 MCU论坛

最新回复

非常感谢,解决了我遇到的问题。   详情 回复 发表于 2022-3-18 10:44
点赞 关注
 

回复
举报

172

帖子

0

TA的资源

宇宙尘埃

沙发
 
谢谢分享!
此帖出自GD32 MCU论坛
 
 
 

回复

208

帖子

0

TA的资源

纯净的硅(初级)

板凳
 
此帖出自GD32 MCU论坛
 
个人签名We are a team and we work as a team !
 
 

回复

1

帖子

2

TA的资源

一粒金砂(初级)

4
 

非常感谢,解决了我遇到的问题。

此帖出自GD32 MCU论坛
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/7 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表