蜂鸣器从结构区分分为压电式蜂鸣器和电磁式蜂鸣器。压电式为压电陶瓷片发音,电流比较小一些,电磁式蜂鸣器为线圈通电震动发音,体积比较小。
按照驱动方式分为有源蜂鸣器和无源蜂鸣器。这里的有源和无源不是指电源,而是振荡源。有源蜂鸣器内部带了振荡源,如图 9-8 所示中,给了 BUZZ 引脚一个低电平,蜂鸣器就会直接响。而无源蜂鸣器内部是不带振荡源的,要让他响必须给 500Hz~4.5KHz 之间的脉冲频率信号来驱动它才会响。有源蜂鸣器往往比无源蜂鸣器贵一些,因为里边多了振荡电路,驱动发音也简单,靠电平就可以驱动,而无源蜂鸣器价格比较便宜,此外无源蜂鸣器声音频率可以控制,而音阶与频率又有确定的对应关系,因此就可以做出来“do re mi fa sol la si”的效果,可以用它制作出简单的音乐曲目,比如生日歌、两只老虎等等。
我们来看一下图 9-8 的电路,蜂鸣器电流依然相对较大,因此需要用三极管驱动,并且加了一个 100 欧的电阻作为限流电阻。此外还加了一个 D4 二极管,这个二极管叫做续流二极管。我们的蜂鸣器是感性器件,当三极管导通给蜂鸣器供电时,就会有导通电流流过蜂鸣器。而我们知道,电感的一个特点就是电流不能突变,导通时电流是逐渐加大的,这点没有问题,但当关断时,经“电源-三极管-蜂鸣器-地”这条回路就截断了,过不了任何电流了,那么储存的电流往哪儿去呢,就是经过这个 D4 和蜂鸣器自身的环路来消耗掉了,从而就避免了关断时由于电感电流造成的反向冲击。接续关断时的电流,这就是续流二极管名称的由来。
蜂鸣器经常用于电脑、打印机、万用表这些设备上做提示音,提示音一般也很简单,就是简单发出个声音就行,我们用程序简单做了个 4KHZ 频率下的发声和 1KHZ 频率下的发声程序,同学们可以自己研究下程序,比较下实际效果。
- #include <reg52.h>
- sbit BUZZ = P1^6;
- unsigned char T0RH = 0;
- unsigned char T0RL = 0;
- void OpenBuzz(unsigned int frequ);
- void StopBuzz();
-
- void main(){
- unsigned int i;
- TMOD = 0x01;
- EA = 1;
- while (1){
- OpenBuzz(4000);
- for (i=0; i<40000; i++);
- StopBuzz();
- for (i=0; i<40000; i++);
- OpenBuzz(1000);
- for (i=0; i<40000; i++);
- StopBuzz();
- for (i=0; i<40000; i++);
- }
- }
-
- void OpenBuzz(unsigned int frequ){
- unsigned int reload;
- reload = 65536 - (11059200/12)/(frequ*2);
- T0RH = (unsigned char)(reload >> 8);
- T0RL = (unsigned char)reload;
- TH0 = 0xFF;
- TL0 = 0xFE;
- ET0 = 1;
- TR0 = 1;
- }
-
- void StopBuzz(){
- ET0 = 0;
- TR0 = 0;
- }
-
- void InterruptTimer0() interrupt 1{
- TH0 = T0RH;
- TL0 = T0RL;
- BUZZ = ~BUZZ;
- }
另外用蜂鸣器来输出音乐,仅仅是好玩而已,应用很少,里边包含了音阶、乐谱的相关内容,程序也有一点复杂,所以就不详细给大家去讲解了。仅提供一个可以播放《两只老虎》的程序,大家可以下载到板子上玩玩,满足一下好奇心。
- #include <reg52.h>
- sbit BUZZ = P1^6;
- unsigned int code NoteFrequ[] = {
- 523, 587, 659, 698, 784, 880, 988,
- 1047, 1175, 1319, 1397, 1568, 1760, 1976
- };
- unsigned int code NoteReload[] = {
- 65536 - (11059200/12) / (523*2),
- 65536 - (11059200/12) / (587*2),
- 65536 - (11059200/12) / (659*2),
- 65536 - (11059200/12) / (698*2),
- 65536 - (11059200/12) / (784*2),
- 65536 - (11059200/12) / (880*2),
- 65536 - (11059200/12) / (988*2),
- 65536 - (11059200/12) / (1047*2),
- 65536 - (11059200/12) / (1175*2),
- 65536 - (11059200/12) / (1319*2),
- 65536 - (11059200/12) / (1397*2),
- 65536 - (11059200/12) / (1568*2),
- 65536 - (11059200/12) / (1760*2),
- 65536 - (11059200/12) / (1976*2),
- };
- bit enable = 1;
- bit tmrflag = 0;
- unsigned char T0RH = 0xFF;
- unsigned char T0RL = 0x00;
- void PlayTwoTiger();
-
- void main(){
- unsigned int i;
- EA = 1;
- TMOD = 0x01;
- TH0 = T0RH;
- TL0 = T0RL;
- ET0 = 1;
- TR0 = 1;
-
- while (1){
- PlayTwoTiger();
- for (i=0; i<40000; i++);
- }
- }
-
- void PlayTwoTiger(){
- unsigned char beat;
- unsigned char note;
- unsigned int time = 0;
- unsigned int beatTime = 0;
- unsigned int soundTime = 0;
-
- 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,
-
- };
-
- 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,
- };
-
-
- for (beat=0; beat<sizeof(TwoTigerNote); ){
- while (!tmrflag);
- tmrflag = 0;
- if (time == 0){
- note = TwoTigerNote[beat] - 1;
- T0RH = NoteReload[note] >> 8;
- T0RL = NoteReload[note];
-
- beatTime = (TwoTigerBeat[beat] * NoteFrequ[note]) >> 2;
-
- soundTime = beatTime - (beatTime >> 2);
- enable = 1;
- time++;
- }else{
-
-
- if (time >= beatTime){
- time = 0;
- beat++;
- }else{
- time++;
-
-
- if (time == soundTime){
- enable = 0;
- }
- }
- }
- }
- }
-
- void InterruptTimer0() interrupt 1{
- TH0 = T0RH;
- TL0 = T0RL;
- tmrflag = 1;
- if (enable){
- BUZZ = ~BUZZ;
- }else{
- BUZZ = 1;
- }
- }