作者:mtheory
家里门铃坏了一个多月了,一直没有时间去搞,过年前几天刚好有点时间,拆出来看看发现由于电池漏液造成了电路板多处被腐蚀,修也是麻烦了,所以索性就自己重新设计一个出来。这个门铃从电路设计,程序设计,调试及焊接都是我自己一手一脚完成,前后一共大概用了2天半时间。做出来的效果还是不错的。大家有兴趣可以试一下。
为了使门铃更有个性化,我采用的是单片机,用单片机来产生所需要的响声。其电路原理是控制单片机的IO口产生适当频率的方波,经过三极管放大后驱动自藕变压器的一次测,这时在变压器的二次测会产生相同频率的高压方波从而使压电陶瓷片发生形变,发出较大的门铃声。电源使用的是4节干电池,共6V,单片机电源是6V电源通过二极管降压0.7V后提供。
程序的原理就是通过控制单片机的计时器T1和T2来产生不同频率的PWM波,然后通过定义一个歌曲的数组,来控制不同音调,音高,音长,音高,音长。以下是曲谱的储存格式:
曲谱存贮格式 unsigned char code MusicName{音高,音长,音高,音长...., 0,0}; 末尾:0,0 表示结束(Important)
音高由三位数字组成:
个位是表示 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, 0x0F,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 };
|