原创 DIYer 的音乐门铃

2009-2-22 15:39 2505 12 14 分类: MCU/ 嵌入式

家里门铃坏了一个多月了,一直没有时间去搞,过年前几天刚好有点时间,拆出来看看发现由于电池漏液造成了电路板多处被腐蚀,修也是麻烦了,所以索性就自己重新设计一个出来。这个门铃从电路设计,程序设计,调试及焊接都是我自己一手一脚完成,前后一共大概用了2天半时间。做出来的效果还是不错的。大家有兴趣可以试一下。


为了使门铃更有个性化,我采用的是单片机,用单片机来产生所需要的响声。其电路原理是控制单片机的IO口产生适当频率的方波,经过三极管放大后驱动自藕变压器的一次测,这时在变压器的二次测会产生相同频率的高压方波从而使压电陶瓷片发生形变,发出较大的门铃声。电源使用的是4节干电池,共6V,单片机电源是6V电源通过二极管降压0.7V后提供。


程序的原理就是通过控制单片机的计时器T1T2来产生不同频率的PWM波,然后通过定义一个歌曲的数组,来控制不同音调,音高,音长,音高,音长。以下是曲谱的储存格式:


曲谱存贮格式 unsigned char code MusicName{音高,音长,音高,音长...., 0,0};    末尾:0,0 表示结束(Important)


<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 


 音高由三位数字组成:


             个位是表示 1~7 这七个音符


             十位是表示音符所在的音区:1-低音,2-中音,3-高音;


             百位表示这个音符是否要升半音: 0-不升,1-升半音。


 


 音长最多由三位数字组成:


             个位表示音符的时值,其对应关系是:


                    |数值(n):  |0 |1 |2 |3 | 4 | 5 | 6


                    |几分音符: |1 |2 |4 |8 |16 |32 |64          音符=2^n


             十位表示音符的演奏效果(0-2):  0-普通,1-连音,2-顿音


             百位是符点位: 0-无符点,1-有符点


 


 调用演奏子程序的格式


             Play(乐曲名,调号,升降八度,演奏速度);


       |乐曲名           : 要播放的乐曲指针,结尾以(0,0)结束;


       |调号(0-11)       :      是指乐曲升多少个半音演奏;


       |升降八度(1-3)        : 1:降八度, 2:不升不降, 3:升八度;


       |演奏速度(1-12000):     值越大速度越快;


 


由于门铃在没有按下时候CPU是没有工作任务的,所以在没有按下门铃时,我令CPU进入休眠状态,这样能够省下不少的电,使电池的工作寿命延长。当有人按下门铃时,就会产生外部中断INT0,使CPU退出休眠状态,进入全速运行状态,当歌曲演奏完毕,重新自动进入休眠状态。下面是主程序和中断服务程序


void main()


{


       IT0=1; //外中断跳变产生中断


       EX0=1;


       EA=1; //打开总中断


       InitialSound();


       PCON |= 0x01;


       while(1)


       {


             


              if(Play_music==1)


              {


                     P1_7=0;


                     Play(Music_happynewyear,0,3,360);


                     P1_7=1;


                     Delay1ms(1500);


                     P1_7=0;


                     Play(Music_yingchun,0,3,360);


                     P1_7=1;


                     Play_music=0;


                     PCON |= 0x01;


              }


       }


}


 


void int0() interrupt 0 using 1


{


       PCON &=~ 0x00;


       Play_music=1;


}


点击看大图


                                                                            电路原理图


 


由于当时快要过年了,所以我编了一首“新年好”和“迎春花”应一下节,其实所有歌都可以写,只要有乐谱就可以,这样你就可以把自己喜欢的歌编成程序烧入芯片,打造个性化的音乐门铃。下面我给出一些歌的编码,由于2051里面的flash容量有限,通常只能装下2首歌,所以如果想换歌时又要自己重新把代码替换烧入芯片。


//新年好


unsigned char code Music_happynewyear[]={ 0x15,0x03, 0x15,0x03, 0x15,0x02, 0x<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />0F,0x02, 0x17,0x03,0x17,0x03, 0x17,0x02, 0x15,0x02, 0x15,0x03, 0x17,0x03,0x19,0x02,0x19,0x02, 0x18,0x03,0x17,0x03,0x16,0x01,0x16,0x03,0x17,0x03,0x18,0x02,0x18,0x02,0x17,0x03,0x16,0x03, 0x17,0x02, 0x15,0x02, 0x15,0x03, 0x17,0x03, 0x16,0x02, 0x0F,0x02, 0x11,0x03, 0x16,0x03, 0x15,0x01, 0x00,0x00 };


//迎春花


