未分类 · 2023年2月3日

GD32E230单片机TIMER0输出pwm例程

步骤
1.初始化时钟
2.初始化Io口
3.初始化定时器
4.初始化pwm输出通道

1、初始化时钟

void rcc_periph_clock_enable(void)
{
    //启动GPIOA时钟
    rcu_periph_clock_enable(RCU_GPIOA);
    //启动TUME0时钟
    rcu_periph_clock_enable(RCU_TIMER0);
}

2.初始化Io口

void GPIO_MOde_init(void)
{
    //设置引脚模式
    gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_8);
    gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_0);
    //设置输出频率
    gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_8);
    //设置初始电平状态
    gpio_bit_reset(GPIOA,GPIO_PIN_8);
    //设置引脚复用哪个功能,这里设置为PWM
    gpio_af_set(GPIOA,GPIO_AF_2, GPIO_PIN_8); 
}

3.初始化定时器

void Timer_initstructure(void)
{
    //定义定时器1结构体变量
    timer_parameter_struct timer0_initpara;
    timer_oc_parameter_struct timer_initpara;

    //设置定时器1为缺省值
    timer_deinit(TIMER0); 
    //注意!当你实用timer0高级定时器的时候必须使能这个,才能有pwm输出
    timer_primary_output_config(TIMER0,ENABLE);
    /* TIMER1 初始化配置 */
    /*
        prescaler: prescaler value of the counter clock, 0~65535
        alignedmode: TIMER_COUNTER_EDGE, TIMER_COUNTER_CENTER_DOWN, TIMER_COUNTER_CENTER_UP, TIMER_COUNTER_CENTER_BOTH
        counterdirection: TIMER_COUNTER_UP, TIMER_COUNTER_DOWN
        period: counter auto reload value, 0~65535
        clockdivision: TIMER_CKDIV_DIV1, TIMER_CKDIV_DIV2, TIMER_CKDIV_DIV4
        repetitioncounter: counter repetition value, 0~255
    */
    timer0_initpara.prescaler = BASIC_TIM_Prescaler;/*!< prescaler value */
    timer0_initpara.alignedmode = TIMER_COUNTER_EDGE;/*!< aligned mode */
    timer0_initpara.counterdirection = TIMER_COUNTER_UP;/*!< counter direction */
    timer0_initpara.clockdivision = TIMER_CKDIV_DIV1;/*!< clock division value */
    timer0_initpara.period = BASIC_TIM_Period;/*!< period value */
    timer0_initpara.repetitioncounter = 0;/*!< the counter repetition value */
    //初始化定时器
    timer_init(TIMER0,&timer0_initpara);

    /* CH1,CH2 and CH3 configuration in PWM mode1 */
    /*
        TIMER channeln output parameter struct
        outputstate: TIMER_CCX_ENABLE,TIMER_CCX_DISABLE
        outputnstate: TIMER_CCXN_ENABLE,TIMER_CCXN_DISABLE
        ocpolarity: TIMER_OC_POLARITY_HIGH,TIMER_OC_POLARITY_LOW
        ocnpolarity: TIMER_OCN_POLARITY_HIGH,TIMER_OCN_POLARITY_LOW
        ocidlestate: TIMER_OC_IDLE_STATE_LOW,TIMER_OC_IDLE_STATE_HIGH
        ocnidlestate: TIMER_OCN_IDLE_STATE_LOW,TIMER_OCN_IDLE_STATE_HIGH
    */
    //是否使能输出
    timer_initpara.outputstate  = TIMER_CCX_ENABLE;
    //是否互补通道输出
    timer_initpara.outputnstate = TIMER_CCXN_DISABLE;
    //输出极性
    timer_initpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    //输出死区延迟的极性
    timer_initpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
    //空闲状态下通道输出极性通道
    timer_initpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
    //空闲时互补通道输出极性
    timer_initpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
    //配置通道
    timer_channel_output_config(TIMER0,TIMER_CH_0,&timer_initpara);
    //*!!!!设置阈值(比较值,调节占空比的时候调节这个值)
    timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,00);
    //输出模式配置 
    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_auto_reload_shadow_enable(TIMER0);
    /* 使能定时器 */
    timer_enable(TIMER0);
}

4.初始化pwm输出通道