当前位置:网站首页>51 MCU peripherals: buzzer
51 MCU peripherals: buzzer
2022-07-25 08:41:00 【Luxi Feixi】
Buzzer
A buzzer is a device that converts an electrical signal into a sound signal , It is often used to generate the key tone of the device 、 Alarm tone and other prompt signals .
Buzzer can be divided into active buzzer and passive buzzer according to driving mode :
Active buzzer : Internal oscillation source , Connect the positive and negative poles to the DC voltage, that is, the continuous sound , Fixed frequency ;
Passive buzzer : No internal oscillation source , The oscillation controller is required to provide an oscillation pulse , Adjust the frequency of the oscillation pulse , It can make sounds of different frequencies ;
The specific principle of buzzer can be consulted by yourself , All in all , What you need to know is , There are two pieces of metal inside the buzzer , When positive and negative electrodes are added , Will adsorb together and produce collision , Make a sound . When one of the buzzer pin electrodes is fixed , When the current between positive and negative phases is applied to the other pin , Will produce a certain frequency of vibration , To make different sounds .
The tone of sound can be controlled by frequency , The higher the frequency is. , The louder the voice .
The sound size is determined by the hardware , Unable to write code to control the sound size .
Look at the schematic
The triode here is used to amplify the pin current of MCU , It has reached the level of being able to drive the buzzer .
There will be J8 Receive P2.0 Interface , And provide a certain frequency signal .
Code implementation
demand : Make a dripping sound , The frequency is 500Hz, Then the clock cycle is 2ms, So every 1ms Change the level state .
I tried it first , I don't know if it's OK to keep high level :P20 = 1; No sound was heard .
The frequency is 500Hz Try again :
/** *@file buzzer.c *@author Timi *@date 2022.07.23 */ #include <reg51.h> sbit P20 = P2^0; #define AIM_COUNTER(time) (65536 - (time) * 1000 / 1) // Function entrance void main(void) { TMOD = 0x01; TL0 = (AIM_COUNTER(50) % 256); TH0 = (AIM_COUNTER(50) / 256); TR0 = 1; ET0 = 1; EA = 1; while(1); } /** *@brief Let the buzzer ring *@param[in] *@param[out] *@return */ void EnableBuzzer() interrupt 1 using 1 { TL0 = (AIM_COUNTER(1) % 256); TH0 = (AIM_COUNTER(1) / 256); P20 = ~P20; }This situation , The buzzer keeps ringing . But what I want is Didi's voice . How to adjust it ?
My idea is , Ring for a while , Then stop for a while , Then ring for a while , Stop for a while , If it goes back and forth .
The code prototype is as follows :
/** *@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); // Function entrance void main(void) { TMOD = 0x01; TL0 = (AIM_COUNTER(50) % 256); TH0 = (AIM_COUNTER(50) / 256); TR0 = 1; ET0 = 1; EA = 1; while(1); } /** *@brief Let the buzzer ring *@param[in] *@param[out] *@return */ void EnableBuzzer(void) interrupt 1 using 1 { TL0 = (AIM_COUNTER(1) % 256); TH0 = (AIM_COUNTER(1) / 256); P20 = ~P20; // The above three codes realize 500Hz Vibration of frequency if(count++ == 100) // Per vibration 100 Tentatively after times 500ms { P20 = 0; Delay500ms(); count = 0; } } void Delay500ms(void) // error 0us { uchar a,b,c; for(c = 205; c > 0; c--) for(b = 116; b > 0; b--) for(a = 9; a > 0; a--); }Although it can realize the function , But I still used for loop Delay, The pause time is not accurate .
I want to use a timer , however , Use timer in timer , Don't you just nest loops ? More complicated , And easy to make mistakes . therefore , How to solve this problem ?
The solution code is as follows :
/** *@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; // Function entrance void main(void) { TMOD = 0x01; TL0 = (AIM_COUNTER(50) % 256); TH0 = (AIM_COUNTER(50) / 256); TR0 = 1; ET0 = 1; EA = 1; while(1); } /** *@brief Let the buzzer ring *@param[in] *@param[out] *@return */ void EnableBuzzer(void) interrupt 1 using 1 { TL0 = (AIM_COUNTER(1) % 256); //500Hz The frequency of TH0 = (AIM_COUNTER(1) / 256); if(count >= 1 && count <=250) { P20 = ~P20; } else if(count >= 251 && count <= 1000) // You can control how long it rings by the ratio of the numbers in these two places , How long does it stop { // Do nothing ; } else { count = 0; } count++; }
Play music
边栏推荐
- Does the server operation and maintenance need to be online 24 hours? Do you need to work overtime on weekends?
- @The difference and use of value and configurationproperties
- Redis fragment cluster
- Implementation of depth first and breadth first traversal of binary tree
- C语言基础
- Talk about your transformation test development process
- Intelligent operation and maintenance scenario analysis: how to detect abnormal business system status through exception detection
- Redis learning
- @Feignclient annotated interface. You may not get instances with @autowired
- Pet adoption system based on ssm+mysql+layui
猜你喜欢
![[Sesame Street family] & Bert Bart Roberta](/img/ff/c685065cd413bd4cffd996fd9afeaa.png)
[Sesame Street family] & Bert Bart Roberta

Talk about your transformation test development process
![[dark horse programmer] redis learning notes 005: enterprise level solutions](/img/76/959d18994ab9eb170cbdbdccbe94e7.png)
[dark horse programmer] redis learning notes 005: enterprise level solutions

Mongodb database

serialization and deserialization

How to set up a personal website for free

Swift初始化器及可选链

SSM+JSP+MYSQL实现的宠物领养收养管理系统源码

Online bookstore system based on jsp+servlet+mysql

25位撤销博士学位
随机推荐
Pet adoption system based on ssm+mysql+layui
@Differences between requestparam, @pathparam, @pathvariable and other annotations (use of some annotations)
Swift初始化器及可选链
Sun Tzu's art of war
Source code of pet adoption management system implemented by ssm+jsp+mysql
Wechat applet ordering system graduation design of applet completion works (2) applet function
Memcached data cache database (improve efficiency)
unity客户端读取文本配置
C#入门系列(三十) -- 异常处理
【黑马程序员】Redis学习笔记002:持久化:RDB 和 AOF
Graduation project of wechat small program ordering system of small program completion works (4) opening report
孙子兵法随感
Foundation 31: Selenium positioning dynamic ID element
Idea starts the project slowly
[shader realizes shadow projection effect _shader effect Chapter 8]
Graduation design of wechat small program ordering system of small program completion works (5) assignment
Review the second time, 220614, video, day03_ Data warehouse design,
提高代码可续性的小技巧,以connectTo方法为例。
Does the server operation and maintenance need to be online 24 hours? Do you need to work overtime on weekends?
Wechat reservation applet graduation design of applet completion works (1) development outline

