查看: 4570|回复: 0

[GD32F450测试]4、PWM测试

[复制链接]
  • TA的每日心情
    开心
    2019-11-19 11:07
  • 签到天数: 226 天

    连续签到: 1 天

    [LV.7]常住居民III

    发表于 2017-5-26 16:57:44 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 zhjb1 于 2017-5-26 17:01 编辑

    结果多次尝试最后在几个实例的帮助下解决了,直接上代码了。首先在文档头中间变量定义:
    uint16_t dut0=1200,dut1=2000,dut2=5000;
    int ddut0=1000,ddut1=2000,ddut2=2500;
    接着在ioInit()函数中增加Timer1的IO定义:
    //enable timer1 clock
    rcu_periph_clock_enable(RCU_TIMER1);
    rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4);
    adc_clock_config(ADC_ADCCK_PCLK2_DIV4);//config ADC clock
    //enable GPIOB
    rcu_periph_clock_enable(RCU_GPIOB);
    //configure PB3(TIMER1 CH1,CH2,CH3) as alternate function
    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_3);
    gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_3);
    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_10);
    gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_10);
    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_11);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_11);
    gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_11);
    之后第一Timer1初始化函数,系统时钟是120MHz,以下是按照此来计算的:
    //configure the TIMER peripheral
    void timeInit(void){
    timer_parameter_struct timer_initpara;
    timer_oc_parameter_struct timer_ocintpara;
    rcu_periph_clock_enable(RCU_TIMER1);
    //TIMER1  configuration
    timer_deinit(TIMER1);
    //TIMER1 configuration
    timer_initpara.prescaler         = 119;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 11999;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER1,&timer_initpara);
    //CH1,CH2 and CH3 configuration in PWM mode
    timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
    timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara);
    timer_channel_output_config(TIMER1,TIMER_CH_2,&timer_ocintpara);
    timer_channel_output_config(TIMER1,TIMER_CH_3,&timer_ocintpara);
    timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,ddut0);
    timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
    timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_2,ddut1);
    timer_channel_output_mode_config(TIMER1,TIMER_CH_2,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER1,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
    timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_3,ddut2);
    timer_channel_output_mode_config(TIMER1,TIMER_CH_3,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER1,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);
    //auto-reload preload enable
    timer_auto_reload_shadow_enable(TIMER1);
    timer_enable(TIMER1);
    }
    之后再main主函数前边加上Timer初始化函数调用:
    timeInit();
    在循环系while中添加改变PWM周期的算法和重写语句:
    if(dut0>10000) ddut0=-100;if(dut0<200) ddut0=100;dut0 +=ddut0;
    if(dut1>8000) ddut1=-100;if(dut1<200) ddut1=100;dut1 +=ddut1;
    if(dut2>15000) ddut2=-150;if(dut2<200) ddut2=150;dut2 +=ddut2;
    timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,dut0);
    timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_2,dut1);
    timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_3,dut2);
    最后在dispBase()和dispMes()函数中增加显示PWM周期的时间就OK了。硬件连接见照片41,示波器采样3路PWM之1路的波形见照片42,串口显示计数器+ADC+3路PWM数据见图43。
    忘了说明了,我的所有实验是基于GPIO_LED实例的。

    41

    41

    42

    42

    43

    43
    回复

    使用道具 举报

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

    本版积分规则

    关闭

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



    手机版|小黑屋|与非网

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

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

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.