当前位置:网站首页>M-arch (fanwai 11) gd32l233 evaluation PWM driven active buzzer

M-arch (fanwai 11) gd32l233 evaluation PWM driven active buzzer

2022-06-12 10:38:00 Interesting Python

Preface

Good architecture , High yield like sows .

Interface modification , The effect comes out .

On the second day of commencement , It takes half an hour to finish pwm Drive active buzzer .

Active buzzer

Active buzzer , It contains an oscillation source inside , It can sound when powered on ;

For active buzzers , It can be done by PWM The high and low level of controls the beeping frequency of the buzzer , High level response , Low level does not sound .

As for why PWM Port to drive , It is designed to be compatible with passive buzzers .

I have an active buzzer on the Internet , Long like this :

47c763aba23ef65fb59e4b2ca696e897.png
SFM-27-I

Driver code

The basic principle : Through adjustment PWM To control the duration of the buzzer .

GD32L233 The development board of does not put all IO The mouth leads out , Corresponding to my design , Only PB5 You can use .

Timer initialization :

void timer3_init(void)
{
    timer_deinit(TIMER2);
    rcu_periph_clock_enable(RCU_TIMER2);
#ifdef DAC_WAVE_TEST
    timerx_init(TIMER2, 639, 9);  // 100KHz 0.1ms
#endif
#ifdef BUZZER_TEST
    timerx_init(TIMER2, 15999, 1999);  // 4KHz 500ms
#endif
    timer_interrupt_enable(TIMER2, TIMER_INT_UP);
    nvic_irq_enable(TIMER2_IRQn, 3);
}

PWM initialization :

void timer3_pwm_init(void)
{
#ifdef BUZZER_TEST
    timerx_PWM_init(TIMER2, TIMER_CH_1, RCU_GPIOB, GPIOB, GPIO_PIN_5, 0);
#endif
}

PWM Change the duty cycle and cycle interface :

void timer3_pwm_change(uint16_t period, uint16_t pulse)
{
    timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, pulse);
    timer_autoreload_value_config(TIMER2, period);
    timer_event_software_generate(TIMER2, TIMER_EVENT_SRC_UPG);
}

Front Lane DS1302 Ate AF The loss of , This time I went to the manual to check AF Value , I'm out of the hole ,-_-

GD32L233 Of PWM There is no complementary function , Few parameters , Initialization is relatively simple , The internal interface of the package can be used after modification :

/******************************************************************************************/
/******************************************************************************************/
/******************************************************************************************/
/******************************************************************************************/
static void timerx_init(uint32_t TIMx, uint16_t prescaler, uint16_t period)
{
    timerx_init_full(TIMx, prescaler, period, TIMER_CKDIV_DIV1, TIMER_COUNTER_UP, 0);
}

static void timerx_init_full(uint32_t TIMx, uint16_t prescaler, uint16_t period, uint16_t clock_div, uint16_t counter_mode, uint8_t repetition_counter)
{
    timer_parameter_struct timer_initpara;
    timer_initpara.prescaler         = prescaler;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = counter_mode;
    timer_initpara.period            = period;
    timer_initpara.clockdivision     = clock_div;
    timer_init(TIMx, &timer_initpara);
    
    timer_update_event_enable(TIMx);
    timer_enable(TIMx);
}

static uint32_t get_alt_func_num(uint32_t TIMx)
{
    switch (TIMx)
    {
        case TIMER1:
        case TIMER2:
            return GPIO_AF_1;
        case LPTIMER:
        case TIMER8:
        case TIMER11:
            return GPIO_AF_2;
        default:
            return GPIO_AF_2;
    }
    return 0;
}

static void timerx_PWM_init(uint32_t TIMx, uint16_t TIMCHx, rcu_periph_enum rcu, uint32_t gpio, uint16_t pin, uint16_t pulse)
{
    timer_oc_parameter_struct timer_ocintpara;

    gpio_init_af_mode(rcu, gpio, pin, GPIO_OSPEED_2MHZ, get_alt_func_num(TIMx));

    timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
    timer_channel_output_config(TIMx, TIMCHx, &timer_ocintpara);
    timer_channel_output_pulse_value_config(TIMx, TIMCHx, pulse);
    timer_channel_output_mode_config(TIMx, TIMCHx, TIMER_OC_MODE_PWM0);
    timer_channel_output_shadow_config(TIMx, TIMCHx, TIMER_OC_SHADOW_DISABLE);

    timer_auto_reload_shadow_enable(TIMx);
    timer_enable(TIMx);
}

Test code :

static void buzzer_test(void)
{
#ifdef BUZZER_TEST
    #define BUZZER_500ms    1999
    #define BUZZER_1000ms   3999
    #define BUZZER_2000ms   7999
    static uint8_t index = 0;
    index++;
    if (index == 2)
    {
        timer3_pwm_change(BUZZER_500ms, BUZZER_500ms/2);
    }
    else if (index == 4)
    {
        timer3_pwm_change(BUZZER_1000ms, BUZZER_1000ms/2);
    }
    else if (index == 6)
    {
        timer3_pwm_change(BUZZER_2000ms, BUZZER_2000ms/2);
    }
    else if (index == 8)
    {
        index = 0;
    }
#endif
}

test result

PWM wave form :

44aa7c69cc3c5a4fcce7757bf27cd441.png
PWM wave form
fa895070612c0a2b4485d5f705618372.gif8938f78c68c03db5e4d3802104fb3c90.gif35e1f06e93714052a8e0c016fb9f56ed.pngc4b144391411c88e20cf523a467241b7.gif
原网站

版权声明
本文为[Interesting Python]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121032527369.html