家里门铃坏了一个多月了,一直没有时间去搞,过年前几天刚好有点时间,拆出来看看发现由于电池漏液造成了电路板多处被腐蚀,修也是麻烦了,所以索性就自己重新设计一个出来。这个门铃从电路设计,程序设计,调试及焊接都是我自己一手一脚完成,前后一共大概用了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 };
上一篇:AT89C52单片机的SD卡读写设计原理
下一篇:基于STC89C52单片机的智能花盆设计案例
推荐阅读最新更新时间:2024-11-13 06:18
设计资源 培训 开发板 精华推荐
- 使用 Microchip Technology 的 TC2950 的参考设计
- ADR434B 4.096 Vout 超低噪声 XFET 电压基准的典型应用,具有灌电流和拉电流能力
- LT3430EFE、42V 至 5V 降压转换器
- 83键特殊配列机械键盘
- 基于CW32f030利用android实现与循迹功能切换
- EVAL-AD7942CB,用于评估 AD7942BRM 14 位、250 K 高分辨率 ADC 的评估板
- ADR525 高精度并联模式电压基准的典型应用
- NCV78763R1GEVK:NCV78763 套件
- MIC2025-1YM单通道配电开关MM8典型应用USB总线供电集线器应用
- DEVKIT-MOTORGD: DEVKIT平台的低成本电机控制解决方案