unsigned char code Music_yingchun[]={ 0x1F,0x03, 0x1F,0x04, 0x1A,0x04, 0x20,0x04, 0x1F,0x04, 0x1A,0x03, 0x19,0x03, 0x19,0x04, 0x17,0x04, 0x1A,0x04, 0x19,0x04, 0x17,0x03, 0x20,0x04, 0x1F,0x04, 0x1B,0x04, 0x1A,0x04, 0x19,0x04, 0x18,0x04, 0x17,0x04, 0x16,0x04,


0x15,0x03, 0x17,0x04, 0x19,0x04, 0x1A,0x04, 0x1A,0x04,0x1A,0x04, 0x1A,0x04, 0x1A,0x03, 0x19,0x03, 0x1A,0x03,0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1A,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x02, 0x16,0x03, 0x17,0x01, 0x17,0x67, 0x19,0x04, 0x1A,0x03, 0x1F,0x03, 0x19,0x02, 0x19,0x03, 0x17,0x03, 0x16,0x67, 0x17,0x04, 0x16,0x03, 0x15,0x03,0x15,0x03, 0x10,0x02, 0x0F,0x03,0x10,0x01,0x10,0x01,0x17,0x03,0x19,0x03,0x17,0x67,0x16,0x04,0x17,0x03,0x19,0x03, 0x17,0x67, 0x16,0x04, 0x17,0x03, 0x19,0x03,0x19,0x03, 0x10,0x03, 0x15,0x01, 0x17,0x03, 0x1F,0x03, 0x1A,0x67, 0x19,0x04, 0x17,0x03, 0x1F,0x03, 0x1A,0x67, 0x19,0x04, 0x1F,0x67, 0x1F,0x04, 0x20,0x04, 0x1F,0x04, 0x1A,0x03, 0x19,0x01, 0x19,0x03, 0x1A,0x03, 0x19,0x03, 0x17,0x04, 0x16,0x04, 0x19,0x03, 0x10,0x03, 0x15,0x17, 0x1F,0x03, 0x1F,0x02, 0x1A,0x04, 0x19,0x04, 0x1A,0x03, 0x1F,0x03, 0x17,0x17, 0x17,0x67, 0x19,0x04, 0x1A,0x03,0x1F,0x03, 0x19,0x02, 0x19,0x67, 0x17,0x04, 0x16,0x67,0x17,0x04, 0x16,0x03, 0x15,0x03, 0x15,0x03, 0x10,0x02, 0x0F,0x03, 0x10,0x01, 0x00,0x00 };


//挥着翅膀的女孩


unsigned char code Music_Girl[]={ 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x03,


0x16,0x03, 0x17,0x03, 0x17,0x03, 0x17,0x03, 0x18,0x03,0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x02, 0x18,0x03, 0x17,0x03, 0x15,0x02, 0x18,0x03, 0x17,0x03, 0x18,0x02,0x10,0x03, 0x15,0x03, 0x16,0x02, 0x15,0x03, 0x16,0x03,0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x1A,0x03, 0x1B,0x03, 0x1F,0x03, 0x1F,0x03, 0x17,0x03, 0x18,0x03,0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03, 0x1F,0x03, 0x1F,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03, 0x20,0x03, 0x20,0x02, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03, 0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03, 0x15,0x03, 0x15,0x03, 0x17,0x03, 0x16,0x66, 0x17,0x04, 0x18,0x04, 0x18,0x03, 0x19,0x03,0x1F,0x03, 0x1B,0x03, 0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66,0x1A,0x03, 0x19,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x00, 0x1A,0x03, 0x1A,0x03, 0x1A,0x03, 0x1B,0x03,0x1B,0x03, 0x1A,0x03, 0x19,0x03, 0x19,0x02, 0x17,0x03,0x15,0x17, 0x15,0x03, 0x16,0x03, 0x17,0x03, 0x18,0x03,


0x17,0x04, 0x18,0x0E, 0x18,0x03, 0x17,0x04, 0x18,0x0E,0x18,0x66, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03, 0x20,0x03, 0x20,0x02, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66,0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03, 0x1B,0x03,0x1F,0x66, 0x1F,0x04, 0x1B,0x0E, 0x1B,0x03, 0x19,0x03,0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03, 0x15,0x03, 0x15,0x03, 0x17,0x03,0x16,0x66,0x17,0x04,0x18,0x04,0x18,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03, 0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x00, 0x18,0x02, 0x18,0x03, 0x1A,0x03, 0x19,0x0D, 0x15,0x03, 0x15,0x02,0x18,0x66, 0x16,0x02, 0x17,0x02, 0x15,0x00, 0x00,0x00};


//同一首歌


