查看: 3565|回复: 0

GD32方案分享⑧:PWM实验

[复制链接]
  • TA的每日心情

    2017-11-27 16:33
  • 签到天数: 7 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2017-5-27 23:45:51 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 何昌昕 于 2017-5-27 23:47 编辑

    因为我们需要驱动电机,所以我们需要输出PWM波,参照例程中的PWM例子进行修改

    PWM频率 = (当前主频(200Mhz)/(period+1))/(prescaler+1)
    通道占空比 = (duty/period)*100%

    IMG_4792.JPG IMG_4793.JPG
    分别输出了 50% 10Khz的PWM波 90% 10Khz的PWM波

    1. #include "PWM.h"

    2. void PWM_Init(void)
    3. {
    4.    rcu_periph_clock_enable(RCU_GPIOB);
    5.    
    6.     gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3);
    7.     gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_3);

    8.     gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
    9.     gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);

    10.     gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_11);
    11.     gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_11);

    12.     gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_3);
    13.     gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_10);
    14.     gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_11);
    15.     timer_oc_parameter_struct timer_ocintpara;
    16.     timer_parameter_struct timer_initpara;

    17.     rcu_periph_clock_enable(RCU_TIMER1);
    18.     rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);

    19.     timer_deinit(TIMER1);

    20.     /* TIMER1 configuration */
    21.     timer_initpara.prescaler         = 1;
    22.     timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    23.     timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    24.     timer_initpara.period            = 9999;
    25.     timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    26.     timer_initpara.repetitioncounter = 0;
    27.     timer_init(TIMER1,&timer_initpara);

    28.     /* CH1,CH2 and CH3 configuration in PWM mode1 */
    29.     timer_ocintpara.ocpolarity  = TIMER_OC_POLARITY_HIGH;
    30.     timer_ocintpara.outputstate = TIMER_CCX_ENABLE;

    31.     timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara);
    32.     timer_channel_output_config(TIMER1,TIMER_CH_2,&timer_ocintpara);
    33.     timer_channel_output_config(TIMER1,TIMER_CH_3,&timer_ocintpara);

    34.     /* CH1 configuration in PWM mode1,duty cycle 25% */
    35.     timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,1999);
    36.     timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM0);
    37.     timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);

    38.     /* CH2 configuration in PWM mode1,duty cycle 50% */
    39.     timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_2,4999);
    40.     timer_channel_output_mode_config(TIMER1,TIMER_CH_2,TIMER_OC_MODE_PWM0);
    41.     timer_channel_output_shadow_config(TIMER1,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);

    42.     /* CH3 configuration in PWM mode1,duty cycle 75% */
    43.     timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_3,8999);
    44.     timer_channel_output_mode_config(TIMER1,TIMER_CH_3,TIMER_OC_MODE_PWM0);
    45.     timer_channel_output_shadow_config(TIMER1,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);

    46.     /* auto-reload preload enable */
    47.     timer_auto_reload_shadow_enable(TIMER1);
    48.     /* auto-reload preload enable */
    49.     timer_enable(TIMER1);
    50. }
    复制代码
    明天开始搭建小车硬件 附件: myGD32 - 副本.rar (7.42 MB, 下载次数: 16)
    回复

    使用道具 举报

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

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-11-15 08:08 , Processed in 0.122839 second(s), 16 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.