打印
[研电赛技术支持]

GD32F303RCT6 请问有定时器互联,PWM输出的案例吗 ?

[复制链接]
594|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
请问各位大神,F303有定时器互联输出PWM的相关文章资源吗?

第一次使用使用这个功能,代码无法输出第二路和第三路PWM




/*
   电磁阀,串气阀 gpio初始化
*/
void timer7_timer4_timer1_gpio_init(void){
   
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_periph_clock_enable(RCU_GPIOC);  
   
    gpio_init(GPIOA, GPIO_MODE_AF_PP,GPIO_OSPEED_2MHZ,Timer1_ch1);
    gpio_init(GPIOA, GPIO_MODE_AF_PP,GPIO_OSPEED_2MHZ,Timer4_ch2);
    gpio_init(GPIOC, GPIO_MODE_AF_PP,GPIO_OSPEED_2MHZ,Timer7_ch0);
   
}


/*
  串气阀, 电磁阀1,电磁阀2初始化
  
  串气阀频率:


*/
void timer7_timer4_timer1_config(void){
   
    timer7_timer4_timer1_gpio_init();
   
    timer_oc_parameter_struct timer_ocinitpara;   //通道输出结构体

    timer_parameter_struct timer_initpara;   
   
    rcu_periph_clock_enable(RCU_TIMER1);

    rcu_periph_clock_enable(RCU_TIMER4);

    rcu_periph_clock_enable(RCU_TIMER7);   

   
    /**************    Timer7      ****************/
    timer_deinit(TIMER7);
    timer_struct_para_init(&timer_initpara);
   
    timer_initpara.prescaler= 60000-1;
    timer_initpara.alignedmode=TIMER_COUNTER_EDGE;
    timer_initpara.clockdivision=TIMER_CKDIV_DIV1;
    timer_initpara.counterdirection=TIMER_COUNTER_UP;
    timer_initpara.period = (2000*6.7-1);
    timer_initpara.repetitioncounter=0;
   
    timer_init(TIMER7,&timer_initpara);
   
    // 初始化外设TIMER通道输出结构体参数
    timer_channel_output_struct_para_init(&timer_ocinitpara);
   
    timer_ocinitpara.ocidlestate=TIMER_OC_IDLE_STATE_LOW;  //空闲状态下为低电平
    timer_ocinitpara.ocnidlestate=TIMER_OCN_IDLE_STATE_LOW;//互补通道空闲状态下为低
    timer_ocinitpara.ocpolarity=TIMER_OC_POLARITY_HIGH;    //通道输出极性
    timer_ocinitpara.ocnpolarity=TIMER_OCN_POLARITY_HIGH;  //互补通道输出极性
    timer_ocinitpara.outputnstate=TIMER_CCXN_DISABLE;      //互补通道输出状态
    timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;       //通道输出状态
   
    //外设TIMERx的通道输出配置
    timer_channel_output_config(TIMER7, TIMER_CH_0, &timer_ocinitpara);
    //配置外设TIMERx的通道输出比较值
    timer_channel_output_pulse_value_config(TIMER7, TIMER_CH_0, 2000*2);
    // 配置外设TIMERx通道输出比较模式
    timer_channel_output_mode_config(TIMER7, TIMER_CH_0, TIMER_OC_MODE_PWM0);
    //配置TIMERx通道输出比较影子寄存器功能
    timer_channel_output_shadow_config(TIMER7, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
    // TIMERx自动重载影子使能
    timer_auto_reload_shadow_enable(TIMER7);
    // TIMERx主从模式配置
    timer_master_slave_mode_config(TIMER7, TIMER_MASTER_SLAVE_MODE_ENABLE);
    // 选择TIMERx主模式输出触发
    timer_master_output_trigger_source_select(TIMER7, TIMER_TRI_OUT_SRC_UPDATE);
    // 所有的通道输出使能
    timer_primary_output_config(TIMER7, ENABLE);
//timer_auto_reload_shadow_enable(TIMER7);
   
   
   
    /**************    Timer1      ****************/
   
    timer_deinit(TIMER1);
   
    timer_struct_para_init(&timer_initpara);
   
    timer_initpara.prescaler         = 60000-1;

    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;

    timer_initpara.counterdirection  = TIMER_COUNTER_UP;

    timer_initpara.period            = 13300;

    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;

    timer_initpara.repetitioncounter = 0;

    timer_init(TIMER1, &timer_initpara);

    timer_channel_output_struct_para_init(&timer_ocinitpara);
   
    timer_ocinitpara.ocidlestate=TIMER_OC_IDLE_STATE_LOW;  //空闲状态下为低电平
    timer_ocinitpara.ocnidlestate=TIMER_OCN_IDLE_STATE_LOW;//互补通道空闲状态下为低
    timer_ocinitpara.ocpolarity=TIMER_OC_POLARITY_HIGH;    //通道输出极性
    timer_ocinitpara.ocnpolarity=TIMER_OCN_POLARITY_HIGH;  //互补通道输出极性
    timer_ocinitpara.outputnstate=TIMER_CCXN_DISABLE;      //互补通道输出状态
    timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;       //通道输出状态

    timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocinitpara);

    timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, 7000);

    timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM0);

    timer_channel_output_shadow_config(TIMER1, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);

    timer_auto_reload_shadow_enable(TIMER1);

    /* 从模式选择:外部时钟模式0 */

    timer_slave_mode_select(TIMER1, TIMER_SLAVE_MODE_EXTERNAL0);

    /* 选择定时器输入触发源:内部触发2(ITI2) */

    timer_input_trigger_source_select(TIMER1, TIMER_SMCFG_TRGSEL_ITI2);

    /* 选择主从模式 */

    timer_master_slave_mode_config(TIMER1, TIMER_MASTER_SLAVE_MODE_ENABLE);

    /* 使用TIMER2更新事件作为触发器输出 */

    timer_master_output_trigger_source_select(TIMER1, TIMER_TRI_OUT_SRC_UPDATE);
   
   
     /**************    Timer4     ****************/
     
    timer_deinit(TIMER4);

    timer_struct_para_init(&timer_initpara);

    timer_initpara.prescaler         = 0;

    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;

    timer_initpara.counterdirection  = TIMER_COUNTER_UP;

    timer_initpara.period            = 3;

    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;

    timer_initpara.repetitioncounter = 0;

    timer_init(TIMER4, &timer_initpara);

    timer_channel_output_struct_para_init(&timer_ocinitpara);

   

    timer_ocinitpara.ocidlestate=TIMER_OC_IDLE_STATE_LOW;  //空闲状态下为低电平
    timer_ocinitpara.ocnidlestate=TIMER_OCN_IDLE_STATE_LOW;//互补通道空闲状态下为低
    timer_ocinitpara.ocpolarity=TIMER_OC_POLARITY_HIGH;    //通道输出极性
    timer_ocinitpara.ocnpolarity=TIMER_OCN_POLARITY_HIGH;  //互补通道输出极性
    timer_ocinitpara.outputnstate=TIMER_CCXN_DISABLE;      //互补通道输出状态
    timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;       //通道输出状态

    timer_channel_output_config(TIMER4, TIMER_CH_2, &timer_ocinitpara);

    timer_channel_output_pulse_value_config(TIMER4, TIMER_CH_2, 2);

    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);

    timer_auto_reload_shadow_enable(TIMER4);

    timer_primary_output_config(TIMER4, ENABLE);

    /* 从模式选择:外部时钟模式0 */

    timer_slave_mode_select(TIMER4, TIMER_SLAVE_MODE_EXTERNAL0);

    /* 选择定时器输入触发源:内部触发2(ITI2) */

    timer_input_trigger_source_select(TIMER4, TIMER_SMCFG_TRGSEL_ITI2);

    /* TIMER 使能 */

    timer_enable(TIMER7);

    timer_enable(TIMER2);

    timer_enable(TIMER4);   

使用特权

评论回复
沙发
duzhiqiang521| | 2022-10-17 17:42 | 只看该作者
你这具体是要哪个定时器触发哪个定时器,是否要求同步? 我看这上面初始化的 1 4 7 定时器,还有触发信号一定要选对,

定时器互联.png (293.53 KB )

定时器互联.png

使用特权

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

本版积分规则

3

主题

3

帖子

0

粉丝