Microcontroller buzzer control program and drive circuit

Publisher:RadiantDreamsLatest update time:2016-12-24 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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. Electromagnetic buzzers use coils to vibrate and make sounds, and their size is relatively small.

According to the driving method, they are divided into active buzzers and passive buzzers. The active and passive here do not refer to power supplies, but to oscillation sources. Active buzzers have oscillation sources inside. As shown in Figure 9-8, when a low level is given to the BUZZ pin, the buzzer will ring directly. Passive buzzers do not have oscillation sources inside. To make them ring, they must be driven by a pulse frequency signal between 500Hz and 4.5KHz. Active buzzers are often more expensive than passive buzzers because they have an oscillation circuit inside and are simple to drive and produce sound. They can be driven by electrical level, while passive buzzers are relatively cheap. In addition, the sound frequency of passive buzzers can be controlled, and there is a definite correspondence between the scale and the frequency. Therefore, the effect of "do re mi fa sol la si" can be produced. It can be used to make simple music tracks, such as the birthday song, two tigers, etc.

Figure 9-8 Buzzer circuit schematic
Figure 9-8 Buzzer circuit schematic


Let's take a look at the circuit in Figure 9-8. The buzzer current is still relatively large, so it needs to be driven by a transistor, and a 100-ohm resistor is added as a current-limiting resistor. In addition, a D4 diode is added, which is called a freewheeling diode. Our buzzer is an inductive device. When the transistor is turned on to power the buzzer, a conduction current will flow through the buzzer. As we know, one of the characteristics of an inductor is that the current cannot change suddenly. When it is turned on, the current gradually increases, which is no problem. But when it is turned off, the circuit of "power supply-transistor-buzzer-ground" is cut off, and no current can pass. So where does the stored current go? It is consumed through this D4 and the buzzer's own loop, thereby avoiding the reverse impact caused by the inductive current when it is turned off. The current when it is turned off continues, which is the origin of the name of the freewheeling diode.

Buzzers are often used as prompt sounds on devices such as computers, printers, and multimeters. The prompt sounds are generally very simple, just making a sound. We used a program to make a sound at a frequency of 4KHZ and 1KHZ. Students can study the program by themselves and compare the actual effects.



#include

sbit BUZZ = P1^6; //Buzzer control pin

unsigned char T0RH = 0; //T0 high byte of reload value

unsigned char T0RL = 0; //Low byte of T0 reload value

void OpenBuzz(unsigned int frequ);

void StopBuzz();

void main(){

    unsigned int i;

    TMOD = 0x01; //Configure T0 to work in mode 1, but do not start it yet

    EA = 1;

    while (1){ //Enable global interrupt

        OpenBuzz(4000); //Start the buzzer at 4KHz frequency

        for (i=0; i<40000; i++);

        StopBuzz(); //Stop the buzzer

        for (i=0; i<40000; i++);

        OpenBuzz(1000); //Start the buzzer at 1KHz frequency

        for (i=0; i<40000; i++);

        StopBuzz(); //Stop the buzzer

        for (i=0; i<40000; i++);

    }

}

/* Buzzer start function, frequ-working frequency*/

void OpenBuzz(unsigned int frequ){

    unsigned int reload; //Calculate the required timer reload value

    reload = 65536 - (11059200/12)/(frequ*2); //Calculate the timer reload value based on the given frequency

    T0RH = (unsigned char)(reload >> 8); //16-bit reload value is decomposed into high and low bytes

    T0RL = (unsigned char)reload;

    TH0 = 0xFF; //Set an initial value close to overflow so that the timer can start working immediately

    TL0 = 0xFE;

    ET0 = 1; // Enable T0 interrupt

    TR0 = 1; //Start T0

}

/* Buzzer stop function*/

void StopBuzz(){

    ET0 = 0; //Disable T0 interrupt

    TR0 = 0; //Stop T0

}

/* T0 interrupt service function, used to control the buzzer sound*/

void InterruptTimer0() interrupt 1{

    TH0 = T0RH; //Reload the reload value

    TL0 = T0RL;

    BUZZ = ~BUZZ; //Invert buzzer control level

}

In addition, using a buzzer to output music is just for fun, and has few applications. It contains relevant content about scales and music scores, and the program is a bit complicated, so I won't explain it in detail. I only provide a program that can play "Two Tigers". You can download it to the board to play and satisfy your curiosity.

Plain text copy

#include

sbit BUZZ = P1^6; //Buzzer control pin

unsigned int code NoteFrequ[] = { // List of corresponding frequencies for middle notes 1-7 and treble notes 1-7

    523, 587, 659, 698, 784, 880, 988, // Alto 1-7

    1047, 1175, 1319, 1397, 1568, 1760, 1976 // Treble 1-7

};

