打印
[研电赛技术支持]

GD32F103输出可控制数量 频率脉冲信号

[复制链接]
826|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
tpgf|  楼主 | 2025-2-6 13:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本来是想做AB相脉冲输出的,最后一步AB相滞后超前90°找不到办法了。只能输出两路脉冲 可控制频率 个数。

主函数调用set_psc即可。

#include "gd32f10x.h"                   // Device header
#include "Delay.h"

int pulse_count = 0;//脉冲计数值
int Pulse_num = 0;//设定脉冲数

void Encoder_init (void)
{
    rcu_periph_clock_enable(RCU_AF);            
    rcu_periph_clock_enable(RCU_GPIOB);
  rcu_periph_clock_enable(RCU_TIMER3);//时钟开启


    gpio_pin_remap_config(GPIO_TIMER2_PARTIAL_REMAP, ENABLE);                //重映射到TIM2
    gpio_pin_remap_config(GPIO_SWJ_SWDPENABLE_REMAP, ENABLE);
    gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_7);    //PB5端口配置
    gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6);    //PB4端口配置

    timer_parameter_struct timer3_init;
    timer_oc_parameter_struct timer3_ocintpara; //结构体初始化

    timer_deinit(TIMER3);
    /* TIMER2 configuration */
    timer3_init.prescaler         = 107;
    timer3_init.alignedmode       = TIMER_COUNTER_EDGE;
    timer3_init.counterdirection  = TIMER_COUNTER_UP;
    timer3_init.period            = 99;                            //10kHz
    timer3_init.clockdivision     = TIMER_CKDIV_DIV1;
    timer3_init.repetitioncounter = 0;
    timer_init(TIMER3,&timer3_init);

    /* CH2 configuration in PWM mode */
    timer3_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    //timer2_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
    timer3_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    //timer2_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    timer3_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
  //timer2_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW; //互补输出不需要

    timer_channel_output_config(TIMER3,TIMER_CH_1,&timer3_ocintpara); //
    timer_channel_output_config(TIMER3,TIMER_CH_0,&timer3_ocintpara); //

    timer_channel_output_pulse_value_config(TIMER3,TIMER_CH_0,50);
    timer_channel_output_mode_config(TIMER3,TIMER_CH_0,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER3,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
    /* CH3 configuration in PWM mode0,duty cycle 50% */
    timer_channel_output_pulse_value_config(TIMER3,TIMER_CH_1,50);
    timer_channel_output_mode_config(TIMER3,TIMER_CH_1,TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMER3,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);

    /*中断*/
    nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
    nvic_irq_enable(TIMER3_IRQn,0,0);

    /* auto-reload preload enable */
    timer_auto_reload_shadow_enable(TIMER3);
    /*enable timer2 interRupt*/
    timer_interrupt_enable(TIMER3, TIMER_INT_UP );
    /* enable TIMER3 */
    timer_enable(TIMER3);

}

/*设定脉冲频率 个数*/
void Set_PSC(int F_HZ , int num)
{
    Pulse_num = num;
    timer_parameter_struct timer3_init;
    long PSC = (1080000 - F_HZ) / F_HZ;
    timer3_init.prescaler         = PSC;
    timer3_init.alignedmode       = TIMER_COUNTER_EDGE;
    timer3_init.counterdirection  = TIMER_COUNTER_UP;
    timer3_init.period            = 99;                            //ARR
    timer3_init.clockdivision     = TIMER_CKDIV_DIV1;
    timer3_init.repetitioncounter = 0;
    timer_init(TIMER3,&timer3_init);

    timer_enable(TIMER3);
}


void TIMER3_IRQHandler(void)
{
if (timer_interrupt_flag_get(TIMER3, TIMER_INT_FLAG_UP)!= 0)
    {
        // 清除中断标志
        timer_interrupt_flag_clear(TIMER3, TIMER_INT_FLAG_UP);
                pulse_count++;
        if (pulse_count >= (Pulse_num))  
        {
            // 达到目标脉冲个数,停止定时器
          timer_disable(TIMER3);
                    pulse_count = 0;
        }
    }
}

————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/m0_74764636/article/details/145467952

使用特权

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

本版积分规则

2206

主题

16574

帖子

17

粉丝