当前位置:网站首页>51单片机外设篇:蜂鸣器
51单片机外设篇:蜂鸣器
2022-07-25 08:40:00 【路溪非溪】
蜂鸣器
蜂鸣器是一种将电信号转换为声音信号的器件,常用来产生设备的按键音、报警音等提示信号。
蜂鸣器按驱动方式可分为有源蜂鸣器和无源蜂鸣器:
有源蜂鸣器:内部自带振荡源,将正负极接上直流电压即可持续发声,频率固定;
无源蜂鸣器:内部不带振荡源,需要控制器提供振荡脉冲才可发声,调整提供振荡脉冲的频率,可发出不同频率的声音;
蜂鸣器的具体原理可以自行查阅资料,总之,要知道的是,蜂鸣器内部有两块金属片,当加上正负电极后,就会吸附在一起而产生碰撞,发出声音。当蜂鸣器的其中一个引脚电极固定,给另外一个引脚加上正负相间的电流时,就会产生一定频率的振动,从而发出不同的声音。
声音的音调可以由频率来控制,频率越高,声音越尖锐。
声音大小由硬件决定,无法写代码去控制声音大小。
查看原理图
这里的三极管是用来放大单片机引脚电流的,已达到能够驱动蜂鸣器的程度。
这里将J8接到P2.0接口,并提供一定频率的信号。
代码实现
需求:发出滴滴滴的声音,频率为500Hz,那么时钟周期为2ms,那么每隔1ms改变电平状态。
我先试了下,一直给高电平不知道行不行:P20 = 1;没听到声音。
频率为500Hz再试下:
/** *@file buzzer.c *@author Timi *@date 2022.07.23 */ #include <reg51.h> sbit P20 = P2^0; #define AIM_COUNTER(time) (65536 - (time) * 1000 / 1) //函数入口 void main(void) { TMOD = 0x01; TL0 = (AIM_COUNTER(50) % 256); TH0 = (AIM_COUNTER(50) / 256); TR0 = 1; ET0 = 1; EA = 1; while(1); } /** *@brief 让蜂鸣器响 *@param[in] *@param[out] *@return */ void EnableBuzzer() interrupt 1 using 1 { TL0 = (AIM_COUNTER(1) % 256); TH0 = (AIM_COUNTER(1) / 256); P20 = ~P20; }这种情况,蜂鸣器一直响。但是我想要的是滴滴滴的声音。怎么调整呢?
我的思路是,先响一段时间,然后停一段时间,之后再响一段时间,再停一段时间,如果循环往复。
代码雏形如下:
/** *@file buzzer.c *@author Timi *@date 2022.07.23 */ #include <reg51.h> #define uchar unsigned char #define AIM_COUNTER(time) (65536 - (time) * 1000 / 1) sbit P20 = P2^0; static uchar count = 0; void Delay500ms(void); //函数入口 void main(void) { TMOD = 0x01; TL0 = (AIM_COUNTER(50) % 256); TH0 = (AIM_COUNTER(50) / 256); TR0 = 1; ET0 = 1; EA = 1; while(1); } /** *@brief 让蜂鸣器响 *@param[in] *@param[out] *@return */ void EnableBuzzer(void) interrupt 1 using 1 { TL0 = (AIM_COUNTER(1) % 256); TH0 = (AIM_COUNTER(1) / 256); P20 = ~P20; //以上三段代码实现500Hz频率的振动 if(count++ == 100) //每振动100次之后暂定500ms { P20 = 0; Delay500ms(); count = 0; } } void Delay500ms(void) //误差 0us { uchar a,b,c; for(c = 205; c > 0; c--) for(b = 116; b > 0; b--) for(a = 9; a > 0; a--); }虽然能够实现功能,但是还是用了for循环Delay,暂停的时间不精准。
我是想要用定时器,但是,在定时器中使用定时器,不就嵌套循环了吗?比较复杂,且容易出错。所以,要怎么解决这个问题呢?
解决代码如下:
/** *@file buzzer.c *@author Timi *@date 2022.07.23 */ #include <reg51.h> #define u32 unsigned int #define AIM_COUNTER(time) (65536 - (time) * 1000 / 1) sbit P20 = P2^0; static u32 count = 1; //函数入口 void main(void) { TMOD = 0x01; TL0 = (AIM_COUNTER(50) % 256); TH0 = (AIM_COUNTER(50) / 256); TR0 = 1; ET0 = 1; EA = 1; while(1); } /** *@brief 让蜂鸣器响 *@param[in] *@param[out] *@return */ void EnableBuzzer(void) interrupt 1 using 1 { TL0 = (AIM_COUNTER(1) % 256); //500Hz的频率 TH0 = (AIM_COUNTER(1) / 256); if(count >= 1 && count <=250) { P20 = ~P20; } else if(count >= 251 && count <= 1000) //可以通过这两处的数字占比来控制响多久,停多久 { //什么也不做; } else { count = 0; } count++; }
播放音乐
边栏推荐
- C语言基础
- Swift initializer and optional chain
- Wechat reservation of the completed works of the applet graduation project (6) opening defense ppt
- Sun Tzu's art of war
- The fifth day of MATLAB learning (cycle type)
- 【万字长文】使用 LSM-Tree 思想基于.Net 6.0 C# 实现 KV 数据库(案例版)
- Redis best practices
- 【黑马程序员】Redis学习笔记005:企业级解决方案
- Wechat sports field reservation of the finished works of the applet graduation project (4) opening report
- JD cloud and Forrester consulting released a hybrid cloud report that cloud Nativity has become a new engine driving industrial development
猜你喜欢

英特尔就产品延误向Xe HPG寻宝游戏获奖者道歉并公布奖品样貌

JS typewriter animation JS special effect plug-in autotyperjs

NVIDIA programmable reasoning accelerator tensorrt learning notes (II) - practical operation

Graduation project of wechat small program ordering system of small program completion works (1) development outline

Intelligent operation and maintenance scenario analysis: how to detect abnormal business system status through exception detection

C语言基础

How can hospitals achieve efficient and low-cost operation and maintenance? Is there any software that can meet it?

Graduation project of wechat small program ordering system of small program completion works (4) opening report

Redis best practices

Wechat applet ordering system graduation design of applet completion works (8) graduation design thesis template
随机推荐
这是我见过写得最烂的Controller层代码...
[ten thousand words long text] Based on LSM tree thought Net 6.0 C # realize kV database (case version)
@Feignclient annotated interface. You may not get instances with @autowired
Talk about your transformation test development process
Basis 33: XPath acquisition methods of page elements under various browsers
Force buckle - 1046. Weight of the last stone
Graduation design of wechat small program ordering system of small program completion works (3) background function
Wechat reservation applet graduation project (7) mid term inspection report of applet completion works
Redis fragment cluster
Record the process of two multi terminal troubleshooting
@Autowired注解的原理
公寓报修系统(IDEA,SSM,MySQL)
25 Ph.D. degrees revoked
JS pop up picture Lightbox light box plug-in spotlight.js
为什么要使用MQ消息中间件?这几个问题必须拿下!
ip命令使用详解
The database of idea cannot prompt the table name, field name, and schema cannot be loaded
Wechat reservation applet graduation design of applet completion works (3) background function
@Use of Autowired
@Principle of Autowired annotation

