当前位置:网站首页>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++; }
播放音乐
边栏推荐
- Redis4.0.14 sentinel automatic failover failed
- BigDecimel转人民币大写
- Wechat Reservation Reservation of applet completion works applet graduation project (8) graduation project thesis template
- 游戏外挂怎么做?
- QA robot sequencing model
- Graduation project of wechat small program ordering system of small program completion works (7) Interim inspection report
- 【万字长文】使用 LSM-Tree 思想基于.Net 6.0 C# 实现 KV 数据库(案例版)
- Eureka forced offline service
- TCGA simple download tool V16 installation error
- Redis learning
猜你喜欢
![[dark horse programmer] redis learning notes 001: introduction to redis + five basic data types](/img/f7/9f43cd964a671f3b53337999332280.png)
[dark horse programmer] redis learning notes 001: introduction to redis + five basic data types

Chapter 3 business function development (query clues)

C语言基础

【黑马程序员】Redis学习笔记002:持久化:RDB 和 AOF
![[dark horse programmer] redis learning notes 005: enterprise level solutions](/img/76/959d18994ab9eb170cbdbdccbe94e7.png)
[dark horse programmer] redis learning notes 005: enterprise level solutions

JS typewriter animation JS special effect plug-in autotyperjs

This week's big news | FCC exposed Pico 4 VR all-in-one machine, and leipeng's parent company established a smart glasses laboratory

Graduation design of wechat small program ordering system of small program completion works (5) assignment

Redis best practices

Review the second time, 220614, video, day03_ Data warehouse design,
随机推荐
[dark horse programmer] redis learning notes 005: enterprise level solutions
递归调用实现打印一个整数的每一位
JVM specification Oracle official website
The database of idea cannot prompt the table name, field name, and schema cannot be loaded
Online shopping E-commerce mall system based on jsp+servlet+mysql+
OpenGL es to achieve the effect of "big head, small head" and "head shaking"
JS cool rolling picture deformation animation JS special effects
A simple SQL injection shooting range exercise
[untitled]
【黑马程序员】Redis学习笔记003:Redis事务
CentOS 8.2 MySQL installation (xshell6)
A simple hotel background management system based on jsp+servlet+mysql
Wechat reservation applet graduation design of applet completion works (3) background function
Redis4.0.14 sentinel automatic failover failed
RTOS series (13): assembly LDR instruction, LDR pseudo instruction, [rn] register indirect reference detailed analysis
Redis分片集群
@Use of Autowired
Use of lambdaquerywrapper, lambdaupdatewrapper, lambdaquerychainwrapper
聊下自己转型测试开发的历程
TCGA simple download tool V16 installation error

