【匠芯创D133CBS】 PWM驱动蜂鸣器
本帖最后由 TL-LED 于 2024-8-29 20:25 编辑<p><span style="font-size:16px;">测试PWM方式驱动蜂鸣器。</span></p>
<p> </p>
<p><span style="font-size:16px;"><strong>一、硬件电路部分</strong></span></p>
<p> </p>
<p><span style="font-size:16px;">开发板使用的蜂鸣器是无源器件,需要外加脉冲信号驱动,常用的是PWM信号,此处连接到MCU的PWM1端口。</span></p>
<p><span style="font-size:16px;">1.1、蜂鸣器部分电路图</span></p>
<p><span style="font-size:16px;"></span></p>
<p> </p>
<p><span style="font-size:16px;">1.2、蜂鸣器参数</span></p>
<p><span style="font-size:16px;">从参数看,蜂鸣器的工作频率在2.7KHz</span></p>
<p><span style="font-size:16px;"></span></p>
<p> </p>
<p><strong><span style="font-size:16px;">二、程序部分</span></strong></p>
<p> </p>
<p><span style="font-size:16px;">2.1、beep.c</span></p>
<pre>
<code>//beep.c
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <sys/time.h>
#include <rtthread.h>
#include "rtdevice.h"
#include "aic_core.h"
#include "aic_hal_gpio.h"
#define PWM_DEV_NAME "pwm"
#define PWM_DEV_CHANNEL 1 /* PWM通道 */
struct rt_device_pwm *pwm_dev;
void beep_on(void)
{
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, 370000, 185000);
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
}
void beep_off(void)
{
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, 370000, 185000);
rt_pwm_disable(pwm_dev, PWM_DEV_CHANNEL);
}
void beep_change_pulse(uint32_t val)
{
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, 370000, val);
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
}
static void beep_hdl(void *parmeter)
{
static rt_err_t result;
while (1)
{
rt_thread_mdelay(1);
}
}
static void test_beep_sample(void)
{
rt_thread_t beep_thread;
/* 查找设备 */
pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
if (pwm_dev == RT_NULL)
{
rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
return RT_ERROR;
}
//创建线程
beep_thread = rt_thread_create(
"test_beep",
beep_hdl,
"pwm1",
2048,
RT_THREAD_PRIORITY_MAX/2,
25);
if(beep_thread != RT_NULL)
{
rt_thread_startup(beep_thread);
}
return RT_EOK;
rt_pwm_disable(pwm_dev, PWM_DEV_CHANNEL);
}
INIT_APP_EXPORT(test_beep_sample);
MSH_CMD_EXPORT(test_beep_sample, beep device sample);
</code></pre>
<p> </p>
<p>2.2、beep.h</p>
<pre>
<code>#ifndef __BEEP_H__
#define __BEEP_H__
void beep_off(void);
void beep_on(void);
void beep_change_pulse(uint32_t val);
#endif </code></pre>
<p> </p>
<p><span style="font-size:16px;"><strong>三、界面</strong></span></p>
<p> </p>
<p><span style="font-size:16px;">3.1、BEEP控制界面</span></p>
<p></p>
<p> </p>
<p><span style="font-size:16px;">3.2、界面文件</span></p>
<div><span style="font-size:16px;"></span></div>
<p> </p>
<p><span style="font-size:16px;"><strong>四、配置项目</strong></span></p>
<p><span style="font-size:16px;">4.1、使能PWM1</span></p>
<p></p>
<p> </p>
<p><span style="font-size:16px;"><strong>五、运行结果</strong></span></p>
<p> </p>
<p><span style="font-size:16px;">下载程序后,复位开发板运行</span></p>
<p><span style="font-size:16px;">5.1、测试PWM1波形</span></p>
<p></p>
<p> </p>
<p><span style="font-size:16px;">5.2、界面控制</span></p>
<p><span style="font-size: 16px;">点击蜂鸣器来控制开关,滑块来控制PWM占空比。</span></p>
<p><span style="font-size:16px;">4d935373cf6a5bc46d5814ccc5d23fbb</span><br />
</p>
页:
[1]