Various Applications of Buzzers in Microcontrollers

Publisher:ShimmeringStarLatest update time:2020-03-19 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In general microcontroller textbooks, there are simple instructions on how to use buzzers. Here we will explain in detail how to write buzzers for different buzzers and different chips.


However, when developing the project, I found that the use of the buzzer in the textbook had great limitations.


In the textbooks, the length of the buzzer is controlled by a busy loop such as delay. However, in reality, this function not only wastes a lot of CPU resources, but also delays the running of other functions. If there are some time-related functions, it will cause time confusion.


In addition, during the buzzer sounding, no operations other than interrupts can be performed. Strictly speaking, except for the infinite loop of the main function, any other busy loop that idles the CPU must be used with caution and the time cannot be too long. Generally speaking, busy loops at the millisecond level are already intolerable.


In addition, this writing method cannot flexibly configure advanced functions such as long or short beeps, and how many times the beeps occur at a certain interval.


However, after checking, I found that turning the buzzer on and off are two different processes.


Therefore, I implemented the buzzer on/off functions and other related functions.


The function of the buzzer on function is to turn on the buzzer, set the time (long beep, short beep, medium beep), and set the number of times.


The function of the buzzer off function is to query the setting value of the on function to decide when to turn off the buzzer, when to turn on the buzzer again, and when to stop turning on the buzzer.


Local global variables control the number of times, and timer global variables control the time.


From the above description, it can be determined that the opening function can be used at will, while the closing function must be looped in the main function to query the setting value.


The following is an example of a function.


1. Necessary initialization


///////////////////Buzzer duration variable declaration//////////////////////

typedef enum //Open time

{

  BEEP_SHORT = SEC_01, //100ms

  BEEP_MIDDLE = SEC_03, //300ms

  BEEP_LONG = SEC_04 //400ms

} BEEP_LAST;

typedef enum //Close interval

{

BEEP_FAST = SEC_03,

BEEP_SLOW = SEC_05

} BEEP_SHUT;


static uint8_t tmp_beep_cnt; //Buzzer count temporary storage

static BEEP_LAST tmp_beep_on; //Buzzer turns on temporary storage

static BEEP_SHUT tmp_beep_period; //temporary storage of buzzer period


2. Buzzer on

/****************************************************************************/

/* Function: buzzer sounds

 * describe:

 * Parameters: duration of opening, duration of closing (one ring is optional), number of rings (>0)

 * return:

 * Note: The number cannot be written as 0 */

/****************************************************************************/

void beep_on(BEEP_LAST last, BEEP_SHUT off, uint8_t times)

{

BEEP_CSR |=1<<5; //Turn on the buzzer

freq.beep_on = last; //beep continues

freq.beep_period = last + off; //One on and off cycle

 

tmp_beep_on = last;

tmp_beep_period = last + off;

if(times > 0)

tmp_beep_cnt = times - 1;

else

tmp_beep_cnt = 0;

}


3. Buzzer off

/****************************************************************************/

/* Function: turn off the buzzer

 * Description: Detection time flag, turn it off when it is 0, and reduce the number by 1

 * Parameters:

 * return:

 * Note: This is inside the main loop */

/****************************************************************************/

void beep_off(void)

{

if (freq.beep_on == 0) //If the duration is up, turn off

{

BEEP_CSR &=~(1<<5); //Close

if (tmp_beep_cnt) //The buzzer is turned off, but the count is still there, wait for the conditions to be met to turn it back on

{

if (freq.beep_period == 0) //The buzzer period has expired

{

BEEP_CSR |=1<<5; //Turn on the buzzer

freq.beep_on = tmp_beep_on; //Assign the last duration

freq.beep_off = tmp_beep_period; //Re-count the buzzer period

s_beep_cnt--; //Number of times minus 1

}

}

}

}


Fourth, timer processing, interrupt once every 10ms.


/*Buzzer related timing*/

if (freq.beep_on)

freq.beep_on--; // duration of buzzer on

if(freq.beep_period)

freq.beep_period--; //Buzzer period

Reference address:Various Applications of Buzzers in Microcontrollers

Previous article:51 single chip microcomputer - play music with buzzer
Next article:51 single chip microcomputer buzzer "East and West"

Recommended ReadingLatest update time:2024-11-16 12:03

Microcontroller buzzer control program and drive circuit
Buzzers are divided into piezoelectric buzzers and electromagnetic buzzers based on their structure. Piezoelectric buzzers use piezoelectric ceramics to make sounds, and the current is relatively small, while electromagnetic buzzers use coils to vibrate and make sounds, and are relatively small in size. According to t
[Microcontroller]
Microcontroller buzzer control program and drive circuit
AVR microcontroller (ATMEGA16) controls the buzzer
#include iom16v.h    #include macros.h    #define uchar unsigned char   #define uint unsigned int   #define DELAY 500   void delay(uint z)    //1ms   {    uint x,y;    for(x=z;x 0;x--)     for(y=157;y 0;y--);   }   void main(void)   {    DDRA |=BIT(0);    while(1)    {     PORTA &=~BIT(0);     delay(
[Microcontroller]
AVR microcontroller (ATMEGA16) controls the buzzer
HT47 Buzzer Application
HT47 Buzzer Application include ht47c20.inc data .section 'data' count1 db ? count2 db ? ;**************************** code .section at 0 'code'  org 00h  jmp start ;****************************  org 20h start: clr i NTC 0  clr intc1 loop: clr pa.1  clr pa.0  call delay  set pa.1  call delay  set pa.0  call delay  j
[Microcontroller]
The serial port communication microcontroller controls the buzzer to sound according to the digital data sent by the host computer.
1. Program Function When the host computer sends 1 to the microcontroller, the buzzer sounds at a frequency of 400ms. When 2 is sent, it sounds at a frequency of 200ms. When 3 is sent, it sounds at a frequency of 100ms. When 4 is sent, the buzzer is turned off.   2. Program source code #include reg52.h   /
[Microcontroller]
How to Design a Low-Cost Buzzer
  In actual applications, although active buzzers are easy to control, their disadvantages are that they are relatively expensive and are easily damaged after being used for a long time in a humid environment. Passive buzzers make up for the shortcomings of active buzzers, but the problem is that passive buzzers requi
[Power Management]
How to Design a Low-Cost Buzzer
51 single chip microcomputer program - timer control buzzer
#include #define uint unsigned int    sbit bb=P3^5;                   //bit definition buzzer interface sbit led1=P0^0;       //bit definition LED interface unsigned int i=0;   void delay(uint z);       //delay function declaration   void main() { TMOD=0x01;                 //TMOD assignment t
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号