【匠芯创D133CBS】--4.PWM驱动蜂鸣器
<div class='showpostmsg'> 本帖最后由 dirty 于 2024-8-17 12:25 编辑<p> 本篇讲述PWM驱动板载蜂鸣器使用不同占空比鸣叫。</p>
<p> </p>
<p><strong><span style="color:#0000ff;">一.硬件原理</span></strong></p>
<p> 开发板蜂鸣器原理如下,这里使用到PWM1配置引脚,需4KHz PWM驱动。</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图1:蜂鸣器原理</div>
<p> </p>
<p><strong><span style="color:#0000ff;">二.代码准备</span></strong></p>
<p>1.命令scons --menuconfig配置使能PWM驱动选项,否则出现编译不过问题 </p>
<p>2.PWM初始化。这里配置为脉冲周期4KHz,x需将宏定义参数设定为250000,在驱动函数里会用到。</p>
<pre>
<code>#define LED_PIN "PD.0"
#define KEY_PIN "PD.15"
#define BUZZER_PIN "PE.11"
#define BUZZER_PWM_CH 1
#define BUZZER_FREQ_PARAM 250000
void app_data_init(void)
{
memset(&msg_control_t,0,sizeof(msg_control));
}
void app_key_init(void)
{
// 1.get pin number
static u32 pin = 0;
pin = rt_pin_get(KEY_PIN);
printf("Key pin%d\n",pin);
// 2.set pin mode to Input-PullUp
rt_pin_mode(pin, PIN_MODE_INPUT_PULLUP);
// 3.attach irq handler
rt_pin_attach_irq(pin, PIN_IRQ_MODE_RISING,key_irq_handler, &pin);//PIN_IRQ_MODE_RISING_FALLING
// 4.enable pin irq
rt_pin_irq_enable(pin, PIN_IRQ_ENABLE);
rt_kprintf("%s\r\n",__func__);
}
void app_led_init(void)
{
// 1.get pin number
static u32 pin = 0;
pin = rt_pin_get(LED_PIN);
printf("Led pin%d\n",pin);
// 2.set pin mode to Output
rt_pin_mode(pin, PIN_MODE_OUTPUT);
rt_kprintf("%s\r\n",__func__);
}
void app_pwm_init(void)
{
struct aic_pwm_action action0 = {
/* CBD, CBU, CAD, */
PWM_ACT_NONE, PWM_ACT_NONE, PWM_ACT_NONE,
/* CAU, PRD, ZRO*/
PWM_ACT_LOW,PWM_ACT_NONE, PWM_ACT_HIGH};
struct aic_pwm_action action1 = {
/* CBD, CBU, CAD, */
PWM_ACT_NONE, PWM_ACT_LOW, PWM_ACT_NONE,
/* CAU, PRD, ZRO*/
PWM_ACT_NONE, PWM_ACT_NONE,PWM_ACT_HIGH};
hal_pwm_init();
hal_pwm_ch_init(BUZZER_PWM_CH, PWM_MODE_UP_COUNT, 0, &action0, &action1);
hal_pwm_enable(BUZZER_PWM_CH);
rt_kprintf("%s\r\n",__func__);
//Freq:4KHz
// hal_pwm_set(BUZZER_PWM_CH, 0, BUZZER_FREQ_PARAM,PWM_SET_CMPA_CMPB);
}
void app_peripheral_init(void)
{
app_data_init();
app_key_init();
app_led_init();
app_pwm_init();
}
</code></pre>
<p>3.创建消息队列与任务,在按键中断回调里发送消息队列(如前一篇所述),每次控制蜂鸣器的PWM增加10%占空比。在接收消息队列任务里实现蜂鸣器PWM不同占空比控制驱动</p>
<pre>
<code>typedef struct
{
uint16_t msg_len;
uint8_t led_value;
uint8_t buzzer_pwm_percent;
}msg_control;
static void key_irq_handler(void *args)
{
unsigned int ret;
u32 pin = *((u32 *)(args));
printf("Key INT\r\n");
msg_control_t.msg_len=sizeof(msg_control_t);
msg_control_t.led_value=!msg_control_t.led_value;
msg_control_t.buzzer_pwm_percent+=10;
if(msg_control_t.buzzer_pwm_percent>100)
{
msg_control_t.buzzer_pwm_percent=0;
}
app_msg_send(&msg_control_t,sizeof(msg_control_t));
}
void app_thread_entry(void *parameter)
{
int pin;
msg_control msg_control_p;
while(1)
{
if(rt_mq_recv(&user_mq, &msg_control_p, sizeof(msg_control_p), RT_WAITING_FOREVER)==RT_EOK)
{
rt_kprintf("Receive msg quene\r\n");
rt_kprintf("msg_len:%d,led_value:%d,buzzer_pwm_percent:%d%%\r\n",\
msg_control_p.msg_len,msg_control_p.led_value,msg_control_p.buzzer_pwm_percent);
pin = rt_pin_get(LED_PIN);
rt_pin_write(pin, msg_control_p.led_value);
hal_pwm_set(BUZZER_PWM_CH, msg_control_p.buzzer_pwm_percent*BUZZER_FREQ_PARAM/100, BUZZER_FREQ_PARAM,PWM_SET_CMPA_CMPB);
}
}
}
</code></pre>
<p> </p>
<p><strong><span style="color:#0000ff;">三.测验</span></strong></p>
<p> 编译烧录,可以观察听到蜂鸣器不同鸣叫,在占空比0%和100%时,蜂鸣器是不鸣叫的,视频如下。串口日志如下</p>
<div style="text-align: center;"></div>
<div style="text-align: center;">图2:蜂鸣器控制串口日志</div>
<p> 至此,实现驱动蜂鸣器功能。</p>
<p> </p>
<p>e92300752b6c175eb16cbf2e7f31707e<br />
</p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p> 开发板蜂鸣器是有源的还是无源的 ?</p>
秦天qintian0303 发表于 2024-8-17 19:28
开发板蜂鸣器是有源的还是无源的 ?
<p>无源</p>
页:
[1]