GD32E230使用高级定时器的比较输出

[复制链接]
16205|10
手机看帖
扫描二维码
随时随地手机跟帖
风来吴山|  楼主 | 2021-4-8 10:18 | 显示全部楼层 |阅读模式
初始化的时候就死机了,不知道怎么使用高级定时器的比较输出模式
void b_timer_config(uint16_t __pres, uint16_t __period, uint16_t __pulse)
{
    // timer_oc_parameter_struct timer_ocinitpara;
    timer_parameter_struct timer_initpara;
    timer_break_parameter_struct timer_breakpara;

    rcu_periph_clock_enable(RCU_TIMER0);
    timer_deinit(TIMER0);

    timer_struct_para_init(&timer_initpara);
    timer_initpara.prescaler         = __pres;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = __period;                // 10KHz
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER0, &timer_initpara);

    // timer_channel_output_struct_para_init(&timer_ocinitpara);
    // timer_ocinitpara.outputstate  = TIMER_CCX_ENABLE;
    // timer_ocinitpara.outputnstate = TIMER_CCXN_DISABLE;
    // timer_ocinitpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    // timer_ocinitpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    // timer_ocinitpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    // timer_ocinitpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    // timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocinitpara);

    timer_break_struct_para_init(&timer_breakpara);
    timer_breakpara.runoffstate      = TIMER_ROS_STATE_ENABLE;
    timer_breakpara.ideloffstate     = TIMER_IOS_STATE_ENABLE;
    timer_breakpara.deadtime         = 164;
    timer_breakpara.breakpolarity    = TIMER_BREAK_POLARITY_LOW;
    timer_breakpara.outputautostate  = TIMER_OUTAUTO_ENABLE;
    timer_breakpara.protectmode      = TIMER_CCHP_PROT_OFF;
    timer_breakpara.breakstate       = TIMER_BREAK_ENABLE;
    timer_break_config(TIMER0, &timer_breakpara);

    timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, __pulse);
    timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM0);
    // timer_channel_output_shadow_config(TIMER0, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
    // timer_primary_output_config(TIMER0, ENABLE);
    nvic_irq_enable(TIMER0_Channel_IRQn, 1);
    timer_interrupt_enable(TIMER0, TIMER_INT_CH0);
    timer_interrupt_disable(TIMER0, TIMER_INT_BRK);
    timer_auto_reload_shadow_enable(TIMER0);
    timer_enable(TIMER0);
}

void TIMER0_IRQHandler(void)
{
    static uint8_t status = 0;
    if (SET == timer_interrupt_flag_get(TIMER0, TIMER_INT_CH0))
    {
        status++;
        switch (status % 3){
        case 0:
            printf("a\n");
            timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, tim[B.idle);
            timer_channel_output_pulse_value_config(TIMER15, TIMER_CH_0, 0);
            break;
        case 1:
            printf("b\n");
            timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, tim[B.t_off);
            timer_channel_output_pulse_value_config(TIMER15, TIMER_CH_0, 0);
            break;
        case 2:
            printf("c\n");
            timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, tim[B.t_on);
            timer_channel_output_pulse_value_config(TIMER15, TIMER_CH_0, pwm_ch[B.pulse);
            break;
        default:
            break;
        }
        timer_interrupt_flag_clear(TIMER0, TIMER_INT_CH0);
    }
}

使用特权

评论回复
风来吴山|  楼主 | 2021-4-8 17:01 | 显示全部楼层
本帖最后由 风来吴山 于 2021-4-8 17:02 编辑
看了芯片手册,把通道换到CH3也不行。
void b_timer_config(uint16_t __pres, uint16_t __period, uint16_t __pulse)
{
    timer_parameter_struct timer_initpara;
    // timer_oc_parameter_struct timer_ocinitpara;
    // timer_break_parameter_struct timer_breakpara;

    rcu_periph_clock_enable(RCU_TIMER0);
    timer_deinit(TIMER0);

    timer_struct_para_init(&timer_initpara);
    timer_initpara.prescaler         = __pres;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.period            = __period;                // 10KHz
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER0, &timer_initpara);

    // timer_channel_output_struct_para_init(&timer_ocinitpara);
    // timer_ocinitpara.outputstate  = TIMER_CCX_DISABLE;
    // timer_ocinitpara.outputnstate = TIMER_CCXN_DISABLE;
    // timer_ocinitpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    // timer_ocinitpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    // timer_ocinitpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    // timer_ocinitpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    // timer_channel_output_config(TIMER14, TIMER_CH_0, &timer_ocinitpara);

    // timer_break_struct_para_init(&timer_breakpara);
    // timer_breakpara.runoffstate      = TIMER_ROS_STATE_ENABLE;
    // timer_breakpara.ideloffstate     = TIMER_IOS_STATE_ENABLE;
    // timer_breakpara.deadtime         = 164;
    // timer_breakpara.breakpolarity    = TIMER_BREAK_POLARITY_LOW;
    // timer_breakpara.outputautostate  = TIMER_OUTAUTO_ENABLE;
    // timer_breakpara.protectmode      = TIMER_CCHP_PROT_OFF;
    // timer_breakpara.breakstate       = TIMER_BREAK_ENABLE;
    // timer_break_config(TIMER0, &timer_breakpara);

    // timer_channel_output_shadow_config(TIMER0, TIMER_CH_3, TIMER_OC_SHADOW_ENABLE);
    timer_channel_output_mode_config(TIMER0, TIMER_CH_3, TIMER_OC_MODE_TIMING);
    timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, __pulse);
    nvic_irq_enable(TIMER0_Channel_IRQn, 1);
    timer_interrupt_enable(TIMER0, TIMER_INT_CH3);
    timer_auto_reload_shadow_enable(TIMER0);
    timer_enable(TIMER0);
}