unsigned int code NoteReload[] = { //Timer reload values ​​corresponding to middle notes 1-7 and treble notes 1-7

    65536 - (11059200/12) / (523*2), // Middle tone 1

    65536 - (11059200/12) / (587*2), //2

    65536 - (11059200/12) / (659*2), //3

    65536 - (11059200/12) / (698*2), //4

    65536 - (11059200/12) / (784*2), //5

    65536 - (11059200/12) / (880*2), //6

    65536 - (11059200/12) / (988*2), //7

    65536 - (11059200/12) / (1047*2), //treble 1

    65536 - (11059200/12) / (1175*2), //2

    65536 - (11059200/12) / (1319*2), //3

    65536 - (11059200/12) / (1397*2), //4

    65536 - (11059200/12) / (1568*2), //5

    65536 - (11059200/12) / (1760*2), //6

    65536 - (11059200/12) / (1976*2), //7

};

bit enable = 1; //Buzzer sound enable flag

bit tmrflag = 0; //Timer interrupt completion flag

unsigned char T0RH = 0xFF; //T0 high byte of reload value

unsigned char T0RL = 0x00; //Low byte of T0 reload value

void PlayTwoTiger();

void main(){

    unsigned int i;

    EA = 1; // Enable global interrupt

    TMOD = 0x01; //Configure T0 to work in mode 1

    TH0 = T0RH;

    TL0 = T0RL;

    ET0 = 1; // Enable T0 interrupt

    TR0 = 1; //Start T0

   

    while (1){

        PlayTwoTiger(); //Play the song - Two Tigers

        for (i=0; i<40000; i++); //Stop for a while

    }

}

/* Two tiger music playing functions*/

void PlayTwoTiger(){

    unsigned char beat; //Current beat index

    unsigned char note; //The note corresponding to the current beat

    unsigned int time = 0; //Current beat timing

    unsigned int beatTime = 0; //Current beat total time

    unsigned int soundTime = 0; //The current beat needs to sound time

    //Two tigers musical note table

    unsigned char code TwoTigerNote[] = {

    1, 2, 3, 1, 1, 2, 3, 1, 3, 4, 5, 3, 4, 5,

    5,6, 5,4, 3, 1, 5,6, 5,4, 3, 1, 1, 5, 1, 1, 5, 1,

   

    };

    //Two Tigers beat chart, 4 represents one beat, 1 represents 1/4 beat, 8 represents 2 beats

    unsigned char code TwoTigerBeat[] = {

    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 8,

    3,1, 3,1, 4, 4, 3,1, 3,1, 4, 4, 4, 4, 8, 4, 4, 8,

    };

   

    //Use the beat index as a loop variable

    for (beat=0; beat

        while (!tmrflag); //After each timer interrupt is completed, detect and process the beat

        tmrflag = 0;

        if (time == 0){ //Start a new beat after the current beat is finished

            note = TwoTigerNote[beat] - 1;

            T0RH = NoteReload[note] >> 8;

            T0RL = NoteReload[note];

            //Calculate the total beat time. Shifting right by 2 bits is equivalent to dividing by 4. Shifting instead of dividing can speed up execution.

            beatTime = (TwoTigerBeat[beat] * NoteFrequ[note]) >> 2;

            //Calculate the sound time, which is 0.75 of the total time. The shift principle is the same as above

            soundTime = beatTime - (beatTime >> 2);

            enable = 1; //instruct the buzzer to start sounding

            time++;

        }else{ //If the current beat is not finished, process the current beat

            //When the current duration reaches the total beat time, it returns to zero.

            // and increment the beat index to prepare to start a new beat

            if (time >= beatTime){

                time = 0;

                beat++;

            }else{ //When the current duration does not reach the total time,

                time++; //Accumulate time count

                //When the sounding time is reached, the buzzer is turned off.

                //Insert a silence interval of 0.25*total time,

                if (time == soundTime){

                    enable = 0; //Used to distinguish two consecutive beats

                }

            }

        }

    }

}

/* T0 interrupt service function, used to control the buzzer sound*/

void InterruptTimer0() interrupt 1{

    TH0 = T0RH; //Reload the reload value

    TL0 = T0RL;

    tmrflag = 1;

    if (enable){ //Reverse buzzer control level when enabled

        BUZZ = ~BUZZ;

    }else{ //Turn off the buzzer when not enabled

        BUZZ = 1;

    }

}


Reference address:Microcontroller buzzer control program and drive circuit

Previous article:Practical Motor Control Program
Next article:Single chip digital stopwatch program

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号