unsigned char code Music_Same[]={ 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x66, 0x18,0x03,


0x17,0x02, 0x15,0x02, 0x16,0x01, 0x15,0x02, 0x10,0x02,0x15,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x02, 0x18,0x66, 0x17,0x03, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x16,0x00, 0x17,0x01, 0x19,0x02, 0x1B,0x02, 0x1B,0x70, 0x1A,0x03, 0x1A,0x01, 0x19,0x02, 0x19,0x03, 0x1A,0x03, 0x1B,0x02, 0x1A,0x0D, 0x19,0x03, 0x17,0x00,0x18,0x66,0x18,0x03,0x19,0x02,0x1A,0x02,0x19,0x0C,0x18,0x0D,0x17,0x03,0x16,0x01, 0x11,0x02, 0x11,0x03, 0x10,0x03, 0x0F,0x0C,0x10,0x02, 0x15,0x00, 0x1F,0x01, 0x1A,0x01, 0x18,0x66, 0x19,0x03, 0x1A,0x01, 0x1B,0x02, 0x1B,0x03, 0x1B,0x03,0x1B,0x0C, 0x1A,0x0D, 0x19,0x03,0x17,0x00,0x1F,0x01,0x1A,0x01,0x18,0x66,0x19,0x03,0x1A,0x01,0x10,0x02,0x10,0x03, 0x10,0x03, 0x1A,0x0C, 0x18,0x0D, 0x17,0x03,0x16,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x70, 0x18,0x03, 0x17,0x02, 0x15,0x03, 0x15,0x03, 0x16,0x66, 0x16,0x03, 0x16,0x02, 0x16,0x03, 0x15,0x03, 0x10,0x02, 0x10,0x01, 0x11,0x01, 0x11,0x66, 0x10,0x03, 0x0F,0x0C, 0x1A,0x02, 0x19,0x02, 0x16,0x03, 0x16,0x03, 0x18,0x66,0x18,0x03, 0x18,0x02, 0x17,0x03, 0x16,0x03, 0x19,0x00, 0x00,0x00 };         


//两只蝴蝶                                 


unsigned char code Music_Two[] ={ 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03,


0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03, 0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x16,0x03,0x17,0x01, 0x16,0x03, 0x17,0x03, 0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03, 0x15,0x03, 0x16,0x01, 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03, 0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03, 0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x19,0x03, 0x19,0x01, 0x19,0x03, 0x1A,0x03, 0x19,0x03, 0x17,0x01, 0x16,0x03, 0x16,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03, 0x10,0x0D, 0x15,0x00, 0x19,0x03, 0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03, 0x1B,0x03, 0x1A,0x03, 0x17,0x0D,0x16,0x03,0x16,0x03,0x16,0x0D,0x17,0x01,0x17,0x03,0x17,0x03,0x19,0x03,0x1A,0x02,0x1A,0x02,0x10,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x01, 0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03, 0x19,0x02, 0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04, 0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04, 0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03,


0x17,0x0D, 0x16,0x03, 0x17,0x03, 0x19,0x01, 0x19,0x03, 0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03, 0x1B,0x03, 0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03, 0x16,0x03, 0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03, 0x1A,0x02, 0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x01, 0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03, 0x19,0x03, 0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04, 0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,


0x17,0x16, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04, 0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03, 0x0F,0x02, 0x10,0x03, 0x15,0x00, 0x00,0x00 };


 


附件里面是程序工程及电路原理图


https://static.assets-stash.eet-china.com/album/old-resources/2009/2/22/3049e909-cafa-45f0-9fae-883ca104fd52.rar

文章评论2条评论)

登录后参与讨论

用户518079 2009-2-23 11:54

好东东

用户1569463 2009-2-22 16:27

很实用的东东!
相关推荐阅读
用户75640 2009-08-05 21:01
地球一小时
        自从130年前爱迪生发明电灯以来,电就成为了人类必不可少的能源之一。从此人类就进入了电气时代,电灯也使人类在黑暗当中得到了光明,但也带来了一系列的环境问题。因为根据能量守恒定律,能量是...
用户75640 2009-05-02 17:53
参加zlg嵌入式大赛有感
这次zlg杯嵌入式比赛结果终于出来的,最终我们还是没上到2等奖,虽然有点失望,但是回想起这几个月的过关斩将,我们的作品还是得到肯定。从开始比赛提交方案,以高票入围全国500队之一的初选,到进入最终53...
用户75640 2009-02-18 17:09
我的暗恋
就是这样,一眨眼就到了高中,高中是个拼搏的时间,我也没有其他心情去注意女孩了,想一心读好书。但是就在高二的一天,我打开电子邮箱,发现一个熟悉的名字出现在收件箱里面,邮件内容是一个贺卡。刚才我说的那个名...
用户75640 2009-02-18 17:02
我的初恋
我的第一次爱情发生在小学。本人在小学那时由于是学校田径队和篮球队的主要成员,并且成绩也不错,身边不乏追求者,几乎坐过我旁边的女生都喜欢上我,但是我对这并不感冒。记得在小学5年级我喜欢了她,开始在上课时...
用户75640 2008-07-03 21:34
麻省理工与哈佛博士教育之比较(何毓琦院士)
不想看完的,就两句话:MIT严格要求,哈佛要靠自己.也是两种学习的方法。       我成年后的整个生活几乎都是在马萨诸塞州的剑桥市渡过的。我从麻省理工获得本科和硕士学位,从哈佛获得博士学位,然后从1...
我要评论
2
12
关闭 站长推荐上一条 /2 下一条