问答

汇集网友智慧,解决技术难题

21ic问答首页 - GD32E230使用高级定时器的比较输出

GD32E230使用高级定时器的比较输出 赏100家园币

风来吴山2021-04-08
回答 +关注 24
17972人浏览 10人回答问题 分享 举报
10 个回答
  • 本帖最后由 风来吴山 于 2021-4-8 17:02 编辑
    1. 看了芯片手册,把通道换到CH3也不行。
    1. void b_timer_config(uint16_t __pres, uint16_t __period, uint16_t __pulse)
    2. {
    3.     timer_parameter_struct timer_initpara;
    4.     // timer_oc_parameter_struct timer_ocinitpara;
    5.     // timer_break_parameter_struct timer_breakpara;

    6.     rcu_periph_clock_enable(RCU_TIMER0);
    7.     timer_deinit(TIMER0);

    8.     timer_struct_para_init(&timer_initpara);
    9.     timer_initpara.prescaler         = __pres;
    10.     timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    11.     timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    12.     timer_initpara.period            = __period;                // 10KHz
    13.     timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    14.     timer_initpara.repetitioncounter = 0;
    15.     timer_init(TIMER0, &timer_initpara);

    16.     // timer_channel_output_struct_para_init(&timer_ocinitpara);
    17.     // timer_ocinitpara.outputstate  = TIMER_CCX_DISABLE;
    18.     // timer_ocinitpara.outputnstate = TIMER_CCXN_DISABLE;
    19.     // timer_ocinitpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    20.     // timer_ocinitpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    21.     // timer_ocinitpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    22.     // timer_ocinitpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    23.     // timer_channel_output_config(TIMER14, TIMER_CH_0, &timer_ocinitpara);

    24.     // timer_break_struct_para_init(&timer_breakpara);
    25.     // timer_breakpara.runoffstate      = TIMER_ROS_STATE_ENABLE;
    26.     // timer_breakpara.ideloffstate     = TIMER_IOS_STATE_ENABLE;
    27.     // timer_breakpara.deadtime         = 164;
    28.     // timer_breakpara.breakpolarity    = TIMER_BREAK_POLARITY_LOW;
    29.     // timer_breakpara.outputautostate  = TIMER_OUTAUTO_ENABLE;
    30.     // timer_breakpara.protectmode      = TIMER_CCHP_PROT_OFF;
    31.     // timer_breakpara.breakstate       = TIMER_BREAK_ENABLE;
    32.     // timer_break_config(TIMER0, &timer_breakpara);

    33.     // timer_channel_output_shadow_config(TIMER0, TIMER_CH_3, TIMER_OC_SHADOW_ENABLE);
    34.     timer_channel_output_mode_config(TIMER0, TIMER_CH_3, TIMER_OC_MODE_TIMING);
    35.     timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, __pulse);
    36.     nvic_irq_enable(TIMER0_Channel_IRQn, 1);
    37.     timer_interrupt_enable(TIMER0, TIMER_INT_CH3);
    38.     timer_auto_reload_shadow_enable(TIMER0);
    39.     timer_enable(TIMER0);
    40. }

    41. void TIMER0_IRQHandler(void)
    42. {
    43.     static uint8_t status = 0;
    44.     if (SET == timer_interrupt_flag_get(TIMER0, TIMER_INT_FLAG_CH3))
    45.     {
    46.         status++;
    47.         switch (status % 3){
    48.         case 0:
    49.             printf("a\n");
    50.             timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, tim[B].idle);
    51.             timer_channel_output_pulse_value_config(TIMER15, TIMER_CH_0, 0);
    52.             break;
    53.         case 1:
    54.             printf("b\n");
    55.             timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, tim[B].t_off);
    56.             timer_channel_output_pulse_value_config(TIMER15, TIMER_CH_0, 0);
    57.             break;
    58.         case 2:
    59.             printf("c\n");
    60.             timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, tim[B].t_on);
    61.             timer_channel_output_pulse_value_config(TIMER15, TIMER_CH_0, pwm_ch[B].pulse);
    62.             break;
    63.         default:
    64.             break;
    65.         }
    66.         timer_interrupt_flag_clear(TIMER0, TIMER_INT_FLAG_CH3);
    67.     }
    68. }

12

您需要登录后才可以回复 登录 | 注册