void TIMER0_IRQHandler(void)
{
    static uint8_t status = 0;
    if (SET == timer_interrupt_flag_get(TIMER0, TIMER_INT_FLAG_CH3))
    {
        status++;
        switch (status % 3){
        case 0:
            printf("a\n");
            timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, tim[B].idle);
            timer_channel_output_pulse_value_config(TIMER15, TIMER_CH_0, 0);
            break;
        case 1:
            printf("b\n");
            timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, tim[B].t_off);
            timer_channel_output_pulse_value_config(TIMER15, TIMER_CH_0, 0);
            break;
        case 2:
            printf("c\n");
            timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, tim[B].t_on);
            timer_channel_output_pulse_value_config(TIMER15, TIMER_CH_0, pwm_ch[B].pulse);
            break;
        default:
            break;
        }
        timer_interrupt_flag_clear(TIMER0, TIMER_INT_FLAG_CH3);
    }
}

使用特权

评论回复
caigang13| | 2021-4-8 18:44 | 显示全部楼层
一来就是上程序,呵呵。

使用特权

评论回复
风来吴山|  楼主 | 2021-4-9 08:49 | 显示全部楼层
caigang13 发表于 2021-4-8 18:44
一来就是上程序,呵呵。

说的很清楚了,这段代码运行就死机,不想思考就别答。

使用特权

评论回复
xdqfc| | 2021-4-9 08:59 | 显示全部楼层
本帖最后由 xdqfc 于 2021-4-9 09:03 编辑

仿真看一下,程序究竟停在什么地方,然后再分析。官方的定时器库文件介绍的不是很能理解,包括STM32也一个屌样,所以,咱在使用定时器相关功能的时候,都是使用寄存器编程,其他外设用库文件。

使用特权

评论回复
zfazhan| | 2021-4-9 17:03 | 显示全部楼层
中断向量搞错了吧。230没有TIMER0_IRQHandler,只有TIMER0_BRK_UP_TRG_COM_IRQHandler,TIMER0_Channel_IRQHandler。除非你自己重新映射了

使用特权

评论回复
sonicll| | 2021-4-9 17:35 | 显示全部楼层
风来吴山 发表于 2021-4-9 08:49
说的很清楚了,这段代码运行就死机,不想思考就别答。

那你自己思考了吗,至少单步调试一下,看看是哪一行代码,读写哪个寄存器时出的问题?死机,是怎么个死法,死机这个词太笼统了,是出现异常fault了吗,你自己都描述不清楚自己的问题,让别人怎么帮你

使用特权

评论回复
风来吴山|  楼主 | 2021-4-11 16:57 | 显示全部楼层
zfazhan 发表于 2021-4-9 17:03
中断向量搞错了吧。230没有TIMER0_IRQHandler,只有TIMER0_BRK_UP_TRG_COM_IRQHandler,TIMER0_Channel_IRQ ...

嗯,是的。

使用特权

评论回复
风来吴山|  楼主 | 2021-4-11 16:58 | 显示全部楼层
sonicll 发表于 2021-4-9 17:35
那你自己思考了吗,至少单步调试一下,看看是哪一行代码,读写哪个寄存器时出的问题?死机,是怎么个死法 ...

已经解决了。

使用特权

评论回复
stefgq| | 2021-11-9 00:15 | 显示全部楼层
这是互补输出的PWM吗

使用特权

评论回复
yg.f| | 2021-11-16 17:09 | 显示全部楼层
我的Timer0为啥没有输出呢?其他定时器可以

使用特权

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

本版积分规则

7

主题

40

帖子

1

粉丝