打印
[研电赛技术支持]

GD32通用定时器、高级定时器PWM输出例程

[复制链接]
2966|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
磨砂|  楼主 | 2021-9-8 07:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
通用定时器
/**********************************************************************************************************
*        函 数 名:PWMOut_LCD_BK_Init
*        功能说明:TFT背光PWM初始化
*        形    参:
*        返 回 值:
**********************************************************************************************************/
void PWMOut_LCD_BK_Init(void)  //TIM4_CH2    LCD_BK-PA2
{
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;
       
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_AF);

    /*Configure PA2(TIMER4 CH2) as alternate function*/
    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);

    rcu_periph_clock_enable(RCU_TIMER4);
    timer_deinit(TIMER4);

    /* TIMER1 configuration */
    timer_initpara.prescaler         = 53;  //TIMER1CLK = SystemCoreClock / 54 = 2MHz
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 99;  // 2MHz/100=20KHz  LCD背光频率20K
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER4,&timer_initpara);

    /* CH2 configuration in PWM mode1 */
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_LOW;       //通道输出极性
    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;            //通道输出状态
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;     //通道处于空闲时的输出       
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;     //互补通道输出极性
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;          //互补通道输出状态
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;    //互补通道处于空闲时的输出
    timer_channel_output_config(TIMER4,TIMER_CH_2,&timer_ocintpara);

    /* CH2 configuration in PWM mode1,duty cycle 50% */
    timer_channel_output_pulse_value_config(TIMER4,TIMER_CH_2,89);
    timer_channel_output_mode_config(TIMER4,TIMER_CH_2,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER4,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);

    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER4);
    /* auto-reload preload enable */
    timer_enable(TIMER4);       
}

/**********************************************************************************************************
*        函 数 名:Set_LCD_Bk
*        功能说明:设置LCD的背光
*        形    参:pulse:背光值,取值范围:0-99,取值0关闭背光,取值99背光最亮
*        返 回 值:无
**********************************************************************************************************/
void Set_LCD_Bk(uint32_t pulse)
{
        TIMER_CH2CV(TIMER4) = (uint32_t)pulse;
}


使用特权

评论回复
沙发
磨砂|  楼主 | 2021-9-8 07:31 | 只看该作者
高级定时器
/**********************************************************************************************************
*        函 数 名:PWM_STEP_GS_Init
*        功能说明:电机REF输出
*        形    参:
*        返 回 值:
**********************************************************************************************************/
void PWM_REF_GS_Init(void)  //TIM7_CH3    SSM_REF-PC9
{
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;
       
    rcu_periph_clock_enable(RCU_GPIOC);
    rcu_periph_clock_enable(RCU_AF);
        rcu_periph_clock_enable(RCU_TIMER7);
       
    /*Configure PC9(TIMER7 CH3) as alternate function*/
    gpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);

    timer_deinit(TIMER7);

    /* TIMER7 configuration */
    timer_initpara.prescaler         = 5;  //TIMER7CLK = SystemCoreClock / ( 5+1 )= 18MHz
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 899;  // 18MHz/(1799+1)=10KHz  周期
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER7,&timer_initpara);

    /* CH2 configuration in PWM mode1 */
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_LOW;       //通道输出极性
    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;            //通道输出状态
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;     //通道处于空闲时的输出       
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;     //互补通道输出极性
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;          //互补通道输出状态
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;    //互补通道处于空闲时的输出
    timer_channel_output_config(TIMER7,TIMER_CH_3,&timer_ocintpara);

    /* CH2 configuration in TOGGLE mode */
        timer_channel_output_pulse_value_config(TIMER7,TIMER_CH_3,99);
    timer_channel_output_mode_config(TIMER7,TIMER_CH_3,TIMER_OC_MODE_PWM1);
    timer_channel_output_shadow_config(TIMER7,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE);

    /* TIMER7 primary output function enable 高级定时器(0和7)特有*/
    timer_primary_output_config(TIMER7,ENABLE);
       
    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER7);

    timer_enable(TIMER7);       
}


使用特权

评论回复
板凳
beizhaibei| | 2021-9-10 11:10 | 只看该作者
按照例程设置多路输出后上板跑发现只有CH0有输出,可以帮忙看看什么问题吗
    /* -----------------------------------------------------------------------
    TIMER2 configuration: generate 2 PWM signals:
    ----------------------------------------------------------------------- */
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;

    /*Configure the pin as alternate function*/
    gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6);
    gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);

    gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_7);
    gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7);

    gpio_af_set(GPIOC, GPIO_AF_1, GPIO_PIN_6); /* PWM LED1 */
    gpio_af_set(GPIOC, GPIO_AF_1, GPIO_PIN_7); /* PWM LED2 */

    rcu_periph_clock_enable(RCU_TIMER2);

    timer_deinit(TIMER2);

    /* TIMER1 configuration */
#ifdef GD32F330
    timer_initpara.prescaler = 0;
#endif /* GD32F330 */
#ifdef GD32F350
    timer_initpara.prescaler = 107;
#endif /* GD32F350 */
    timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection = TIMER_COUNTER_UP;
    timer_initpara.period = (n_LEDPWMPeriod - 1);
    timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
   
    timer_init(TIMER2, &timer_initpara);

    /* CH0 configuration in PWM mode */
    timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
    timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
    timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;

    timer_channel_output_config(TIMER2, TIMER_CH_0, &timer_ocintpara); /* PC6 LED1*/
    timer_channel_output_config(TIMER2, TIMER_CH_1, &timer_ocintpara); /* PC7 LED2 */

    timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_0, n_LEDPWMPeriod - 1);
    timer_channel_output_mode_config(TIMER2, TIMER_CH_0, TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER2, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);

    timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, n_LEDPWMPeriod - 1);
    timer_channel_output_mode_config(TIMER2, TIMER_CH_1, TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER2, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);

    timer_primary_output_config(TIMER2,ENABLE);
    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER2);
    /* auto-reload preload enable */
    timer_enable(TIMER2);

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

94

主题

4126

帖子

2

粉丝