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

Posted qlexcel

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GD32通用定时器高级定时器PWM输出例程相关的知识,希望对你有一定的参考价值。

通用定时器

/**********************************************************************************************************
*	函 数 名: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;
}

高级定时器

/**********************************************************************************************************
*	函 数 名: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);	
}

以上是关于GD32通用定时器高级定时器PWM输出例程的主要内容,如果未能解决你的问题,请参考以下文章

STM32 PWM输出(映射)

CMT2380F32模块开发10-高级定时器例程

CMT2380F32模块开发10-高级定时器例程

STM32-通用定时器

STM32定时器输出PWM频率和步进电机控制速度计算

STM32定时器输出PWM频率和步进电机控制速度计算