单片机蜂鸣器

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验原理51单片机的一个I/O口控制speaker发声,演唱祝你平安歌曲。主要器件以及电路图单片机——AT89C51,蜂鸣器——speaker。一、电磁式蜂鸣器驱动原理蜂鸣器发声原理是电流通过电磁线圈,使电磁线圈产生磁场来驱动振动膜发声的,因此需要一定的电流才能驱动它,单片机IO引脚输出的电流较小,单片机输出的TTL电平基本上驱动不了蜂鸣器,因此需要增加一个电流放大的电路。S51增强型单片机实验板通过一个三极管C8550来放大驱动蜂鸣器,原理图见下面图二、如图所示,蜂鸣器的正极接到VCC(+5V)电源上面,蜂鸣器的负极接到三极管的发射极E,三极管的基级B经过限流电阻R1后由单片机的P3.7引脚控制,当P3.7输出高电平时,三极管T1截止,没有电流流过线圈,蜂鸣器不发声;当P3.7输出低电平时,三极管导通,这样蜂鸣器的电流形成回路,发出声音。因此,我们可以通过程序控制P3.7脚的电平来使蜂鸣器发出声音和关闭。程序中改变单片机P3.7引脚输出波形的频率,就可以调整控制蜂鸣器音调,产生各种不同音色、音调的声音。另外,改变P3.7输出电平的高低电平占空比,则可以控制蜂鸣器的声音大小,这些我们都可以通过编程实验来验证。;------------------------------------;蜂鸣器演奏--祝你平安;功能:蜂鸣器-蜂鸣器奏乐-祝你平安;------------------------------------SPKbitP2.3ORG0000HLJMPSTARTORG000BHINC20H;中断服务,中断计数器加1MOVTH0,#0D8HMOVTL0,#0EFH;12M晶振,形成10毫秒中断RETISTART:MOVSP,#60H;计数器中断初始化MOVTH0,#0D8HMOVTL0,#0EFHMOVTMOD,#01HMOVIE,#82HMUSIC0:NOPMOVDPTR,#DAT;表头地址送DPTRMOV20H,#00H;中断计数器清0MOVB,#00H;表序号清0MUSIC1:NOPCLRAMOVCA,@A+DPTR;查表取代码JZEND0;是00H,则结束CJNEA,#0FFH,MUSIC5LJMPMUSIC3MUSIC5:NOPMOVR6,AINCDPTRMOVA,BMOVCA,@A+DPTR;取节拍代码送R7MOVR7,ASETBTR0;启动计数MUSIC2:NOPCPLSPKMOVA,R6MOVR3,ALCALLDELMOVA,R7CJNEA,20H,MUSIC2;中断计数器(20H)=R7否?不等,则继续循环MOV20H,#00H;等于,则取下一代码INCDPTR;INCBLJMPMUSIC1MUSIC3:NOPCLRTR0;休止100毫秒MOVR2,#0DHMUSIC4:NOPMOVR3,#0FFHLCALLDELDJNZR2,MUSIC4INCDPTRLJMPMUSIC1END0:NOPMOVR2,#64H;歌曲结束,延时1秒后继续MUSIC6:MOVR3,#00HLCALLDELDJNZR2,MUSIC6LJMPMUSIC0DEL:NOPDEL3:MOVR4,#02HDEL4:NOPDJNZR4,DEL4NOPDJNZR3,DEL3RETNOPDAT:;祝你平安db26h,20h,20h,20h,20h,20h,26h,10h,20h,10h,20h,80h,26h,20h,30h,20hdb30h,20h,39h,10h,30h,10h,30h,80h,26h,20h,20h,20h,20h,20h,1ch,20hdb20h,80h,2bh,20h,26h,20h,20h,20h,2bh,10h,26h,10h,2bh,80h,26h,20hdb30h,20h,30h,20h,39h,10h,26h,10h,26h,60h,40h,10h,39h,10h,26h,20hdb30h,20h,30h,20h,39h,10h,26h,10h,26h,80h,26h,20h,2bh,10h,2bh,10hdb2bh,20h,30h,10h,39h,10h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,20hdb20h,10h,20h,10h,2bh,10h,26h,30h,30h,80h,18h,20h,18h,20h,26h,20hdb20h,20h,20h,40h,26h,20h,2bh,20h,30h,20h,30h,20h,1ch,20h,20h,20hdb20h,80h,1ch,20h,1ch,20h,1ch,20h,30h,20h,30h,60h,39h,10h,30h,10hdb20h,20h,2bh,10h,26h,10h,2bh,10h,26h,10h,26h,10h,2bh,10h,2bh,80hdb18h,20h,18h,20h,26h,20h,20h,20h,20h,60h,26h,10h,2bh,20h,30h,20hdb30h,20h,1ch,20h,20h,20h,20h,80h,26h,20h,30h,10h,30h,10h,30h,20hdb39h,20h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,10h,40h,10h,20h,10hdb20h,10h,2bh,10h,26h,30h,30h,80h,00HEND;程序结束

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功