单片机蜂鸣器的控制程序与驱动电路图

2019-07-21 07:34发布

<p> 蜂鸣器从结构区分分为压电式蜂鸣器和电磁式蜂鸣器。压电式为压电陶瓷片发音,电流比较小一些,电磁式蜂鸣器为线圈通电震动发音,体积比较小。按照驱动方式分为有源蜂鸣器和无源蜂鸣器。这里的有源和无源不是指电源,而是振荡源。有源蜂鸣器内部带了振荡源,如图1中所示,给了 BUZZ 引脚一个低电平,蜂鸣器就会直接响。而无源蜂鸣器内部是不带振荡源的,要让他响必须给 500Hz~4.5KHz 之间的脉冲频率信号来驱动它才会响。有源蜂鸣器往往比无源蜂鸣器贵一些,因为里边多了振荡电路,驱动发音也简单,靠电平就可以驱动,而无源蜂鸣器价格比较便宜,此外无源蜂鸣器声音频率可以控制,而音阶与频率又有确定的对应关系,因此就可以做出来“do re mi fa sol la si”的效果,可以用它制作出简单的音乐曲目,比如生日歌、两只老虎等等。</p><p>&nbsp; <ignore_js_op><img src="data/attach/1907/jkb4s4vr1ambdxeleolmtur6ae408rma.jpg" alt="b1b9d54851b0dad7c14df00cd97e0de7.jpg" title="b1b9d54851b0dad7c14df00cd97e0de7.jpg">&nbsp;</ignore_js_op></p><p><ignore_js_op></ignore_js_op>来看一下图1的电路,蜂鸣器电流依然相对较大,因此需要用三极管驱动,并且加了一个 100 欧的电阻作为限流电阻。此外还加了一个 D4 二极管,这个二极管叫做续流二极管。我们的蜂鸣器是感性器件,当三极管导通给蜂鸣器供电时,就会有导通电流流过蜂鸣器。而我们知道,电感的一个特点就是电流不能突变,导通时电流是逐渐加大的,这点没有问题,但当关断时,经“电源-三极管-蜂鸣器-地”这条回路就截断了,过不了任何电流了,那么储存的电流往哪儿去呢,就是经过这个 D4 和蜂鸣器自身的环路来消耗掉了,从而就避免了关断时由于电感电流造成的反向冲击。接续关断时的电流,这就是续流二极管名称的由来。蜂鸣器经常用于电脑、打印机、万用表这些设备上做提示音,提示音一般也很简单,就是简单发出个声音就行,我们用程序简单做了个 4KHZ 频率下的发声和 1KHZ 频率下的发声程序,同学们可以自己研究下程序,比较下实际效果。</p><pre style="max-width: 100%;"><code class="cpp hljs" codemark="1"><span class="hljs-meta">#&lt;reg52.h&gt;</span> sbit BUZZ = P1^<span class="hljs-number">6</span>; <span class="hljs-comment">//蜂引脚</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> T0RH = <span class="hljs-number">0</span>; <span class="hljs-comment">//T0 重载值的高字节</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> T0RL = <span class="hljs-number">0</span>; <span class="hljs-comment">//T0 重载值的低字节</span> OpenBuzz(<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> fr); <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">StopBuzz</span><span class="hljs-params">()</span></span>; <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span>{ <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> i; TMOD = <span class="hljs-number">0x01</span>; <span class="hljs-comment">//配置 T0 工作在模式 1,但先不启动</span> EA = <span class="hljs-number">1</span>; (<span class="hljs-number">1</span>){ <span class="hljs-comment">//使能全局中断</span> OpenBuzz(<span class="hljs-number">4000</span>); <span class="hljs-comment">//以 4KHz 的频率启动蜂鸣器</span> <span class="hljs-keyword">for</span> (i=<span class="hljs-number">0</span>; i&lt;<span class="hljs-number">40000</span>; i++); StopBuzz(); <span class="hljs-comment">//停止蜂鸣器</span> <span class="hljs-keyword">for</span> (i=<span class="hljs-number">0</span>; i&lt;<span class="hljs-number">40000</span>; i++); OpenBuzz(<span class="hljs-number">1000</span>); <span class="hljs-comment">//以 1KHz 的频率启动蜂鸣器</span> <span class="hljs-keyword">for</span> (i=<span class="hljs-number">0</span>; i&lt;<span class="hljs-number">40000</span>; i++); StopBuzz(); <span class="hljs-comment">//停止蜂鸣器</span> <span class="hljs-keyword">for</span> (i=<span class="hljs-number">0</span>; i&lt;<span class="hljs-number">40000</span>; i++); } } <span class="hljs-comment">/* 蜂鸣器启动函数,frequ-工作频率 */</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">OpenBuzz</span><span class="hljs-params">(<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> frequ)</span></span>{ <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> reload;<span class="hljs-comment">//计算所需的重载值</span> reload = <span class="hljs-number">65536</span> - (<span class="hljs-number">11059200</span>/<span class="hljs-number">12</span>)/(frequ*<span class="hljs-number">2</span>); <span class="hljs-comment">//由给定算定时器重载值</span> T0RH = (<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span>)(reload &gt;&gt; <span class="hljs-number">8</span>); <span class="hljs-comment">//16 位重载值分解为高低两个字节</span> T0RL = (<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span>)reload; TH0 = <span class="hljs-number">0xFF</span>; <span class="hljs-comment">//设定一个接近溢出的初值,以使定时器马上投入工作</span> TL0 = <span class="hljs-number">0xFE</span>; ET0 = <span class="hljs-number">1</span>; <span class="hljs-comment">//使能 T0 中断</span> TR0 = <span class="hljs-number">1</span>; <span class="hljs-comment">//启动 T0</span> } <span class="hljs-comment">/* 蜂鸣器停止函数 */</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">StopBuzz</span><span class="hljs-params">()</span></span>{ ET0 = <span class="hljs-number">0</span>; <span class="hljs-comment">//禁用 T0 中断</span> TR0 = <span class="hljs-number">0</span>; <span class="hljs-comment">//停止 T0</span> } <span class="hljs-comment">/* T0 中断服务函数,用于控制蜂鸣器发声 */</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">InterruptTimer0</span><span class="hljs-params">()</span> 1</span>{ TH0 = T0RH; <span class="hljs-comment">//重新加载重载值</span> TL0 = T0RL; BUZZ = ~BUZZ; <span class="hljs-comment">//反转蜂鸣器控制电平</span> }</code></pre><p>另外用蜂鸣器来输出音乐,仅仅是好玩而已,应用很少,里边包含了音阶、乐谱的相关内容,程序也有一点复杂,所以就不详细给大家去讲解了。仅提供一个可以播放《两只老虎》的程序,大家可以下载到板子上玩玩,满足一下好奇心。</p><p><br></p><pre style="max-width: 100%;"><code class="cpp hljs" codemark="1"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;reg52.h&gt;</span></span> sbit BUZZ = P1^<span class="hljs-number">6</span>; <span class="hljs-comment">//蜂鸣器控制引脚</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> code NoteFrequ[] = { <span class="hljs-comment">//中音 1-7 和高音 1-7 对应频率列表</span> <span class="hljs-number">523</span>, <span class="hljs-number">587</span>, <span class="hljs-number">659</span>, <span class="hljs-number">698</span>, <span class="hljs-number">784</span>, <span class="hljs-number">880</span>, <span class="hljs-number">988</span>, <span class="hljs-comment">//中音 1-7</span> <span class="hljs-number">1047</span>, <span class="hljs-number">1175</span>, <span class="hljs-number">1319</span>, <span class="hljs-number">1397</span>, <span class="hljs-number">1568</span>, <span class="hljs-number">1760</span>, <span class="hljs-number">1976</span> <span class="hljs-comment">//高音 1-7</span> }; <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> code NoteReload[] = { <span class="hljs-comment">//中音 1-7 和高音 1-7 对应的定时器重载值</span> <span class="hljs-number">65536</span> - (<span class="hljs-number">11059200</span>/<span class="hljs-number">12</span>) / (<span class="hljs-number">523</span>*<span class="hljs-number">2</span>), <span class="hljs-comment">//中音 1</span> <span class="hljs-number">65536</span> - (<span class="hljs-number">11059200</span>/<span class="hljs-number">12</span>) / (<span class="hljs-number">587</span>*<span class="hljs-number">2</span>), <span class="hljs-comment">//2</span> <span class="hljs-number">65536</span> - (<span class="hljs-number">11059200</span>/<span class="hljs-number">12</span>) / (<span class="hljs-number">659</span>*<span class="hljs-number">2</span>), <span class="hljs-comment">//3</span> <span class="hljs-number">65536</span> - (<span class="hljs-number">11059200</span>/<span class="hljs-number">12</span>) / (<span class="hljs-number">698</span>*<span class="hljs-number">2</span>), <span class="hljs-comment">//4</span> <span class="hljs-number">65536</span> - (<span class="hljs-number">11059200</span>/<span class="hljs-number">12</span>) / (<span class="hljs-number">784</span>*<span class="hljs-number">2</span>), <span class="hljs-comment">//5</span> <span class="hljs-number">65536</span> - (<span class="hljs-number">11059200</span>/<span class="hljs-number">12</span>) / (<span class="hljs-number">880</span>*<span class="hljs-number">2</span>), <span class="hljs-comment">//6</span> <span class="hljs-number">65536</span> - (<span class="hljs-number">11059200</span>/<span class="hljs-number">12</span>) / (<span class="hljs-number">988</span>*<span class="hljs-number">2</span>), <span class="hljs-comment">//7</span> <span class="hljs-number">65536</span> - (<span class="hljs-number">11059200</span>/<span class="hljs-number">12</span>) / (<span class="hljs-number">1047</span>*<span class="hljs-number">2</span>), <span class="hljs-comment">//高音 1</span> <span class="hljs-number">65536</span> - (<span class="hljs-number">11059200</span>/<span class="hljs-number">12</span>) / (<span class="hljs-number">1175</span>*<span class="hljs-number">2</span>), <span class="hljs-comment">//2</span> <span class="hljs-number">65536</span> - (<span class="hljs-number">11059200</span>/<span class="hljs-number">12</span>) / (<span class="hljs-number">1319</span>*<span class="hljs-number">2</span>), <span class="hljs-comment">//3</span> <span class="hljs-number">65536</span> - (<span class="hljs-number">11059200</span>/<span class="hljs-number">12</span>) / (<span class="hljs-number">1397</span>*<span class="hljs-number">2</span>), <span class="hljs-comment">//4</span> <span class="hljs-number">65536</span> - (<span class="hljs-number">11059200</span>/<span class="hljs-number">12</span>) / (<span class="hljs-number">1568</span>*<span class="hljs-number">2</span>), <span class="hljs-comment">//5</span> <span class="hljs-number">65536</span> - (<span class="hljs-number">11059200</span>/<span class="hljs-number">12</span>) / (<span class="hljs-number">1760</span>*<span class="hljs-number">2</span>), <span class="hljs-comment">//6</span> <span class="hljs-number">65536</span> - (<span class="hljs-number">11059200</span>/<span class="hljs-number">12</span>) / (<span class="hljs-number">1976</span>*<span class="hljs-number">2</span>), <span class="hljs-comment">//7</span> }; bit = <span class="hljs-number">1</span>; <span class="hljs-comment">//蜂鸣器发声使能标志</span> bit tmrflag = <span class="hljs-number">0</span>; <span class="hljs-comment">//完成标志</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> T0RH = <span class="hljs-number">0xFF</span>; <span class="hljs-comment">//T0 重载值的高字节</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> T0RL = <span class="hljs-number">0x00</span>; <span class="hljs-comment">//T0 重载值的低字节</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">PlayTwoTiger</span><span class="hljs-params">()</span></span>; <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span>{ <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> i; EA = <span class="hljs-number">1</span>; <span class="hljs-comment">//使能全局中断</span> TMOD = <span class="hljs-number">0x01</span>; <span class="hljs-comment">//配置 T0 工作在模式 1</span> TH0 = T0RH; TL0 = T0RL; ET0 = <span class="hljs-number">1</span>; <span class="hljs-comment">//使能 T0 中断</span> TR0 = <span class="hljs-number">1</span>; <span class="hljs-comment">//启动 T0</span> <span class="hljs-keyword">while</span> (<span class="hljs-number">1</span>){ PlayTwoTiger(); <span class="hljs-comment">//播放乐曲--两支老虎</span> <span class="hljs-keyword">for</span> (i=<span class="hljs-number">0</span>; i&lt;<span class="hljs-number">40000</span>; i++); <span class="hljs-comment">//停止一段时间</span> } } <span class="hljs-comment">/* 两支老虎乐曲播放函数 */</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">PlayTwoTiger</span><span class="hljs-params">()</span></span>{ <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> beat; <span class="hljs-comment">//当前节拍索引</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> note; <span class="hljs-comment">//当前节拍对应的音符</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> e = <span class="hljs-number">0</span>; <span class="hljs-comment">//当前节拍计时</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> beatTime = <span class="hljs-number">0</span>; <span class="hljs-comment">//当前节拍总时间</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">int</span> soundTime = <span class="hljs-number">0</span>; <span class="hljs-comment">//当前节拍需发声时间</span> <span class="hljs-comment">//两只老虎音符表</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> code TwoTigerNote[] = { <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>,<span class="hljs-number">6</span>, <span class="hljs-number">5</span>,<span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">5</span>,<span class="hljs-number">6</span>, <span class="hljs-number">5</span>,<span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">5</span>, <span class="hljs-number">1</span>, }; <span class="hljs-comment">//两只老虎节拍表,4 表示一拍,1 就是 1/4 拍,8 就是 2 拍</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> code TwoTigerBeat[] = { <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">8</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">8</span>, <span class="hljs-number">3</span>,<span class="hljs-number">1</span>, <span class="hljs-number">3</span>,<span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">3</span>,<span class="hljs-number">1</span>, <span class="hljs-number">3</span>,<span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">8</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">8</span>, }; <span class="hljs-comment">//用节拍索引作为循环变量</span> <span class="hljs-keyword">for</span> (beat=<span class="hljs-number">0</span>; beat&lt;<span class="hljs-keyword">sizeof</span>(TwoTigerNote); ){ <span class="hljs-keyword">while</span> (!tmrflag); <span class="hljs-comment">//每次定时器中断完成后,检测并处理节拍</span> tmrflag = <span class="hljs-number">0</span>; <span class="hljs-keyword">if</span> (time == <span class="hljs-number">0</span>){ <span class="hljs-comment">//当前节拍播完则启动一个新节拍</span> note = TwoTigerNote[beat] - <span class="hljs-number">1</span>; T0RH = NoteReload[note] &gt;&gt; <span class="hljs-number">8</span>; T0RL = NoteReload[note]; <span class="hljs-comment">//计算节拍总时间,右移 2 位相当于除 4,移位代替除法可以加快执行速度</span> beatTime = (TwoTigerBeat[beat] * NoteFrequ[note]) &gt;&gt; <span class="hljs-number">2</span>; <span class="hljs-comment">//计算发声时间,为总时间的 0.75,移位原理同上</span> soundTime = beatTime - (beatTime &gt;&gt; <span class="hljs-number">2</span>); enable = <span class="hljs-number">1</span>; <span class="hljs-comment">//指示蜂鸣器开始发声</span> time++; }e{ <span class="hljs-comment">//当前节拍未播完则处理当前节拍</span> <span class="hljs-comment">//当前持续时间到达节拍总时间时归零,</span> <span class="hljs-comment">//并递增节拍索引,以准备启动新节拍</span> <span class="hljs-keyword">if</span> (time &gt;= beatTime){ time = <span class="hljs-number">0</span>; beat++; }<span class="hljs-keyword">else</span>{ <span class="hljs-comment">//当前持续时间未达到总时间时,</span> time++; <span class="hljs-comment">//累加时间计数</span> <span class="hljs-comment">//到达发声时间后,指示关闭蜂鸣器,</span> <span class="hljs-comment">//插入 0.25*总时间的静音间隔,</span> <span class="hljs-keyword">if</span> (time == soundTime){ enable = <span class="hljs-number">0</span>; <span class="hljs-comment">//用以区分连续的两个节拍</span> } } } } } <span class="hljs-comment">/* T0 中断服务函数,用于控制蜂鸣器发声 */</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">InterruptTimer0</span><span class="hljs-params">()</span> interrupt 1</span>{ TH0 = T0RH; <span class="hljs-comment">//重新加载重载值</span> TL0 = T0RL; tmrflag = <span class="hljs-number">1</span>; <span class="hljs-keyword">if</span> (enable){ <span class="hljs-comment">//使能时反转蜂鸣器控制电平</span> BUZZ = ~BUZZ; }<span class="hljs-keyword">else</span>{ <span class="hljs-comment">//未使能时关闭蜂鸣器</span> BUZZ = <span class="hljs-number">1</span>; } }</code></pre><p> 嵌入式单片机stm32学习资料群:816509137<br> </p><p><br></p><p></p><p><br></p>
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。