当前位置:网站首页>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 :

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 :





边栏推荐
- Wechat payment, wechat refund, Alibaba payment
- Common tools download address
- 2022 Taobao 618 Super Cat Games introduction 618 super cat games playing skills
- Mqtt protocol Chinese version
- AcWing 135. 最大子序和(前缀和 + 单调队列求定长区间最小值)
- Common methods of string class
- M-Arch(番外12)GD32L233评测-CAU加解密(捉弄下小编)
- file_ get_ Contents() JSON after reading_ Decode cannot be converted to array
- Class selectors and using pseudo class selectors with
- Pycharm view the current version of opencv
猜你喜欢

淺談調和形狀上下文特征HSC對3DSC的改進

Why check the @nonnull annotation at run time- Why @Nonnull annotation checked at runtime?

卡鱼刺别再喝醋吞米饭了!教你2招,让鱼刺安全“跑出来”

【实验】MySQL主从复制及读写分离

QT custom window fillets

浅谈调和形状上下文特征HSC对3DSC的改进

Properties Chinese garbled code

2022淘宝618超级喵运会玩法来了 超级喵运会有哪些攻略方法

How the ArrayList collection implements ascending and descending order

AcWing 131. 直方图中最大的矩形(单调栈经典运用 模板)
随机推荐
Remote desktop cannot copy and paste solution
pycharm 查看opencv当前的版本
Common regular expressions
MYSQL——内置函数
Golang start service background daemon
2022淘宝618超级喵运会玩法来了 超级喵运会有哪些攻略方法
Getting started with cloud API basics -- basic knowledge of picgo writing plug-ins
Malicious code analysis practice - use IDA pro to analyze lab05-01 dll
AcWing 41. 包含min函数的栈(单调栈)
JS scale down the width and height of the picture
机器学习不是你想用,想用就能用
AcWing 132. 小组队列(队列模拟题)
2022 Taobao 618 Super Cat Games introduction 618 super cat games playing skills
Pycharm view the current version of opencv
Why check the @nonnull annotation at run time- Why @Nonnull annotation checked at runtime?
Several solutions across domains
Class. Forname connection MySQL driver keeps throwing classnotfoundexception exception solution
QT custom window fillets
Chapter 3 search
Immer source code reading