打印
[研电赛技术支持]

gd32的定时器比较模式的配置问题

[复制链接]
2276|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
a864894787|  楼主 | 2021-11-4 09:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
想使用定时器的比较反转模式,按照例程进行配置,发现比较模式不起作用,io的改变只是因为定时器的溢出进行的io反转,求各位大神指导!
void gpio_config(void)
{
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_AF);

    /*configure PA1(TIMER1 CH1) as alternate function*/
    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
}

/**
    \brief      configure the timer peripheral
    \param[in]  none
    \param[out] none
    \retval     none
  */
void timer_config(void)
{
    /* ---------------------------------------------------------------------------
    TIMER1 configuration: output compare toggle mode:
    TIMER1CLK = systemcoreclock / 5400 = 20K,
    CH1 update rate = TIMER1 counter clock / CH1VAL = 20000/4000 = 5 Hz
    ----------------------------------------------------------------------------*/
    timer_oc_parameter_struct timer_ocintpara;
    timer_parameter_struct timer_initpara;

       
    rcu_periph_clock_enable(RCU_TIMER1);

    timer_deinit(TIMER1);

    /* TIMER1 configuration */
    timer_initpara.prescaler         = 108-1;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = 9999;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER1,&timer_initpara);

     /* CH1 configuration in OC TOGGLE mode */
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    timer_channel_output_config(TIMER1,TIMER_CH_1,&timer_ocintpara);

    timer_channel_output_pulse_value_config(TIMER1,TIMER_CH_1,4999);
    timer_channel_output_mode_config(TIMER1,TIMER_CH_1,TIMER_OC_MODE_TOGGLE);
    timer_channel_output_shadow_config(TIMER1,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);

    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER1);
    timer_enable(TIMER1);
}
int main(void)
{
        int i=1;
        rcu_apb2_clock_config(RCU_APB2_CKAHB_DIV1);

        rcu_periph_clock_enable(RCU_GPIOC);
       
        systick_config();
       
        gpio_init(GPIOC, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
        USART0_init(115200U);
       
        printf("system running -------\r\n");
       
        gpio_config();
    timer_config();
       
       
    while(1)
    {               
                        TIMER_CAR(TIMER1) = 3000;
                        TIMER_CH1CV(TIMER1) = 2000;
                        printf("key press\r\n");
                        delay_ms(1000);
                        gpio_bit_set(GPIOC, GPIO_PIN_13);
                        delay_ms(1000);
                        gpio_bit_reset(GPIOC, GPIO_PIN_13);

    }
    return 0;
}

使用特权

评论回复
沙发
sadicy| | 2021-11-11 10:43 | 只看该作者
帮顶,看不出毛病

使用特权

评论回复
板凳
lifeforrent| | 2021-11-11 19:14 | 只看该作者
在while(1)中加入下面代码:
while(1)
    {  
                delay_1ms(10);
                if(i>10000){
                        i=0;
                }else{
                        i=i+2000;
                }               
        TIMER_CH1CV(TIMER1)= i;
    }
定时器溢出不会导致电平翻转,其实比较翻转已经发生了,只是你这种方法用示波器看不到而已,你配置的定时器周期是10ms,所以在while循环中10ms更改一次比较值,每次比较值设置为不同的比较值,就可以在示波器上观察到波形变化。

使用特权

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

本版积分规则

1

主题

1

帖子

0

粉丝