单片机蜂鸣器音乐

合集下载

单片机蜂鸣器播放音乐

单片机蜂鸣器播放音乐

#include <reg52.h>#define uchar unsigned char#define uint unsigned intsbit speaker = P1^7;uint j;uchar m=1;uchar flag;uchar line;uchar code * data song;// 休止符低6 低7 中1 中2 中3 中4 中5 中 6 中7 高 1 低 3 低 5 低 4 高3uchar code yin[30]={0xFF,0xFF,0xFB,0x90,0xFC,0x0C,0xFC,0x44,0xFC,0xAC,0xFD,0x09,0xFD,0x34,0xF D,0x82,0xFD,0xC8,0xFE,0x06,0xFE,0x22,0xFA,0X15,0XFB,0x04,0xFA,0x67,0xFE,0x85};uchar code song1[97]={0x34,0x32,0x32,0x34,0x42,0x51, //<<干杯,朋友>>0x62,0x52,0x42,0x32,0x34,0x04,0x74,0x74,0x62,0x62,0x64,0x3c,0x04,0x64,0x62,0x52,0x42,0x32,0x34,0x33,0x31,0x32,0x72,0x76,0x72,0x83,0x81,0x82,0x82,0x82,0x74,0x72,0x7c,0x04,0x63,0x61,0x62,0x62,0x64,0x72,0x82,0x72,0x74,0x72,0x62,0x52,0x42,0x32,0x42,0x44,0x42,0x42,0x52,0x62,0x52,0x5c,0x04,0x64,0x62,0x62,0x64,0x72,0x82,0x72,0x74,0x72,0x62,0x52,0x42,0x32,0x42,0x46,0x53,0x41,0x42,0x32,0x3c,0x04,0x44,0x48,0x02,0x32,0x3f,0x44,0x48,0x02,0x32,0x3f,0x34,0x0c,0xFF};uchar code song2[46]={0x12,0x52,0x52,0x52,0x56,0x42, //<<兰花草>>0x32,0x42,0x32,0x22,0x18,0x82,0x82,0x82,0x82,0x86,0x72,0xB2,0x72,0x72,0x62,0x58,0x52,0x82,0x82,0x72,0x56,0x42,0x32,0x42,0x32,0x22,0x16,0xB2,0xB2,0x32,0x32,0x22,0x16,0x52,0x42,0x32,0x22,0xC2,0x88,0xFF};uchar code song3[131]={0x52,0x42, //<<两只蝴蝶>>0x58,0x02,0x42,0x52,0x42,0x38,0x04,0x12,0x32,0x44,0x42,0x52,0x42,0x32,0x12,0x12,0xC8,0x04,0x52,0x42,0x58,0x02,0x42,0x52,0x42,0x38,0x04,0x12,0x32,0x44,0x42,0x52,0x42,0x32,0x12,0x32,0x48,0x04,0x52,0x42,0x58,0x02,0x42,0x52,0x42,0x38,0x04,0x12,0x32,0x44,0x42,0x52,0x42,0x32,0x12,0x11,0x31,0xC8,0x04,0x52,0x72,0x78,0x02,0x72,0x82,0x72,0x58,0x04,0x42,0x42,0x44,0x42,0x52,0x42,0x32,0x12,0x12,0x32,0x32,0x3C,0x09,0x72,0x72,0x82,0xA2,0x92,0x92,0x82,0x52,0x42,0x42,0x42,0x58,0x02,0x52,0x52,0x72,0x84,0x84,0x02,0x12,0x52,0x42,0x48,0x04,0x52,0x72,0x72,0x52,0x74,0x02,0xA4,0x92,0x82,0x92,0x54,0x02,0x82,0x82,0x92,0x82,0x72,0x52,0x42,0x02,0xC4,0x12,0x3C,0xFF};uchar code song4[37]={0xC4,0x12,0x32,0x44,0x42,0x52, //<<祈祷>>0x52,0x44,0x32,0x32,0x12,0x14,0x12,0x12,0x32,0x42,0x54,0x44,0x4F,0xC4,0x12,0x32,0x44,0x42,0x72,0x58,0x42,0x32,0x34,0x14,0x12,0x32,0x34,0x32,0x12,0x1F,0xFF};uchar code song5[144]={0x52,0x62, //<<阳光总在风雨后>>0x74,0x74,0x74,0x34,0x42,0x34,0x52,0x56,0x42,0x34,0x34,0x14,0x32,0x12,0x12,0xC2,0xC4,0x04,0x52,0x62,0x74,0x74,0x74,0x34,0x42,0x34,0x52,0x56,0x42,0x34,0x32,0x12,0x12,0x34,0x42,0x4C,0x52,0x42,0x34,0x32,0x42,0x32,0x12,0x12,0xC2,0xC2,0x54,0x52,0x54,0x52,0x52,0x42,0x32,0x32,0x12,0x32,0x42,0x32,0x52,0x5C,0x52,0x42,0x34,0x32,0x42,0x32,0x12,0x12,0xC2,0xC2,0x54,0x52,0x54,0x02,0x52,0x52,0x42,0x32,0x12,0x32,0x84,0x72,0x7F,0x0F,0x74,0x72,0x52,0x54,0x74,0x82,0x52,0x52,0x72,0x78,0x34,0x32,0x32,0x42,0x54,0x72,0x7F,0x86,0x82,0x72,0x52,0x54,0x72,0x84,0x32,0x36,0x52,0x42,0x52,0x42,0x32,0x34,0x12,0x32,0x32,0x42,0x4C,0x86,0x82,0x72,0x52,0x54,0x72,0x84,0x52,0x56,0x52,0x42,0x52,0x72,0x82,0x52,0x34,0x12,0x12,0x32,0x3C,0xFF};uchar code song6[85]={0x04,0x24,0x14,0xC4, //<<心心相印>>0x54,0x54,0x48,0x04,0x54,0x44,0x34,0x22,0x14,0xB2,0xC8,0x04,0x12,0xC2,0x16,0x12,0x76,0x52,0x46,0x12,0x3F,0x04,0xB4,0xD4,0xC4,0x14,0x28,0xC4,0x14,0x12,0x32,0x16,0xC2,0x54,0x78,0x34,0x4C,0x42,0x52,0x86,0x72,0x76,0x52,0x74,0x28,0x42,0x52,0x46,0x12,0x26,0x12,0xCF,0x04,0x24,0x14,0xC4,0x56,0x52,0x48,0x04,0x54,0x44,0x34,0x22,0x14,0xB2,0xC8,0x04,0x34,0x14,0xC4,0x12,0x32,0x18,0x42,0x42,0x74,0x58,0x44,0x3F,0xFF};uchar code song7[138]={0xC2,0xC2,0x12, //<<最浪漫的事>>0x34,0x32,0x32,0x32,0x12,0x12,0x52,0x54,0x02,0xC2,0x12,0x32,0x32,0x32,0x32,0x34,0x52,0x72,0x7C,0x02,0x72,0x82,0x72,0x82,0x72,0x84,0x72,0x41,0x51,0x52,0x41,0x31,0x34,0x02,0x32,0x32,0x12,0x34,0x32,0x12,0x32,0x54,0x42,0x32,0x32,0x32,0x32,0x34,0x52,0x72,0x7C,0x72,0x72,0x82,0x72,0x82,0x72,0x84,0x82,0x72,0x42,0x52,0x42,0x34,0x12,0x12,0x32,0xC4,0x52,0x42,0x34,0x12,0x12,0x31,0x41,0x32,0x3C,0x0A,0x72,0x82,0xA2,0x84,0x82,0x72,0x82,0x72,0x52,0x72,0x78,0x02,0x72,0x82,0xA2,0x84,0x82,0x72,0x82,0x72,0x82,0x32,0x38,0x02,0x32,0x42,0x52,0x64,0x62,0x72,0x82,0x82,0x72,0x82,0x82,0xA2,0x82,0x82,0x82,0xA2,0x82,0x72,0x78,0x72,0x32,0x82,0x72,0x78,0x72,0x82,0xA3,0x51,0x42,0x51,0x41,0x3C,0xFF};uchar code song8[77]={0xC2,0xC2,0x34,0x42,0x42, //<<一生有你>>0x52,0x41,0x51,0x5C,0x04,0xC2,0xC2,0x34,0x42,0x42,0x52,0x31,0x41,0x4C,0x04,0xC2,0xC2,0x34,0x42,0x42,0x52,0x41,0x51,0x58,0x32,0x52,0x4C,0x42,0x32,0x34,0x32,0x42,0x48,0x04,0xC2,0xC2,0x34,0x42,0x42,0x72,0x41,0x51,0x5C,0x04,0xC2,0xC2,0x32,0x32,0x42,0x42,0x52,0x42,0x4C,0x04,0xC2,0xC2,0x34,0x42,0x42,0x52,0x41,0x51,0x58,0x32,0x52,0x4C,0x42,0x32,0x32,0x42,0x4C,0xFF};uchar code song9[]={0X54,0XC4,0X12,0X32,0X34, //<<宁夏>>0X52,0X52,0X52,0X52,0X42,0X32,0X34,0XC1,0XC2,0X52,0XC2,0X12,0X32,0X34,0X11,0X12,0X31,0X12,0XC2,0X78,0X72,0X52,0X52,0X42,0X32,0X42,0X52,0X11,0XC1,0XC4,0XC1,0XC1,0XC1,0XC1,0X12,0X32,0X32,0X12,0X42,0X42,0X44,0XC2,0X12,0X32,0X42,0X52,0X72,0X72,0X82,0X41,0X51,0X42,0X48,0X42,0X12,0X3C,0X72,0X62,0X3C,0X72,0X62,0X3C,0X72,0X62,0x21,0x31,0x41,0x51,0x61,0x71,0x81,0x91,0x61,0x71,0x81,0x91,0X3F,0X3F,0X54,0XC2,0XC2,0X42,0XC2,0XC4,0X34,0X32,0X42,0X52,0XC2,0XC4,0XD2,0XC2,0X12,0X22,0X38,0X12,0X22,0X32,0X42,0X48,0xFF};uchar code song10[]={0x32,0x31,0x41, //<<老鼠爱大米>>0x52,0x52,0x42,0x31,0x41,0x44,0x31,0x41,0x52,0x52,0x42,0x32,0x34,0x71,0x11,0x74,0x71,0x71,0x11,0x31,0x34,0x32,0x32,0x32,0x32,0x31,0x41,0x44,0x32,0x41,0x51,0x52,0x52,0x72,0x81,0x41,0x44,0x52,0x41,0x31,0x32,0x32,0x42,0x51,0x71,0x74,0x71,0x71,0x12,0x31,0x31,0x31,0x52,0x42,0x32,0x32,0x41,0x31,0x34,0x52,0x51,0x71,0x72,0x71,0x71,0x72,0x71,0x81,0x84,0x52,0x42,0x32,0x32,0x32,0x41,0x51,0x54,0x51,0x71,0x72,0x72,0x72,0x81,0xA1,0xA2,0x82,0x72,0x52,0x42,0x32,0x32,0x12,0x44,0x51,0x71,0x72,0x72,0x71,0x82,0x71,0x84,0x52,0x42,0x32,0x32,0x32,0x41,0x51,0x54,0x51,0x71,0x72,0x72,0x72,0x81,0xA1,0xA2,0x82,0x72,0x51,0x41,0x42,0x42,0x44,0x32,0x51,0x61,0x72,0x42,0x44,0x52,0x41,0x31,0x34,0x52,0x41,0x31,0x34,0x52,0x42,0x52,0x32,0x52,0x81,0x71,0x74,0x51,0x71,0x82,0x81,0x71,0x72,0x81,0x71,0x74,0x51,0x41,0x31,0x42,0x41,0x51,0x42,0x31,0x41,0x44,0x52,0x41,0x31,0x34,0x52,0x41,0x31,0x34,0x52,0x42,0x52,0x32,0x52,0x81,0x71,0x74,0x51,0x41,0x82,0x82,0xA2,0x71,0x81,0x72,0x71,0x51,0x41,0x31,0x44,0x41,0x31,0x41,0x51,0x44,0x52,0x52,0x42,0x31,0x34,0x52,0x41,0x31,0x42,0x31,0x34,0xFF};uchar code song11[]={0XC2,0XB2, //<<泰坦尼克号>>0X36,0X52,0X42,0X32,0X11,0XC1,0XB2,0XC6,0X12,0XC4,0X12,0XC2,0X76,0XA2,0X82,0X72,0X51,0X41,0X32,0X4C,0X42,0X32,0X74,0X82,0X74,0X82,0X72,0X52,0X41,0X38,0X32,0X42,0X16,0XC2,0X72,0X52,0X42,0X42,0X3C,0XC2,0XB2,0X36,0X52,0X42,0X32,0X11,0XC1,0XB2,0XC6,0X12,0XC4,0X12,0XC2,0X76,0XA2,0X82,0X72,0X51,0X41,0X32,0X4C,0X42,0X32,0X76,0X82,0X74,0X82,0X72,0X53,0X41,0X38,0X32,0X42,0X16,0XC2,0X72,0X52,0X41,0X51,0X12,0XCC,0XC2,0XB2,0X16,0XC2,0X72,0X52,0X42,0X42,0X3C,0X32,0X32,0X26,0X22,0X24,0X42,0X32,0x22,0x12,0x18,0x02,0x52,0X46,0X12,0X44,0X72,0X82,0X5C,0X52,0X52,0X46,0X42,0X74,0X42,0X32,0X36,0X42,0X58,0X82,0X72,0X62,0X52,0X46,0X32,0X3C,0X12,0XB2,0X36,0X52,0X42,0X32,0X11,0XC1,0XB2,0XC6,0X12,0XC4,0X12,0XC2,0X76,0XA2,0X82,0X72,0X51,0X41,0X32,0X48,0X02,0X42,0X52,0X62,0X76,0X82,0X74,0X82,0X72,0X56,0X41,0X38,0X32,0X22,0X16,0XC2,0X72,0X52,0X42,0X42,0X3C,0X04,0X04,0X04,0X04,0xFF};uchar code song12[]={0x34,0x32, //<<喜欢你>>0x34,0x34,0x24,0x34,0x32,0x32,0x24,0x38,0x44,0x58,0x48, 0x34,0x32,0x34,0x34,0x24,0x38,0x34,0x7F,0x34,0x32,0x34,0x32,0x24,0x38,0x34,0x24,0x38,0x44,0x58,0x48,0x34,0x32,0x34,0x34,0x24,0x38,0x34,0x7F,0x16,0x4C,0x74,0x78,0x64,0x54,0x48,0x54,0x64,0x58,0x44,0x34,0x24,0x38,0x24,0x14,0x12,0x21,0x11,0x78,0x68,0x3F,0x4C,0x74,0x78,0x64,0x52,0x42,0x48,0x54,0x64,0x58,0x44,0x34,0x24,0x38,0x24,0x24,0x38,0x44,0x58,0x48,0x3C,0xFF};//**********************************************************************//// 延时程序////**********************************************************************//void delay(uint d) //延时d微秒,最大延时65536微妙{while(d--);}/*延时1/4拍时间即187ms*/void delay187ms(void){uchar t;for(t=0;t<130;t++) delay(100);}void main(){uchar i;uchar k;delay(100);TMOD=0x01;IT0=1;EX0=1;TR0=1;ET0=1;EA=1;song=song1;while(1){switch(m){case 1 :song=song1;break;case 2 :song=song2;break;case 3 :song=song3;break;case 4 :song=song4;break;case 5 :song=song5;break;case 6 :song=song6;break;case 7 :song=song7;break;case 8 :song=song8;break;case 9 :song=song9;break;case 10 :song=song10;break;case 11 :song=song11;break;case 12 :song=song12;break;default :m=1;song=song1;break;}j=0;flag=1;while(*(song+j)!=0xFF&&flag){k=*(song+j)&0x0F; //取出节拍line=*(song+j)>>4; //取出音TH0=yin[2*line];TL0=yin[2*line+1];TR0=1;if ((yin[2*line]==0xFF)&&(yin[2*line+1]==0xFF)){TR0=0;}for(i=k;i>0;--i){delay187ms();}TR0=0;j++;}for(i=0;i<20;i++)delay187ms();m++;}}/* 每个音符的发声*/void timer1() interrupt 1{TH0=yin[2*line];TL0=yin[2*line+1];speaker = !speaker;}void int0() interrupt 0{flag=0;TR0=0;}。

单片机蜂鸣器唱歌程序(二)2024

单片机蜂鸣器唱歌程序(二)2024

单片机蜂鸣器唱歌程序(二)引言概述:本文档主要介绍了单片机蜂鸣器唱歌程序(二),包括使用单片机控制蜂鸣器发出不同音乐的方法和具体实现步骤。

本文将从五个大点进行阐述,每个大点包含5-9个小点,以便读者更好地理解和实践。

正文:一、引脚连接设置1. 确定单片机的输出引脚和蜂鸣器的输入引脚2. 将单片机的输出引脚与蜂鸣器的输入引脚连接3. 确保连接的稳定性和正确性4. 利用电路图进行布线二、编程环境配置1. 安装适合单片机的编程软件2. 创建新的项目3. 配置单片机的型号及选项4. 导入相关的库文件5. 编写代码框架三、发声原理及代码实现1. 理解蜂鸣器工作原理2. 使用单片机的PWM输出功能控制蜂鸣器的频率3. 利用PWM输出的方式实现不同音调的发声4. 编写音调转换函数5. 编写歌曲的音乐片段代码四、优化和调试1. 测试不同频率的声音2. 调整蜂鸣器的音量3. 避免噪音的干扰4. 检查代码的正确性和合理性5. 不断尝试,优化代码和音效五、实验结果及总结1. 运行程序,测试蜂鸣器的唱歌效果2. 记录实验结果和观察结果3. 分析实验过程中遇到的问题和解决方法4. 总结实验经验和注意事项5. 展望将来的改进和研究方向总结:本文详细介绍了单片机蜂鸣器唱歌程序(二)的实现方法和步骤。

通过连接设置、编程环境配置、发声原理及代码实现、优化和调试、实验结果及总结等五个大点的阐述,读者可以深入了解单片机控制蜂鸣器发声的原理和方法,并通过实验得到具体的唱歌效果。

同时,读者在实践过程中也要注意优化和调试,不断尝试和改进,以实现更好的音效效果。

希望本文对读者有所帮助,为单片机蜂鸣器唱歌程序的开发提供了指导和参考。

51单片机蜂鸣器播放音乐代码

51单片机蜂鸣器播放音乐代码

/*生日快乐歌曲*/#include <>#define uint unsigned int#define uchar unsigned charsbit beep = P1^5;uchar code SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159, 212,212,106,126,159,169,190,119,119,126,159,142,159,0};uchar code SONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0};//延时void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}void PlayMusic(){uint i=0,j,k;while(SONG_LONG[i]!=0||SONG_TONE[i]!=0){ //播放各个音符,SONG_LONG 为拍子长度for(j=0;j<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE 延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}void main(){beep=0;while(1){PlayMusic(); //播放生日快乐DelayMS(500); //播放完后暂停一段时间}}两只蝴蝶:#include <> // 这是单片机音乐代码生成器生成的代码#define uchar unsigned charsbit beepIO=P1^5; // 输出为可以修改成其它 IO 口uchar m,n;uchar code T[49][2]={{0,0},{0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0 xB9},{0xFB,0x03},{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},{0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0 x5B},{0xFD,0x81},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},{0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0 xAD},{0xFE,0xC0},{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},{0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0 x56},{0xFF,0x60},{0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81}};uchar code music[][2]={{0,4},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8}, {21,4},{23,4},{21,4},{19,4},{16,4},{19,4},{14,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,24},{16,4},{19,4},{21,8}, {21,4},{23,4},{21,4},{19,4},{16,4},{19,4},{21,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8}, {21,4},{23,4},{21,4},{19,4},{16,4},{19,4},{14,24},{23,4},{26,4},{26,16},{26,4},{28,4},{26,4},{23,24},{21,4},{23,4},{21,8},{21,4}, {23,4},{21,4},{19,4},{16,4},{16,2},{19,2},{19,24},{0,20},{26,4},{26,4},{28,4},{31,4},{30,4},{30,4},{28,4},{23,4},{21,4},{21,4},{23,16},{ 0,4},{23,4},{23,4},{26,4},{28,8},{28,12},{16,4},{23,4},{21,4},{21,24},{23,4},{26,4},{26,4},{23,4},{26,8},{0,4},{31,8},{30,4},{28,4},{30,4},{2 3,8},{0,4},{28,4},{28,4},{30,4},{28,4},{26,4},{23,4},{21,8},{23,4},{21,4},{23,4 },{26,16},{0xFF,0xFF}};void delay(uchar p){uchar i,j;for(;p>0;p--)for(i=181;i>0;i--)for(j=181;j>0;j--);}void pause(){uchar i,j;for(i=150;i>0;i--)for(j=150;j>0;j--);}void T0_int() interrupt 1{beepIO=!beepIO;TH0=T[m][0]; TL0=T[m][1];}void main()uchar i=0;TMOD=0x01; EA=1; ET0=1;while(1){m=music[i][0];n=music[i][1];if(m==0x00){TR0=0;delay(n);i++;}else if(m==0xFF){TR0=0;delay(30);i=0;}else if(m==music[i+1][0]){TR0=1;delay(n);TR0=0;pause();i++;}else{TR0=1;delay(n);i++;}}}祝你平安:#include ""unsigned char Count;sbit _Speak =P1^5 ;unsigned char code SONG[] ={ //祝你平安0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20 ,0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20 ,0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20 ,0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20 ,0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10 ,0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20 ,0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20 ,0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20 ,0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10 ,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80 ,0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20 ,0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x200x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10 ,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00};void Time0_Init(){TMOD = 0x01;IE = 0x82;TH0 = 0xD8;TL0 = 0xEF; //12MZ晶振,10ms}void Time0_Int() interrupt 1{TH0 = 0xD8;TL0 = 0xEF;Count++; //长度加1}/*-------------------------------------------------功能:1MS延时子程序-------------------------------------------------*/void Delay_xMs(unsigned int x){unsigned int i,j;for( i =0;i < x;i++ ){for( j =0;j<3;j++ );}}void Play_Song(unsigned char i){unsigned char Temp1,Temp2;unsigned int Addr;Count = 0; //中断计数器清0Addr = i * 217;while(1){Temp1 = SONG[Addr++];if ( Temp1 == 0xFF ) //休止符{TR0 = 0;Delay_xMs(100);}else if ( Temp1 == 0x00 ) //歌曲结束符{return;}else{Temp2 = SONG[Addr++];TR0 = 1;while(1){_Speak = ~_Speak;Delay_xMs(Temp1);if ( Temp2 == Count ){Count = 0;break;}}}}}/*-------------------------------------------------功能:主程序-------------------------------------------------*/ void main(){Time0_Init(); //定时器0中断初始化while(1){Play_Song(0); //播放}}。

51单片机蜂鸣器播放音乐代码(生日快乐 两只蝴蝶 祝你平安)精编版

51单片机蜂鸣器播放音乐代码(生日快乐 两只蝴蝶 祝你平安)精编版

/*生日快乐歌曲*/#include <reg51.h>#define uint unsigned int#define uchar unsigned charsbit beep = P1^5;uchar code SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159, 212,212,106,126,159,169,190,119,119,126,159,142,159,0};uchar code SONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0};//延时void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}void PlayMusic(){uint i=0,j,k;while(SONG_LONG[i]!=0||SONG_TONE[i]!=0){ //播放各个音符,SONG_LONG 为拍子长度for(j=0;j<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE 延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}void main(){beep=0;while(1){PlayMusic(); //播放生日快乐DelayMS(500); //播放完后暂停一段时间}}两只蝴蝶:#include <reg51.h> // 这是单片机音乐代码生成器生成的代码#define uchar unsigned charsbit beepIO=P1^5; // 输出为P1.5 可以修改成其它IO 口uchar m,n;uchar code T[49][2]={{0,0},{0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0xB9},{0xFB,0x03 },{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},{0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0x5B},{0xFD,0x8 1},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},{0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0xAD},{0xFE,0xC0 },{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},{0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0x56},{0xFF,0x60},{ 0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81}};uchar code music[][2]={{0,4},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{14,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,24},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{21,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{14,24},{23,4},{26,4},{26,16},{26,4},{28,4},{26,4},{23,24},{21,4},{23,4},{21,8},{21,4},{23,4},{21,4},{19,4},{16 ,4},{16,2},{19,2},{19,24},{0,20},{26,4},{26,4},{28,4},{31,4},{30,4},{30,4},{28,4},{23,4},{21,4},{21,4},{23,16},{0,4},{23,4},{23,4},{26,4} ,{28,8},{28,12},{16,4},{23,4},{21,4},{21,24},{23,4},{26,4},{26,4},{23,4},{26,8},{0,4},{31,8},{30,4},{28,4},{30,4},{23,8},{0,4},{28,4},{28,4},{ 30,4},{28,4},{26,4},{23,4},{21,8},{23,4},{21,4},{23,4},{26,16},{0xFF,0xFF}};void delay(uchar p){uchar i,j;for(;p>0;p--)for(i=181;i>0;i--)for(j=181;j>0;j--);}void pause(){uchar i,j;for(i=150;i>0;i--)for(j=150;j>0;j--);}void T0_int() interrupt 1beepIO=!beepIO;TH0=T[m][0]; TL0=T[m][1];}void main(){uchar i=0;TMOD=0x01; EA=1; ET0=1;while(1){m=music[i][0];n=music[i][1];if(m==0x00){TR0=0;delay(n);i++;}else if(m==0xFF){TR0=0;delay(30);i=0;}else if(m==music[i+1][0]){TR0=1;delay(n);TR0=0;pause();i++;}else{TR0=1;delay(n);i++;}}}祝你平安:#include "reg52.h"unsigned char Count;sbit _Speak =P1^5 ;unsigned char code SONG[] ={ //祝你平安0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20, 0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20, 0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10, 0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20, 0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20, 0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20, 0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10, 0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80, 0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20, 0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20, 0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10, 0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00};void Time0_Init(){TMOD = 0x01;IE = 0x82;TH0 = 0xD8;TL0 = 0xEF; //12MZ晶振,10ms}void Time0_Int() interrupt 1{TH0 = 0xD8;TL0 = 0xEF;Count++; //长度加1}/*-------------------------------------------------功能:1MS延时子程序-------------------------------------------------*/ void Delay_xMs(unsigned int x){unsigned int i,j;for( i =0;i < x;i++ ){for( j =0;j<3;j++ );}}void Play_Song(unsigned char i){unsigned char Temp1,Temp2;unsigned int Addr;Count = 0; //中断计数器清0Addr = i * 217;while(1){Temp1 = SONG[Addr++];if ( Temp1 == 0xFF ) //休止符{TR0 = 0;Delay_xMs(100);}else if ( Temp1 == 0x00 ) //歌曲结束符{return;}else{Temp2 = SONG[Addr++];TR0 = 1;while(1){_Speak = ~_Speak;Delay_xMs(Temp1);if ( Temp2 == Count ){Count = 0;break;}}}}}/*------------------------------------------------- 功能:主程序-------------------------------------------------*/ void main(){Time0_Init(); //定时器0中断初始化while(1){Play_Song(0); //播放}}。

51单片机蜂鸣器播放单音节音乐

51单片机蜂鸣器播放单音节音乐
// 3_ 2_ 3 1
0xFD,0x23,0x7F, 0xFC,0xAC,0x7F, 0xFD,0x23,0xFF, 0xFC,0x44,0xFF,
// 1_ 3_ 2 .5
0xFC,0x44,0x7F, 0xFD,0x23,0x7F, 0xFC,0xAC,0xFF, 0xFA,0x68,0xFF,
{
for (i =0; i < 363; i++)
{;}
}
}
*名称:Init()
*功能:设置计数器0工作方式,16位计数,溢出中断方式
**********************************************************************************/
void Init()
{
TMOD = 0x01; //定时器0处于计时方式,16位
*名称:Count1(void) interrupt 1
*功能:设置计时器0溢出中断,每中断一次改变P2_3引脚电平
*********************************************************************************/
void Count1(void) interrupt 1
void main()
{
uchar time;
Init();
TH0 = high;
TL0 = low;
while (1)
{
if (music[ptr] != 0xFF && music[ptr] != 0x00)//判断是否是正常音符
{
TR0 = 0;
P2_3 = 1;

51单片机蜂鸣器奏乐(生日快乐)

51单片机蜂鸣器奏乐(生日快乐)
mov 30h,#00h ;取简谱码指针
next: mov a,30h ;简谱码指针载入A
mov dptr,#table ;至table取简谱码
movc a,@a+dptr
mov r2,a ;取到的简谱码暂存于R2
jz end0 ;是否取到00(结束码)?
anl a,#0fh ;不是,则取低4位(节拍码)
dw 64684,64777,64820,64898
dw 64968,65030,65058,65110
dw 65157,65178,65217
table:
;1
db 82h,01h,81h,94h,84h,0b4h,0a4h,04h,82h,01h,81h,94h,84h,0c4h,0b4h,04h
mov 22h,a ;存入(22H).
rl a ;乘2
mov dptr,#table1 ;至table1取相对的高位字节计数值
m高位字节存入TH0
mov 21h,a ;取到的高位字节存入(21H)
mov a,22h ;再载入取的的音符码
inc 30h ;取简谱码指针加1
jmp next ;取下一个码
end0: clr tr0 ;停止timer0
jmp start0 ;重复循环
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;time0中断服务子程序用来产生相应的音调脉冲频率。
;4
db 82h,01h,81h,0f4h,0d4h,0b4h,0a4h,94h,0e2h,01h,0e1h,0d4h,0b4h,0c4h,0b4h,04h,00

单片机课程设计报告利用蜂鸣器播放音乐

单片机课程设计报告利用蜂鸣器播放音乐

课程设计:嵌入式系统应用题目名称:利用蜂鸣器实现音乐播放功能姓名:学号:班级:完成时间:1设计的任务设计内容:动手焊接一个51单片机设计目标:利用单片机上的蜂鸣器实现音乐播放功能2设计的过程基本结构在本次的试验中采用了STC89C52RC单片机,STC89C52RC单片机是宏晶科技推出的新一代高速/低功耗/超强抗干扰的单片机,指令代码完全兼容传统8051单片机,12时钟/机器周期,工作电压:~5V单片机/~3V单片机,工作频率范围:0~40MHz,相当于普通8051的0~80MHz,实际工作频率可达48MHz,用户应用程序空间为8K字节;STC89C52RC引脚图STC89C52RC单片机的工作模式:(1)典型功耗<μA,可由外部中断唤醒,中断返回后,继续执行原程序(2)空闲模式:典型功耗2mA(3)正常工作模式:典型功耗4Ma~7mA4唤醒,适用于水表、气表等电池供电系统及便携设备2.蜂鸣器及其工作原理:蜂鸣器按其结构分主要分为压电式蜂鸣器和电磁式蜂鸣器两种类型;电磁式蜂鸣器由振荡器、电磁线圈、磁铁、振动膜片及外壳等组成;接通电源后,振荡器产生的音频信号电流通过电磁线圈,使电磁线圈产生磁场,振动膜片在电磁线圈和磁铁的相互作用下,周期性地振动发声;本实验采用的是电磁式蜂鸣器;蜂鸣器按其是否带有信号源又分为有源和无源两种类型;有源蜂鸣器只需要在其供电端加上额定直流电压,其内部的震荡器就可以产生固定频率的信号,驱动蜂鸣器发出声音;无源蜂鸣器可以理解成与喇叭一样,需要在其供电端上加上高低不断变化的电信号才可以驱动发出声音;本实验采用的是有源蜂鸣器;蜂鸣器与单片机连接电路图软件设计过程1.蜂鸣器发声原理本实验由于采用有源蜂鸣器,只需将引脚端口P3^4清零,蜂鸣器即可发声;P3^4置位,蜂鸣器停止发声;采用置1置0的方法只能使蜂鸣器发声或停止发声,想要使蜂鸣器发出声音,必须对蜂鸣器发出声音的音频和节拍进行控制;音乐基础音调:不同音高的乐音是用C、D、E、F、G、A、B来表示,这7个字母就是音乐的音名,它们一般依次唱成DO、RE、MI、FA、SO、LA、SI,即唱成简谱的1、2、3、4、5、6、7,相当于汉字“多来米发梭拉西”的读音,这是唱曲时乐音的发音,所以叫“音调”,即Tone;把C、D、E、F、G、A、B这一组音的距离分成12个等份,每一个等份叫一个“半音”;两个音之间的距离有两个“半音”,就叫“全音”;在钢琴等键盘乐器上,C–D、D–E、F–G、G–A、A–B两音之间隔着一个黑键,他们之间的距离就是全音;E–F、B–C两音之间没有黑键相隔,它们之间的距离就是半音;通常唱成1、2、3、4、5、6、7的音叫自然音,那些在它们的左上角加上﹟号或者b号的叫变化音;﹟叫升记号,表示把音在原来的基础上升高半音,b叫降记音,表示在原来的基础上降低半音;例如高音DO的频率1046Hz刚好是中音DO的频率523Hz的一倍,中音DO的频率523Hz刚好是低音DO频率266Hz的一倍;同样的,高音RE 的频率1175Hz刚好是中音RE的频率587Hz的一倍,中音RE的频率587Hz 刚好是低音RE频率294Hz的一倍;节拍:节拍是让音乐具有旋律固定的律动,而且可以调节各个音的快满度;“节拍”,即Beat,简单说就是打拍子,就像我们听音乐不自主的随之拍手或跺脚;若1拍实,则1/4拍为;至于1拍多少s,并没有严格规定,就像人的心跳一样,大部分人的心跳是每分钟72下,有些人快一点,有些人慢一点,只要听的悦耳就好;音持续时间的长短即时值,一般用拍数表示;休止符表示暂停发音;1)控制发声频率要产生音频脉冲,只要算出某一音频的周期1/频率,然后将此周期除以2,即为半周期的时间;利用定时器计时这半个周期时间,每当计时到后就将输出脉冲的I/O反相,然后重复计时此半周期时间再对I/O反相,就可在I/O 脚上得到此频率的脉冲;利用STC89C52RC的内部定时器使其工作在计数器模式MODE1下,改变计数值TH0及TL0从而产生不同频率;此外结束符和休止符可以分别用代码00H和FFH来表示,若查表结果为0x00,则表示曲子终了;若查表结果为0xff,则产生相应的停顿效果;以标准音高A为例,A的频率是440Hz,周期T=1/440=2272us;在占空比为50%的情况下,导通时间=断开时间=半周期t=2272us/2=1136us,利用P3^4端口的位操作,经过不断地反相变换即可得到标准音高A的音频脉冲;端口导通时间与断开时的时间利用定时器实现;具体的方法是将单片机定时器的中断触发时间设为半周期t,这样每隔半周期端口反相,输出连续的对应音高的频率;设晶振的频率为f0,中断触发时间半周期为t,定时器工作在模式1时计数器的初值为THL,高8位为THL,低8位为TL;时钟周期即为1/f0,定时器每一次累加用去一个机器周期,一个机器周期包含12个时钟周期,即定时器每次加一所用时间是12/f0;定时器在模式1下计时采用16位数,最大计数为2^16-165535,再次加一65536溢出触发中断;根据以上分析可得如下关系:音频对应定时器初值的高8位TH=THL/2^8=65536-tf/12/256;音频对应定时器初值的低8位TL=THL%2^8=65536-tf/12%256;附:八度12音阶定时器初值表只含自然音低音音名 频率Hz 晶振12MHz 晶振 中音音名 频率Hz 晶振12MHz 晶振 高音音名 频率Hz 晶振12MHz 晶振 Do 262 0xF885 0xF91B Do 523 0xFC43 0xFC8E Do 1046 0xFE21 0xFE47 Re 294 0xF95A 0xF9DF Re 587 0xFCAD 0xFCED Re 1175 0xFE56 0xFE76 Mi 330 0xFA13 0xFA8A Mi 659 0xFD0A 0xFD43 Mi 1318 0xFE85 0xFEA1 Fa 349 0xFA68 0xFAD8 Fa 698 0xFD34 0xFD6A Fa 1397 0xFE9A 0xFEC7 So 392 0xFB04 0xFB68 So 784 0xFD82 0xFDB3 So 1568 0xFEC1 0xFED9 La 440 0xFB90 0xFBE9 La 880 0xFDC8 0xFDF3 La 1760 0xFEE4 0xFEF9 Si4940xFC0C0xFC5BSi9880xFE060xFE2DSi19760xFF030xFF162控制发声节拍每个音符的节拍可通过延时一定的时间来实现,在具体实现时需要有一个基本的带参延时程序,用于主函数根据不同的音符调用不同的时延;若以十六分之一音符的时长为基本延时时间,则十六分音符只需调用一次延时程序,八分音符则需调用两次延时程序,以此类推;3) 简谱及其编码用数字1至7表示;这7个数字就等于的;如果是C 大调,加上音名,就会是这样:八度如果是高一个八度,就会在数字上方加上一点;如果是低一个八度,就会数字下方加上一点;在中间的那一个就什么也不用加;如果要再高一个八度,就在上方垂直加上两点如:;要再低一个八度,就在下方垂直加上两点如:,如此类推;见下表所示: 大调 ... ... 小调......音长通常只有数字的是;数字下加一条横线,就可令四分音符的长度减半,即成为;两条横线可令八分音符的长度减半,即成为,余此类推;简单来説,下音阶CDEFGAB唱名 do re mi fa so la si数字 1 2 3 4 5 6 7加横綫数目与五綫谱的符尾数目相对应;数字后方的横线延长音符,每加一条横线延长一个的长度;正如五綫谱的附点一样,数字后方加一点会将音符长度增加一半;休止符休止符用“0”来表示;比四分休止符长,就只需在每一个四分休止符再多加一个“0”,不需在后方加上横线;若整个小节均是休止状态的话,就在每个四分音符加一个“0”,就像是3拍4就写成“|000|”,而4拍4就写成“|0000|”;无音高音符就像敲击乐的乐器那样是没有音高的,这时,简谱就需要用“X”或“x”表达;例如拍手时,简谱上就需如此表达:小节线正如五綫谱一样,小节与小节之间以纵綫称分隔,乐曲终止以一粗一细的直綫表示但是一般不分粗细;重复乐段以纵綫后两点表示开始,纵綫前两点表示终止;拍号的表达方式为:“2/4”、“3/4”、“4/4”、“6/8”等等;拍号一般都是置于的后方;变调与调号表示有两种方法:可以直接写“C调”,“C小调”或“Key:C”,“Key:Cm”;亦可写清楚主音代表哪一个音,例如1=C,就是C大调大调以1为主音;6=C就是C小调小调以6为主音;2=D就是D多利安调以2为主音;五线谱有,简谱同样有升降号;在简谱中,若需表达升降号,就把符号加在数字1至7的前方,让该音升高或降低;若要把升降号加在调号跟CDEFGAB七个字母,就须加在它们的后方;在五綫谱中,C小调要把B-E-A三个音降低半音;所以表达它的和声小调时,就会把一个放在B前面;简谱就一定得写 5,因为5不能下降;延音线、圆滑线与其他音乐符号简谱Tie跟slur都是跟五线谱相同,一定得置在数字上方;表达staccato时需要特别小心,因为很容易会跟低八度的那一点混淆,因此在表达跳音时把那一点写得粗点及低点;简谱编码将简谱中的每个音符进行编码,每个音符用一个unsignedchar字符类型表示,简谱可用一个unsignedchar字符数组表示;字符的前四位表示音频,可以表示0-f共十六个音符;本实验中采用了中音区和高音区;中音do-si分别编码为1~7,高音do-si分别编码为8~E,停顿编为0;字符的后四位表示节拍,节拍以十六分音符为单位在本程序中为165ms,一拍即四分音符等于4个十六分音符,编为4,其它的播放时间以此类推;以0xff作为曲谱的结束标志;程序从数组中取出一个数,然后分离出高4位得到音调,将值赋给定时器0,得到音调;接着分离出该数的低4位,得到节拍;本实验中播放音乐使用简谱如下:将其编码成:ucharcodezbjbxf={0x64,0xA3,0xC1,0xA2,0x62,0x84,0x61,0x81,0x61,0xA4,0xA1,0x91,0xA1,0x81,0 x64,0xA3,0xC1,0xD2,0xD2,0xD2,0xA2,0xC4,0xA1,0xC1,0xA1,0xC1,0xD2,0xD2,0x D2,0xA2,0xC4,0xC2,0x62,0xC2,0x62,0xA2,0xA2,0x84,0x94,0x94,0x92,0x81,0x9 1,0xA2,0xC2,0xD4,0xE4,0xA4,0xE4,0xA2,0xE2,0xA2,0xE2,0xA2,0xA2,0x84,0x94 ,0x94,0x92,0x81,0x91,0xA2,0xC2,0xD8};程序流图及说明主程序流程图程序代码:include<> defineucharunsignedchardefineuintunsignedintsbitfm=P3^4;算机与单片机的连接不上问题计算机与单片机的连接需要在电脑端安装相应的USB转串的驱动,由于我组没有使用实验室windowsxp系统,而使用的是个人计算机windows7系统,所以需要针对win7的驱动;后来联网搜索下载相应的驱动之后解决了这一问题;2.如何使蜂鸣器发出声音而非Be-Be声为了解决这个问题,我们深入研究了蜂鸣器发声原理与乐谱相关的知识;通过学习相关知识发现,不同的音符对应不同的音高,音高取决于发声频率;所以可以通过构造不同频率的方波来使蜂鸣器发出不同频率的声音,具体实现时利用定时器计时,定时器每一次累加消耗一个机器周期,即12个时钟周期;本实验采用晶振频率为12MHz,即定时器每一次累加消耗1us,通过设置定时器初值设定蜂鸣器取法的时间间隔,从而产生相应的音频;每个音符不但有频率属性,还有节拍属性;对不同节拍的控制可以采用延迟一定的时间来得到; 5总结在本次课程设计中,我们进一步加深了对小型嵌入式系统的认识;实验初期单片机需要自己焊接,通过亲手焊接电路板,了解了如何将实验原理图转化为具体的硬件实物连接;通过使用TN单片机开发板和STC89C52RC系统,对单片机应用开发有了初步的熟悉;在实验的过程中,使用到了C51的编译环境KeilC和单片机通讯程序STC_ISP,两者是单片机开发所不可缺少的工具;在具体实施的过程中,通过一一解决遇到的问题,增强了动手实践能力;。

单片机驱动蜂鸣器音乐生日快乐的播放程序

单片机驱动蜂鸣器音乐生日快乐的播放程序

单片机驱动蜂鸣器音乐生日快乐的播放程序;音乐生日快乐的播放;P1.6是喇叭输出端口,用杜邦线连接到J42插针 B1针脚,喇叭即播放音乐numtim equ 20hgewei equ 21hshiwei equ 22hscanled equ 23horg 000hjmp startorg 00bhjmp tim0org 100hstart:; JB P3.7,$ ;检测播放按键是否按下mov tmod,#00000001bmov ie,#10000010bmov numtim,#01h start0:mov 30h,#00hnext:mov a,30hmov dptr,#tablemovc a,@a+dptrmov r2,ajz end0anl a,#0fhmov r5,amov a,r2swap aanl a,#0fhjnz singclr tr0jmp d1sing: dec amov 22h,arl amov dptr,#table1movc a,@a+dptrmov th0,amov 21h,amov a,22hrl ainc amovc a,@a+dptrmov tl0,amov 20h,asupplies and get to use, the registration of company assets, record maintenance, maintenance of effective management; 2.6 cost management: preparation of the annual administrative budget, control administrative costs, determine the scope of cost-sharing, sharing the office expenses by month; 2.7 information management: responsible for documents,materials, information and publicity, communication links, and ensure promptly, ranking officers reporting; responsible for the collation and analysis of information, provide the basis for corporate decisions; corporate culture construction, planning, editing, internal publications;2.8 vehicle management: responsible for the management, repairs and maintenance of vehicles; 2.9 legal disputes: the company responsible for the handling of disputes; 2.10 other: maintaining normal working order, and security. General Manager General Manager job description 1, 1.1 develop strategies and plans: according to company strategic planning, establish the annual management plan, reported to the Board of Directors for approval; 1.2 decomposition measures: according to the Board of Directors issued the annual management plan, annual index of decomposition, through the Organization of coordination mandate incharge of the Department Organization and implementation, ensuring comprehensive implementation of annual indicators; 1.3 inspection: convenes the General Manager Office, throughsetb tr0d1:lcall delayinc 30hjmp nextend0:clr tr0jmp start0tim0:push accpush pswmov tl0,20hmov th0,21hcpl p1.6 ;由该口输出音频数据pop pswpop accretidelay: mov r7,#02hd2: mov r4,#187d3: mov r3,#248djnz r3,$djnz r4,d3djnz r7,d2djnz r5,delayrettable1: ;音乐码表dw 64260,64400,64524,64580 dw 64684,64777,64820,64898 dw 64968,65030,65058,65110 dw 65157,65178,65217table:db 82h,01h,81h,94h,84h db 0b4h,0a4h,04hdb 82h,01h,81h,94h,84hdb 0c4h,0b4h,04hdb 82h,01h,81h,0f4h,0d4hdb 0b4h,0a4h,94hdb 0e2h,01h,0e1h,0d4h,0b4hdb 0c4h,0b4h,04hdb 82h,01h,81h,94h,84hdb 0b4h,0a4h,04hdb 82h,01h,81h,94h,84hdb 0c4h,0b4h,04h supplies and get to use, the registration of company assets, record maintenance, maintenance of effective management;2.6 cost management: preparation of the annual administrative budget, control administrative costs, determine the scope of cost-sharing, sharing the office expenses by month; 2.7 information management: responsible for documents, materials, information and publicity, communication links, and ensure promptly, ranking officers reporting; responsible for the collation and analysis of information, provide the basis for corporate decisions; corporate culture construction, planning, editing, internal publications; 2.8 vehicle management: responsible for the management, repairs and maintenance of vehicles; 2.9 legal disputes: the company responsible for the handling of disputes; 2.10 other: maintaining normal working order, and security. General Manager General Manager job description 1, 1.1 develop strategies and plans: according to company strategic planning, establish the annual management plan, reported to the Board of Directors for approval; 1.2 decomposition measures: according to the Board of Directors issued the annual management plan, annual index of decomposition, through the Organizationof coordination mandate in charge of the Department Organization and implementation, ensuring comprehensive implementation of annual indicators; 1.3 inspection: convenes the General Manager Office, through db 82h,01h,81h,0f4h,0d4hdb 0b4h,0a4h,94hdb 0e2h,01h,0e1h,0d4h,0b4hdb 0c4h,0b4h,04hdb 00TABLE2:DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90Hend另外的一个C文件。

51单片机__蜂鸣器音乐——梁祝

51单片机__蜂鸣器音乐——梁祝

51单片机__蜂鸣器音乐——梁祝单片机实训-----蜂鸣器一、设计思路学习案例中的实例程序,通过对程序的修改,使蜂鸣器能发出音乐二、程序代码#includesbit speaker = P3^6;unsigned char timer0h, timer0l, time;//--------------------------------------//单片机晶振采用11.0592MHz// 频率-半周期数据表高八位本软件共保存了四个八度的28个频率数据code unsigned char FREQH[] = {0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8, //低音12345670xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC,//1,2,3,4,5,6,7,i 0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, //高音2345670xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF}; //超高音1234567// 频率-半周期数据表低八位code unsigned char FREQL[] = {0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6, //低音12345670x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D, //高音2345670x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16}; //超高音1234567 //--------------------------------------/*unsigned char code sszymmh[] = {3,2,3,5,2,1,6,2,2,5,2,1,6,2,1,1,3,4,6,2,3,1,3,1,3,2,2,2,2,1,3,2,1,5,2,4,5,2,3,3,2,1,6,2,2,3,2,1,2,2,1,1,2,4,5,2,3,3,2,1,2,2,3,6,1,1,1,2,4,1,2,4,1,3,3,6,2,1,5,2,2,6,2,1,6,2,1,5,2,1,5,2,4,5,2,3,6,2,1,1,2,1,2,2,1,5,2,1,3,2,4,2,2,3,3,2,1,5,2,2,1,3,2,6,2,1,5,2,1,3,2,1,2,2,1,1,2,4,6,2,3,1,3,1,5,2,1,3,2,1,2,2,2,6,1,1,1,2,1,1,2,4,6,2,3,1,3,1,5,2,1,3,2,1,2,2,2,1,2,4,6,2,3,1,3,1,5,2,1,3,2,1,2,2,2,6,1,1,1,2,1,1,2,4,6,2,3,1,3,1,5,2,1,3,2,1,2,2,2,6,1,1,1,2,1,1,2,4,0, 0, 0}*/unsigned char code sszymmh[] = {1,2,3,2,2,1,6,1,1,1,2,1,5,1,2,5,2,3,1,3,1,6,2,1,5,2,1,3,2,1,5,2,1,2,2,4,2 ,2,3,3,2,1,7,1,2,6,1,2,5,1,3,6,1,1,1,2,2,2,2,2,3,1,2,1,2,2,6,1,1,5,1,1,6,1,1,1,2,1,5,1 ,4,3,2,3,5,2,1,7,1,2,2,2,2,6,1,1,1,2,1,5,1,4,3,1,1,5,1,2,3,1,2,5,1,1,6,1,1,7,1,1,2,2,1,6,1,4,5,1, 1,6,1,1,1,2,3,2,2,1,5,2,2,3,2,2,2,2,2,3,2,1,2,2,1,1,2,2,6,1,1,5,1,1,3,1,2,3,1,4,1,2,4,6,1,3, 1,2,1,6,1,1,5,1,1, 3,1,1,5,1,1,6,1,1,1,2,1,5,1,4,0, 0, 0};//梁祝void t0int() interrupt 1 //T0中断程序,控制发音的音调{TR0 = 0; //先关闭T0speaker = !speaker; //输出方波, 发音TH0 = timer0h; //下次的中断时间, 这个时间, 控制音调高低TL0 = timer0l;TR0 = 1; //启动T0}//--------------------------------------void delay(unsigned char t) //延时程序,控制发音的时间长度{unsigned char t1;unsigned long t2;for(t1 = 0; t1 < t; t1++) //双重循环, 共延时t个半拍for(t2 = 0; t2 < 8000; t2++); //延时期间, 可进入T0中断去发音TR0 = 0; //关闭T0, 停止发音}//--------------------------------------void song() //演奏一个音符{TH0 = timer0h; //控制音调TL0 = timer0l;TR0 = 1; //启动T0, 由T0输出方波去发音delay(time); //控制时间长度}//--------------------------------------void main(void){unsigned char k, i;TMOD = 1; //置T0定时工作方式1ET0 = 1; //开T0中断EA = 1; //开CPU中断while(1) {i = 0;time = 1;while(time) {k = sszymmh[i] + 7 * sszymmh[i + 1] - 1;//第i个是音符, 第i+1个是第几个八度timer0h = FREQH[k]; //从数据表中读出频率数值timer0l = FREQL[k]; //实际上, 是定时的时间长度time = sszymmh[i + 2]; //读出时间长度数值i += 3;song(); //发出一个音符}}}三、实验结果蜂鸣器发出梁祝的音乐,结果视频见结果视频文件夹。

51单片机蜂鸣器播放音乐代码

51单片机蜂鸣器播放音乐代码

/*生日快乐歌曲*/#include <>#define uint unsigned int#define uchar unsigned charsbit beep = P1^5;uchar code SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159, 212,212,106,126,159,169,190,119,119,126,159,142,159,0};uchar code SONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0};//延时!void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}void PlayMusic(){uint i=0,j,k;while(SONG_LONG[i]!=0||SONG_TONE[i]!=0){ //播放各个音符,SONG_LONG 为拍子长度%for(j=0;j<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE 延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}~void main(){beep=0;while(1){PlayMusic(); //播放生日快乐DelayMS(500); //播放完后暂停一段时间}}两只蝴蝶:%#include <> // 这是单片机音乐代码生成器生成的代码#define uchar unsigned charsbit beepIO=P1^5; // 输出为可以修改成其它IO 口uchar m,n;uchar code T[49][2]={{0,0},{0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0xB9},{0xFB,0x03 },{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},{0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0x5B},{0xFD,0x8 1},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},{0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0xAD},{0xFE,0xC0 },{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},{0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0x56},{0xFF,0x60},{ 0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81}};~uchar code music[][2]={{0,4},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{14,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,24},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{21,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{14,24},{23,4},{26,4},{26,16},{26,4},{28,4},{26,4},{23,24},{21,4},{23,4},{21,8},{21,4},{23,4},{21,4},{19,4},{16 ,4},{16,2},{19,2},{19,24},{0,20},{26,4},{26,4},{28,4},{31,4},{30,4},{30,4},{28,4},{23,4},{21,4},{21,4},{23,16},{0,4},{23,4},{23,4},{26,4} ,{28,8},{28,12},{16,4},{23,4},{21,4},{21,24},{23,4},{26,4},{26,4},{23,4},{26,8},{0,4},{31,8},{30,4},{28,4},{30,4},{23,8},{0,4},{28,4},{28,4},{ 30,4},{28,4},{26,4},{23,4},{21,8},{23,4},{21,4},{23,4},{26,16},{0xFF,0xFF}};void delay(uchar p){.uchar i,j;for(;p>0;p--)for(i=181;i>0;i--)for(j=181;j>0;j--);}void pause(){uchar i,j;for(i=150;i>0;i--)for(j=150;j>0;j--);/}void T0_int() interrupt 1{beepIO=!beepIO;TH0=T[m][0]; TL0=T[m][1];}void main(){uchar i=0;TMOD=0x01; EA=1; ET0=1;、while(1){m=music[i][0];n=music[i][1];if(m==0x00){TR0=0;delay(n);i++;}else if(m==0xFF){TR0=0;delay(30);i=0;}else if(m==music[i+1][0]){TR0=1;delay(n);TR0=0;pause();i++;}else.{TR0=1;delay(n);i++;}}}祝你平安:#include ""unsigned char Count;sbit _Speak =P1^5 ;unsigned char code SONG[] ={ //祝你平安0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20, 0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20, %0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10, 0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20, 0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20, 0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20, 0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10, 0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80, 0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20, 0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,¥0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00}; void Time0_Init(){TMOD = 0x01;IE = 0x82;TH0 = 0xD8;TL0 = 0xEF; //12MZ晶振,10ms}void Time0_Int() interrupt 1({TH0 = 0xD8;TL0 = 0xEF;Count++; //长度加1}/*-------------------------------------------------功能:1MS延时子程序-------------------------------------------------*/void Delay_xMs(unsigned int x){)unsigned int i,j;for( i =0;i < x;i++ ){for( j =0;j<3;j++ );}}void Play_Song(unsigned char i){unsigned char Temp1,Temp2;unsigned int Addr;!Count = 0; //中断计数器清0Addr = i * 217;while(1){Temp1 = SONG[Addr++];if ( Temp1 == 0xFF ) //休止符{TR0 = 0;Delay_xMs(100);}:else if ( Temp1 == 0x00 ) //歌曲结束符{return;}else{Temp2 = SONG[Addr++];TR0 = 1;while(1){._Speak = ~_Speak;Delay_xMs(Temp1);if ( Temp2 == Count ){Count = 0;break;}}}}}/*-------------------------------------------------功能:主程序-------------------------------------------------*/ void main(){Time0_Init(); //定时器0中断初始化while(1){Play_Song(0); //播放}}。

单片机课程设计报告利用蜂鸣器播放音乐样本

单片机课程设计报告利用蜂鸣器播放音乐样本

课程设计: 电子设计题目名称: 音乐流水灯姓名: 戴锦超学号: 08123447班级: 信科12-3班完成时间: 10月23日1设计的任务设计内容: 动手焊接一个51单片机设计目标: 利用单片机上的蜂鸣器以及二极管实现音乐播放以及根据音乐的节奏而规律性闪亮的二极管。

而且经过程序调节音乐节奏的快慢。

2 设计的过程2.1 基本结构1.STC89C52RC在本次的试验中采用了STC89C52RC单片机, STC89C52RC 单片机是宏晶科技推出的新一代高速/低功耗/超强抗干扰的单片机, 指令代码完全兼容传统8051单片机, 12时钟/机器周期, 工作电压: 5.5V~3.3V( 5V单片机) /3.8V~2.0V( 3V单片机) , 工作频率范围: 0~40MHz, 相当于普通8051的0~80MHz, 实际工作频率可达48MHz, 用户应用程序空间为8K字节。

( STC89C52RC引脚图)STC89C52RC单片机的工作模式:(1)典型功耗<0.1μA,可由外部中断唤醒, 中断返回后, 继续执行原程序(2)空闲模式: 典型功耗2mA(3)正常工作模式: 典型功耗4Ma~7mA( 4) 唤醒, 适用于水表、气表等电池供电系统及便携设备2.蜂鸣器及其工作原理:蜂鸣器按其结构分主要分为压电式蜂鸣器和电磁式蜂鸣器两种类型。

电磁式蜂鸣器由振荡器、电磁线圈、磁铁、振动膜片及外壳等组成。

接通电源后, 振荡器产生的音频信号电流经过电磁线圈, 使电磁线圈产生磁场, 振动膜片在电磁线圈和磁铁的相互作用下, 周期性地振动发声。

本实验采用的是电磁式蜂鸣器。

蜂鸣器按其是否带有信号源又分为有源和无源两种类型。

有源蜂鸣器只需要在其供电端加上额定直流电压, 其内部的震荡器就能够产生固定频率的信号, 驱动蜂鸣器发出声音。

无源蜂鸣器能够理解成与喇叭一样, 需要在其供电端上加上高低不断变化的电信号才能够驱动发出声音。

本实验采用的是有源蜂鸣器。

单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安

单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安

单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安/*生日快乐歌曲*/#include<reg51.h>#defineuintunsignedint#defineucharunsignedcharsbitbeep=P1^5;ucharcodeSONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159,212,212,106,126,159,169,190,119,119,126,159,142,159,0};ucharcodeSONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24,9,3,12,12,12,12,12,9,3,12,12,12,24,0};//延时voidDelayMS(uintx){uchart;while(x--)for(t=0;t<120;t++);}voidPlayMusic(){uinti=0,j,k;while(SONG_LONG[i]!=0||SONG_TONE[i]!=0){//播放各个音符,SONG_LONG为拍子长度for(j=0;j<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}voidmain(){beep=0;while(1){PlayMusic();//播放生日快乐DelayMS(500);//播放完后暂停一段时间}}两只蝴蝶:#include<reg51.h>//这是单片机音乐代码生成器生成的代码#defineucharunsignedcharsbitbeepIO=P1^5;//输出为P1.5可以修改成其它IO口ucharcodeT[49][2]={{0,0},{0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0xB9},{ 0xFB,0x03},{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},{0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0x5B},{ 0xFD,0x81},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},{0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0xAD},{ 0xFE,0xC0},{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},{0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0x56},{ 0xFF,0x60},{0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81}};ucharcodemusic[][2]={{0,4},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4} ,{23,4},{21,4},{19,4},{16,4},{19,4},{14,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,24},{16,4},{19,4},{21,8},{21,4} ,{23,4},{21,4},{19,4},{16,4},{19,4},{21,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4} ,{23,4},{21,4},{19,4},{16,4},{19,4},{14,24},{23,4},{26,4},{26,16},{26,4},{28,4},{26,4},{23,24},{21,4},{23,4},{21,8},{21,4},{23,4} ,{21,4},{19,4},{16,4},{16,2},{19,2},{19,24},{0,20},{26,4},{26,4},{28,4},{31,4},{30,4},{30,4},{28,4},{23,4},{21,4},{21,4},{23,16},{0,4},{ 23,4},{23,4},{26,4},{28,8},{28,12},{16,4},{23,4},{21,4},{21,24},{23,4},{26,4},{26,4},{23,4},{26,8},{0,4},{31,8},{30,4},{28,4},{30,4},{23,8},{ 0,4},{28,4},{28,4},{30,4},{28,4},{26,4},{23,4},{21,8},{23,4},{21,4},{23,4},{26,16}, {0xFF,0xFF}};voiddelay(ucharp){uchari,j;for(;p>0;p--)for(i=181;i>0;i--)for(j=181;j>0;j--);}voidpause(){uchari,j;for(i=150;i>0;i--)for(j=150;j>0;j--);}voidT0_int()interrupt1{beepIO=!beepIO;TH0=T[m][0];TL0=T[m][1];}voidmain(){uchari=0;while(1){m=music[i][0];n=music[i][1];if(m==0x00){TR0=0;delay(n);i++;}elseif(m==0xFF){TR0=0;delay(30);i=0;}elseif(m==music[i+1][0]){TR0=1;delay(n);TR0=0;pause();i++;}else{TR0=1;delay(n);i++;}}}祝你平安:#include"reg52.h"unsignedcharCount;sbit_Speak=P1^5;unsignedcharcodeSONG[]={//祝你平安0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20, 0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20, 0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10, 0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20, 0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20, 0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20, 0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10, 0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80, 0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20, 0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20, 0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10, 0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00};voidTime0_Init(){TMOD=0x01;IE=0x82;TH0=0xD8;TL0=0xEF;//12MZ晶振,10ms}voidTime0_Int()interrupt1{TH0=0xD8;TL0=0xEF;Count++;//长度加1}功能:1MS延时子程序-------------------------------------------------*/ voidDelay_xMs(unsignedintx){unsignedinti,j;for(i=0;i<x;i++){for(j=0;j<3;j++);}}voidPlay_Song(unsignedchari){unsignedcharTemp1,Temp2;unsignedintAddr;Count=0;//中断计数器清0Addr=i*217;while(1){Temp1=SONG[Addr++];if(Temp1==0xFF)//休止符{TR0=0;Delay_xMs(100);}elseif(Temp1==0x00)//歌曲结束符{return;}else{Temp2=SONG[Addr++];TR0=1;while(1){_Speak=~_Speak;Delay_xMs(Temp1);if(Temp2==Count){Count=0;break;}}}}}功能:主程序-------------------------------------------------*/ voidmain(){Time0_Init();//定时器0中断初始化while(1){Play_Song(0);//播放}}。

基于c51单片机编写简单蜂鸣器音乐程序的方法

基于c51单片机编写简单蜂鸣器音乐程序的方法
/*7*/case 0x22:j=s/494;break;case 0x23:j=s/998;break;case 0x24:j=s/1967;break;
/*o*/case 0xff:j=0;break;
}
return j;
}
void sound(uint s)//给一次脉冲来进行发声的子程序,其中参数s与m_t()中的0x01等十六进制数是对应的
0x01,0x0d,0x16,0x1c,0xff,0x1c,0x1c,0x16,0x07,0x07,0x10,0x0d,0xff, 0x01,0x0d,0x16,0x1c,0xff,
0x1c,0x1c,0x22,0x1c,0x16,0x10,0x0d,0x10,0x0d,0x07,0x01,0x00} ;//*
以下是本人编写的一段简单音乐程序,程序已经在板子上调试成功,水平有限,如有不正确的地方请多包涵
*******************************************************************************************************/
#include<reg52.H>
#include <intrins.h> //内部包含延时函数_nop_();
typedef unsigned char uchar;
typedef unsigned int uint;
uint c;
sbit spk =P2^0 ; //定义p2.0口为电平信号输出端
最后在主程序中运行子程序,通过读rom里面的数组来达到连续播放各种不同音调的音的目的,这样听起来就像一首曲子了。
以下是我按以上方法编写的一个小程序:

单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安

单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安

/生日快乐歌曲/ include<reg51.h>defineuintunsignedintdefineucharunsignedcharsbitbeep=P1^5;ucharcodeSONG_TONE={212;212;190;212;159;169;212;212;190;212;142;159;212;212;106;126;159;169;190;119;119;126;159;142;159;0};ucharcodeSONG_LONG={9;3;12;12;12;24;9;3;12;12;12;24;9;3;12;12;12;12;12;9;3;12;12;12;24;0};//延时voidDelayMSuintx{uchart;whilex--fort=0;t<120;t++;}voidPlayMusic{uinti=0;j;k;whileSONG_LONGi=0||SONG_TONEi=0{//播放各个音符;SONG_LONG为拍子长度forj=0;j<SONG_LONGi20;j++{//SONG_TONE延时表决定了每个音符的频率fork=0;k<SONG_TONEi/3;k++;}DelayMS10;i++;}}voidmain{beep=0;while1{PlayMusic;//播放生日快乐DelayMS500;//播放完后暂停一段时间}}两只蝴蝶:include<reg51.h>//这是单片机音乐代码生成器生成的代码defineucharunsignedcharsbitbeepIO=P1^5;//输出为P1.5可以修改成其它IO口ucharm;n;ucharcodeT492={{0;0};{0xF8;0x8B};{0xF8;0xF2};{0xF9;0x5B};{0xF9;0xB7};{0xFA;0x14};{0xFA;0x66};{0xFA;{0xFC;0x43};{0xFC;0x78};{0xFC;0xAB};{0xFC;0xDB};{0xFD;0x08};{0xFD;0x33};{0xFD; 0x5B};{0xFD;0x81};{0xFD;0xA5};{0xFD;0xC7};{0xFD;0xE7};{0xFE;0x05};{0xFE;0x21};{0xFE;0x3C};{0xFE;0x55};{0xFE;0x6D};{0xFE;0x84};{0xFE;0x99};{0xFE; 0xAD};{0xFE;0xC0};{0xFE;0x02};{0xFE;0xE3};{0xFE;0xF3};{0xFF;0x02};{0xFF;0x10};{0xFF;0x1D};{0xFF;0x2A};{0xFF;0x36};{0xFF;0x42};{0xFF;0x4C};{0xFF; 0x56};{0xFF;0x60};{0xFF;0x69};{0xFF;0x71};{0xFF;0x79};{0xFF;0x81}};ucharcodemusic2={{0;4};{23;4};{21;4};{23;16};{23;4};{21;4};{23;4};{21;4};{19;16};{16;4};{19;4};{21;8} ;{21;4};{23;4};{21;4};{19;4};{16;4};{19;4};{14;24};{23;4};{21;4};{23;16};{23;4};{21;4};{23;4};{21;4};{19;24};{16;4};{19;4};{21;8} ;{21;4};{23;4};{21;4};{19;4};{16;4};{19;4};{21;24};{23;4};{21;4};{23;16};{23;4};{21;4};{23;4};{21;4};{19;16};{16;4};{19;4};{21;8} ;{21;4};{23;4};{21;4};{19;4};{16;4};{19;4};{14;24};{23;4};{26;4};{26;16};{26;4};{28;4};{26;4};{23;24};{21;4};{23;4};{21;8};{21;4} ;{23;4};{21;4};{19;4};{16;4};{16;2};{19;2};{19;24};{0;20};{26;4};{26;4};{28;4};{31;4};{30;4};{30;4};{28;4};{23;4};{21;4};{21;4};{23;16}; {0;4};{23;4};{23;4};{26;4};{28;8};{28;12};{16;4};{23;4};{21;4};{21;24};{23;4};{26;4};{26;4};{23;4};{26;8};{0;4};{31;8};{30;4};{28;4};{30;4};{ 23;8};{0;4};{28;4};{28;4};{30;4};{28;4};{26;4};{23;4};{21;8};{23;4};{21;4};{23 ;4};{26;16};{0xFF;0xFF}};voiddelayucharpuchari;j;for;p>0;p--fori=181;i>0;i--forj=181;j>0;j--;}voidpause{uchari;j;fori=150;i>0;i--forj=150;j>0;j--;}voidT0_intinterrupt1 {beepIO=beepIO;TH0=Tm0;TL0=Tm1;}voidmain{uchari=0;TMOD=0x01;EA=1;ET0=1; while1{m=musici0;n=musici1;{TR0=0;delayn;i++;}elseifm==0xFF{TR0=0;delay30;i=0;}elseifm==musici+10{TR0=1;delayn;TR0=0;pause;i++;}else{TR0=1;delayn;i++;}}}祝你平安:include"reg52.h"unsignedcharCount;sbit_Speak=P1^5;unsignedcharcodeSONG={//祝你平安0x26;0x20;0x20;0x20;0x20;0x20;0x26;0x10;0x20;0x10;0x20;0x80;0x26;0x20;0x30;0x2 0;0x30;0x20;0x39;0x10;0x30;0x10;0x30;0x80;0x26;0x20;0x20;0x20;0x20;0x20;0x1c;0x2 0;0x20;0x80;0x2b;0x20;0x26;0x20;0x20;0x20;0x2b;0x10;0x26;0x10;0x2b;0x80;0x26;0x2 0;0x30;0x20;0x30;0x20;0x39;0x10;0x26;0x10;0x26;0x60;0x40;0x10;0x39;0x10;0x26;0x2 0;0x30;0x20;0x30;0x20;0x39;0x10;0x26;0x10;0x26;0x80;0x26;0x20;0x2b;0x10;0x2b;0x10x2b;0x20;0x30;0x10;0x39;0x10;0x26;0x10;0x2b;0x10;0x2b;0x20;0x2b;0x40;0x40;0x2 0;0x20;0x10;0x20;0x10;0x2b;0x10;0x26;0x30;0x30;0x80;0x18;0x20;0x18;0x20;0x26;0x2 0;0x20;0x20;0x20;0x40;0x26;0x20;0x2b;0x20;0x30;0x20;0x30;0x20;0x1c;0x20;0x20;0x2 0;0x20;0x80;0x1c;0x20;0x1c;0x20;0x1c;0x20;0x30;0x20;0x30;0x60;0x39;0x10;0x30;0x1 0;0x20;0x20;0x2b;0x10;0x26;0x10;0x2b;0x10;0x26;0x10;0x26;0x10;0x2b;0x10;0x2b;0x8 0;0x18;0x20;0x18;0x20;0x26;0x20;0x20;0x20;0x20;0x60;0x26;0x10;0x2b;0x20;0x30;0x2 0;0x30;0x20;0x1c;0x20;0x20;0x20;0x20;0x80;0x26;0x20;0x30;0x10;0x30;0x10;0x30;0x2 0;0x39;0x20;0x26;0x10;0x2b;0x10;0x2b;0x20;0x2b;0x40;0x40;0x10;0x40;0x10;0x20;0x1 0;0x20;0x10;0x2b;0x10;0x26;0x30;0x30;0x80;0x00};voidTime0_Init{TMOD=0x01;IE=0x82;TH0=0xD8;TL0=0xEF;//12MZ晶振;10msvoidTime0_Intinterrupt1{TH0=0xD8;TL0=0xEF;Count++;//长度加1}/-------------------------------------------------功能:1MS延时子程序-------------------------------------------------/ voidDelay_xMsunsignedintx{unsignedinti;j;fori=0;i<x;i++{forj=0;j<3;j++;}}voidPlay_Songunsignedchari{unsignedcharTemp1;Temp2;unsignedintAddr;Count=0;//中断计数器清0Addr=i217;Temp1=SONGAddr++;ifTemp1==0xFF//休止符{TR0=0;Delay_xMs100;}elseifTemp1==0x00//歌曲结束符{return;}else{Temp2=SONGAddr++;TR0=1;while1{_Speak=~_Speak;Delay_xMsTemp1;ifTemp2==Count{Count=0;break;}}}/-------------------------------------------------功能:主程序-------------------------------------------------/ voidmain{Time0_Init;//定时器0中断初始化while1{Play_Song0;//播放}}。

用单片机驱动蜂鸣器唱歌的设计与原理

用单片机驱动蜂鸣器唱歌的设计与原理

用单片机驱动蜂鸣器唱歌的设计与原理1.蜂鸣器蜂鸣器是一种一体化结构的电子讯响器,主要分为压电式蜂鸣器和电磁式蜂鸣器两种类型,他广泛应用于计算机、打印机、复印机、报警器、电话机等电子产品中作发声器件。

2.单片机驱动单片机上面使用的蜂鸣器一般都是无源电磁式的蜂鸣器(如下图所示)。

它由振荡器、电磁线圈、磁铁、振动膜片及外壳等组成。

接通电源后,振荡器产生的音频信号电流通过电磁线圈,使电磁线圈产生磁场,振动膜片在电磁线圈和磁铁的相互作用下,周期性地振动发声。

蜂鸣器发声原理是电流通过电磁线圈,使电磁线圈产生磁场来驱动振动膜发声的,因此需要一定的电流才能驱动它,单片机IO引脚输出的电流较小,单片机输出的TTL电平基本上驱动不了蜂鸣器,因此需要增加一个电流放大的电路。

单片机与蜂鸣器连接如图二所示。

图中,蜂鸣器的正极接到VCC(+5V)电源上面,蜂鸣器的负极接到三极管的发射极E,三极管的基级B经过限流电阻R1后由单片机的P3.7引脚控制,当P3.7输出高电平时,三极管T1截止,没有电流流过线圈,蜂鸣器不发声;当P3.7输出低电平时,三极管导通,这样蜂鸣器的电流形成回路,发出声音。

因此,我们可以通过程序控制P3.7脚的电平来使蜂鸣器发出声音和关闭。

程序中改变单片机P3.7引脚输出波形的频率,就可以调整控制蜂鸣器音调,产生各种不同音色、音调的声音。

另外,改变P3.7输出电平的高低电平占空比,则可以控制蜂鸣器的声音大小,这些我们都可以通过编程实验来验证。

下面就是一个能够发出音乐的单片机程序,示例中所播放的音乐是《两只老虎》。

SPK EQU P3.7 ;位定义ORG 0000H ;伪指令,指定程序从0000H开始存放LJMP START ;程序跳转至START处执行ORG 0030H ;伪指令,指定程序从0030H开始存放START: MOV SP,#60H ;堆栈初始化MOV R3,#00H ;给R3赋值NEXT:MOV A,R3MOV DPTR,#TABLE ;查歌曲表MOVC A,@A+DPTRJZ START ;为00则循环播放此歌MOV R7,A ;R7/R2保存连续相邻的表数据INC R3MOV A,R3MOVC A,@A+DPTRMOV R2,AACALL SONGINC R3SJMP NEXT;==============================;=========歌曲播放子程序==========;============================== SONG:MOV A,R2 ;取出节拍RL AJNZ KEEP ;A不等于零则跳MOV A,#01H ;A等于零则赋值为1 KEEP:MOV R2,A ;REPEAT:ACALL EIGHTH ;调用1/8拍延时程序DJNZ R2,REPEAT ;RET;===============================;=======产生1/8拍延时子程序=======;===============================EIGHTH:MOV A,R7 ;查表取出延时参数,保存到R4MOV DPTR,#DELAY_TMOVC A,@A+DPTRMOV R4,AMOV A,R7 ;查表取出1/8拍周期数,保存到R5 MOV DPTR,#S_PARAMOVC A,@A+DPTRMOV R5,ANEXTCYC:ACALL SOUNDDJNZ R5,NEXTCYCRET;==============================;===========发声子程序===========;==============================SOUND:SETB SPKACALL SDELAYCLR SPKACALL SDELAYRET;==============================;============================== SDELAY:MOV A,R4 ;延时值在R4内MOV R0,AXL2:MOV R1,#03HDL1:NOPDJNZ R1,DL1DJNZ R0,XL2RET;============================== ;===========1/8拍周期表========== ;============================== S_PARA:DS 1DHDB 15H,16H,00DB 19H,00H,1CH,00H,1FH,21H,00H,25HDB 00H,29H,2CH,00H,31H,34H,37H,00HDB 3EH,41H,00H,49H,00H,52H,57H,00HDB 62H;==============================;============================== DELAY_T:DS 1DHDB 7EH,77H,00HDB 6AH,00H,5EH,00H,54H,4FH,00H,46HDB 00H,3FH,3BH,00H,35H,32H,2FH,00HDB 2AH,27H,00H,23H,00H,1FH,1DH,0C0HDB 1AH;============================== ;============歌曲表============= ;============================== TABLE:DW 2504H,2704H,2904H,2504HDW 2504H,2704H,2904H,2504HDW 2904H,2A04H,2C08HDW 2904H,2A04H,2C08HDW 2C02H,2E02H,2C02H,2A02H,2904H,2504H DW 2C02H,2E02H,2C02H,2A02H,2904H,2504H DW 2904H,2004H,2508HDW 2904H,2004H,2508HDW 0000HEND童鞋们可能发现了,数据表TABLE里面的数据才是歌曲数据。

单片机蜂鸣器音乐代码

单片机蜂鸣器音乐代码

单片机蜂鸣器音乐代码在单片机的应用中,蜂鸣器是一个常用的音频输出设备。

它可以通过产生不同频率的声音来实现音乐播放、提醒和警报等功能。

本文将介绍如何使用单片机控制蜂鸣器播放音乐,并提供一个简单的音乐代码示例。

首先,让我们了解一下单片机蜂鸣器的工作原理。

蜂鸣器实际上是一个压电陶瓷元件,当电压作用于其上时,它会振动产生声音。

为了产生不同的音调,我们需要控制蜂鸣器的频率和占空比。

单片机通过IO口与蜂鸣器连接,并使用定时器来产生所需的频率。

具体的代码实现将依赖于使用的单片机型号和开发环境。

在这里,我们将以C语言为例,并基于51单片机进行说明。

以下是一个简单的单片机蜂鸣器音乐代码示例,演奏的是《欢乐颂》的前几个音符:```c#include <reg52.h>// 定义各个音符的频率#define C4 4778#define D4 4257#define E4 3792#define F4 3579#define G4 3189#define A4 2841#define B4 2531#define C5 2388// 延时函数void delay(unsigned int count){while(count--);}// 发声函数void beep(unsigned int frequency, unsigned int duration) {unsigned int i;unsigned long time;time = 11059200 / frequency; time >>= 1;for(i = 0; i < duration; i++) {P1 = 0x08;delay(time);P1 = 0x00;delay(time);}}// 主函数void main(){// 设置定时器TMOD = 0x01;TH0 = (65536 - 50000) / 256; TL0 = (65536 - 50000) % 256; TR0 = 1;// 播放音乐beep(C4, 100);beep(D4, 100);beep(E4, 100);beep(F4, 100);beep(G4, 100);beep(A4, 100);beep(B4, 100);beep(C5, 100);while(1);}```以上代码中,我们首先定义了各个音符的频率,这些频率是通过实验测量得到的,具体数值可能因蜂鸣器型号、电源电压等因素而略有差异。

基于单片机的蜂鸣器唱歌设计(附图)

基于单片机的蜂鸣器唱歌设计(附图)

蜂鸣器是一种一体化结构的电子讯响器�主要分为压电式蜂鸣器和电磁式蜂鸣器两种类型�他广泛应用于计算机、打印机、复印机、报警器、电话机等电子产品中作发声器件。

单片机上面使用的蜂鸣器一般都是无源电磁式的蜂鸣器�如下图所示�。

它由振荡器、电磁线圈、磁铁、振动膜片及外壳等组成。

接通电源后�振荡器产生的音频信号电流通过电磁线圈�使电磁线圈产生磁场�振动膜片在电磁线圈和磁铁的相互作用下�周期性地振动发声。

蜂鸣器发声原理是电流通过电磁线圈�使电磁线圈产生磁场来驱动振动膜发声的�因此需要一定的电流才能驱动它�单片机IO引脚输出的电流较小�单片机输出的TTL电平基本上驱动不了蜂鸣器�因此需要增加一个电流放大的电路。

单片机与蜂鸣器连接如图二所示。

图中�蜂鸣器的正极接到VCC��5V�电源上面�蜂鸣器的负极接到三极管的发射极E�三极管的基级B经过限流电阻R1后由单片机的P3.7引脚控制�当P3.7输出高电平时�三极管T1截止�没有电流流过线圈�蜂鸣器不发声�当P3.7输出低电平时�三极管导通�这样蜂鸣器的电流形成回路�发出声音。

因此�我们可以通过程序控制P3.7脚的电平来使蜂鸣器发出声音和关闭。

程序中改变单片机P3.7引脚输出波形的频率�就可以调整控制蜂鸣器音调�产生各种不同音色、音调的声音。

另外�改变P3.7输出电平的高低电平占空比�则可以控制蜂鸣器的声音大小�这些我们都可以通过编程实验来验证。

下面就是一个能够发出音乐的单片机程序�示例中所播放的音乐是《两只老虎》。

SPK EQU P3.7 ;位定义ORG 0000H ;伪指令�指定程序从0000H开始存放LJMP START ;程序跳转至START处执行ORG 0030H ;伪指令�指定程序从0030H开始存放START: MOV SP,#60H ;堆栈初始化MOV R3,#00H ;给R3赋值NEXT:MOV A,R3MOV DPTR,#TABLE ;查歌曲表MOVC A,@A+DPTRJZ START ;为00则循环播放此歌MOV R7,A ;R7/R2保存连续相邻的表数据INC R3MOV A,R3MOVC A,@A+DPTRMOV R2,AACALL SONGINC R3SJMP NEXT;==============================;=========歌曲播放子程序==========;==============================SONG:MOV A,R2 ;取出节拍RL AJNZ KEEP ;A不等于零则跳MOV A,#01H ;A等于零则赋值为1KEEP:MOV R2,A ;REPEAT:ACALL EIGHTH ;调用1/8拍延时程序DJNZ R2,REPEAT ;RET;===============================;=======产生1/8拍延时子程序=======;===============================EIGHTH:MOV A,R7 ;查表取出延时参数,保存到R4MOV DPTR,#DELAY_TMOVC A,@A+DPTRMOV R4,AMOV A,R7 ;查表取出1/8拍周期数,保存到R5 MOV DPTR,#S_PARAMOVC A,@A+DPTRMOV R5,ANEXTCYC:ACALL SOUNDDJNZ R5,NEXTCYCRET;==============================;===========发声子程序===========;==============================SOUND:SETB SPKACALL SDELAYCLR SPKACALL SDELAYRET;==============================;===========延时子程序===========;==============================SDELAY:MOV A,R4 ;延时值在R4内MOV R0,AXL2:MOV R1,#03HDL1:NOPDJNZ R1,DL1DJNZ R0,XL2RET;==============================;===========1/8拍周期表==========;==============================S_PARA:DS 1DHDB 15H,16H,00DB 19H,00H,1CH,00H,1FH,21H,00H,25HDB 00H,29H,2CH,00H,31H,34H,37H,00HDB 3EH,41H,00H,49H,00H,52H,57H,00HDB 62H;==============================;===========延时参数表===========;==============================DELAY_T:DS 1DHDB 7EH,77H,00HDB 6AH,00H,5EH,00H,54H,4FH,00H,46HDB 00H,3FH,3BH,00H,35H,32H,2FH,00HDB 2AH,27H,00H,23H,00H,1FH,1DH,0C0HDB 1AH;==============================;============歌曲表=============;==============================TABLE:DW 2504H,2704H,2904H,2504HDW 2504H,2704H,2904H,2504HDW 2904H,2A04H,2C08HDW 2904H,2A04H,2C08HDW 2C02H,2E02H,2C02H,2A02H,2904H,2504HDW 2C02H,2E02H,2C02H,2A02H,2904H,2504HDW 2904H,2004H,2508HDW 2904H,2004H,2508HDW 0000HEND童鞋们可能发现了�数据表TABLE里面的数据才是歌曲数据。

51单片机蜂鸣器音阶所对应频率

51单片机蜂鸣器音阶所对应频率

在51单片机中,蜂鸣器音阶所对应的频率是相当重要的。

通过对频率的设定,可以在实际应用中实现不同的音调和音乐效果。

让我们先来了解一下51单片机蜂鸣器的工作原理。

51单片机蜂鸣器是一种被广泛应用于各种电子设备中的音频输出装置,它通过控制电流的频率和占空比来发出不同音调的声音。

在实际应用中,我们要根据需要来设定蜂鸣器的频率,从而实现不同的音阶和音乐效果。

接下来,让我们来详细探讨一下51单片机蜂鸣器音阶所对应的频率。

在音乐理论中,音阶是由一系列音符按特定的音程组成的音乐音阶体系。

常见的音阶包括C大调、D大调、E大调等,每个音阶都对应着特定的频率。

在51单片机蜂鸣器中,我们可以通过设置不同的频率来模拟出这些音阶,从而实现丰富的音乐效果。

以C大调音阶为例,我们可以将C4音符的频率设定为261.63Hz,D4音符的频率设定为293.66Hz,E4音符的频率设定为329.63Hz,以此类推。

通过逐个设置每个音符的频率,我们就可以在51单片机蜂鸣器上模拟出C大调音阶的音乐效果。

同样的方法也适用于其他音阶,只需要根据对应的频率来进行设置即可。

除了基本的音阶,我们还可以通过设置不同频率的音符来实现和弦、音阶、旋律等更复杂的音乐效果。

在实际应用中,我们可以根据具体的需求来调整蜂鸣器的频率,从而实现丰富多样的音乐效果。

总结回顾:在51单片机中,蜂鸣器的工作原理是通过控制电流的频率和占空比来发出不同音调的声音。

对应频率是实现不同音阶和音乐效果的关键。

通过设置不同频率的音符,我们可以模拟出各种音阶、和弦、旋律等丰富的音乐效果。

在实际应用中,可以根据具体的需求来调整蜂鸣器的频率,从而实现丰富多样的音乐效果。

个人观点:蜂鸣器音阶所对应的频率在51单片机中起着至关重要的作用,它不仅可以用于模拟各种音阶和音乐效果,还可以用于实现各种声音提示和警报。

在实际应用中,充分理解和掌握蜂鸣器频率与音阶的对应关系,可以为我们的电子设备带来更丰富、更灵活的音响功能。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

单片机蜂鸣器音乐单片机在我们的生活中无处不在,它被广泛地应用在各种电子产品中,为我们的生活带来了便利。

今天,我要向大家介绍的是一种基于单片机的蜂鸣器音乐播放器。

一、硬件部分1、单片机:我们选用的是AT89C51单片机,它具有低功耗、高性能的特点,非常适合用于音乐播放器。

2、蜂鸣器:蜂鸣器是用来发出声音的,我们将其连接在单片机的输出口上。

3、存储芯片:为了能够播放存储在芯片中的音乐,我们需要将音乐以某种格式存储在芯片中。

常用的存储芯片有EEPROM和Flash芯片。

4、按键:为了能够选择播放不同的音乐,我们需要添加一个按键。

二、软件部分1、音乐编码:我们需要将音乐转换成二进制编码,这样才能被单片机读取并播放。

常用的音乐编码格式有MIDI、WAV等。

2、音乐播放:当按下按键时,单片机读取存储芯片中的音乐数据,并通过蜂鸣器播放。

3、音乐选择:通过按键可以选择不同的音乐进行播放。

4、音量控制:我们可以通过编程来控制蜂鸣器的音量大小。

三、调试与测试1、硬件调试:检查连接是否正确,确保没有短路或断路的情况。

2、软件调试:将程序下载到单片机中进行调试,确保能够正常播放音乐。

3、综合测试:将所有硬件和软件都连接起来进行测试,确保能够正常工作。

四、总结与展望通过本次实验,我们成功地制作了一个基于单片机的蜂鸣器音乐播放器。

它具有简单、实用的特点,可以用来播放存储在芯片中的音乐。

未来,我们可以进一步扩展其功能,例如添加更多的按键来选择不同的音乐、添加显示屏来显示歌曲名称等。

我们也可以将其应用到其他领域,例如智能家居、智能安防等。

单片机蜂鸣器唱歌程序在许多应用中,单片机蜂鸣器经常被用来发出声音或音乐。

下面是一个使用单片机蜂鸣器唱歌的程序示例。

我们需要确定单片机和蜂鸣器的连接方式。

通常,单片机具有一个内置的蜂鸣器输出引脚,可以将蜂鸣器连接到这个引脚上。

在以下的示例中,我们将假设单片机具有一个内置蜂鸣器输出引脚,并将其连接到P1.0端口上。

下面是使用C语言编写的一个简单的单片机蜂鸣器唱歌程序:cinclude <reg52.h> //包含单片机寄存器的头文件define BUZZER P1_0 //定义蜂鸣器输出端口void delay(unsigned int time) //延时函数unsigned int i, j;for(i=0; i<time; i++)for(j=0; j<1275; j++);void main()while(1) //无限循环{BUZZER = 0; //将蜂鸣器输出端口设为低电平,关闭蜂鸣器 delay(1000); //延时1秒BUZZER = 1; //将蜂鸣器输出端口设为高电平,打开蜂鸣器 delay(1000); //延时1秒BUZZER = 0; //再次将蜂鸣器输出端口设为低电平,关闭蜂鸣器delay(1000); //延时1秒BUZZER = 1; //再次将蜂鸣器输出端口设为高电平,打开蜂鸣器delay(2000); //延时2秒,控制音符的长度}在这个程序中,我们使用了无限循环来不断地发送信号到蜂鸣器。

在每个循环中,我们先将蜂鸣器输出端口设为低电平,关闭蜂鸣器,然后延时一段时间(在这个示例中,我们使用了1秒钟的时间)。

接着,我们将蜂鸣器输出端口设为高电平,打开蜂鸣器,再次延时一段时间(在这个示例中,我们使用了1秒钟的时间)。

然后,我们再次将蜂鸣器输出端口设为低电平,关闭蜂鸣器,并延时一段时间(在这个示例中,我们使用了2秒钟的时间),以控制音符的长度。

这个过程重复进行,就形成了一首简单的歌曲。

当然,大家可以根据自己的需要修改这个程序来实现不同的效果。

例如,大家可以添加更多的音符、改变它们的顺序、添加和弦等等。

基于单片机的蜂鸣器代码随着科技的不断发展,单片机已经成为现代电子设备中不可或缺的一部分。

蜂鸣器作为一种常见的电子器件,广泛应用于各种系统中,例如报警系统、音频播放系统等。

本文将介绍如何基于单片机编写蜂鸣器代码。

一、硬件准备首先需要准备一块单片机开发板和一只蜂鸣器。

以常见的Arduino开发板为例,可以使用Arduino UNO板作为单片机开发板,将蜂鸣器连接到开发板的某个数字引脚上。

二、代码编写下面是一个简单的基于Arduino的蜂鸣器代码,可以让蜂鸣器发出一段特定的频率:c++int buzzerPin = 9; //定义蜂鸣器连接的引脚号void setup() {pinMode(buzzerPin, OUTPUT); //将蜂鸣器引脚设置为输出模式void loop() {tone(buzzerPin, 2000); //生成2000Hz的音调,让蜂鸣器发声 delay(500); //持续发声500毫秒noTone(buzzerPin); //停止发声delay(500); //等待500毫秒,再进行下一次循环这段代码使用了Arduino的tone函数和noTone函数来控制蜂鸣器的发声和停止。

tone函数接受两个参数,第一个参数是引脚号,第二个参数是频率值(单位为赫兹)。

noTone函数则用于停止蜂鸣器的发声。

通过在loop函数中不断循环调用这两个函数,可以实现蜂鸣器的连续发声。

三、代码调试将代码上传到Arduino开发板上进行测试。

如果一切正常,蜂鸣器应该会按照预期的频率和时间发声。

如果有问题,可以检查连接是否正确,或者调整代码中的参数值。

单片机驱动蜂鸣器的程序设计随着科技的不断发展,单片机在许多应用场景中发挥着越来越重要的作用。

其中,单片机驱动蜂鸣器更是被广泛地应用于各种场合,如报警、提示、音乐播放等。

本文将介绍如何使用单片机驱动蜂鸣器,并给出相应的程序设计方法。

单片机驱动蜂鸣器原理单片机是一种集成度很高的微型计算机,它内部含有CPU、存储器、定时器/计数器、串行通信接口等多种功能模块。

通过编写程序,我们可以控制单片机的各个功能模块,从而实现特定的应用。

蜂鸣器通常与单片机的一个GPIO(通用输入输出)端口连接。

通过程序控制GPIO端口的输出电平,就可以控制蜂鸣器是否发声。

例如,当GPIO端口输出高电平时,蜂鸣器不发声;当GPIO端口输出低电平时,蜂鸣器发声。

程序设计1、确定应用程序的设计思路和流程在设计程序时,我们需要先确定应用程序的设计思路和流程。

以下是一个简单的报警系统程序设计思路:(1)定义变量:为了便于程序的控制和调试,我们需要先定义一些变量,如GPIO端口的输出电平、蜂鸣器的状态等。

(2)初始化蜂鸣器和GPIO端口:在程序开始运行时,我们需要对蜂鸣器和GPIO端口进行初始化,以确保它们能够正常工作。

(3)设置报警阈值:根据实际需求,设置报警阈值,如电压阈值、温度阈值等。

(4)检测输入信号:通过ADC(模数转换器)或其他传感器模块,实时检测输入信号是否超过报警阈值。

(5)控制蜂鸣器:当输入信号超过报警阈值时,通过程序控制GPIO 端口输出低电平,使蜂鸣器发声。

(6)延时:为了让报警声音持续一段时间,程序中需要加入延时操作。

(7)关闭蜂鸣器:当报警解除时,通过程序控制GPIO端口输出高电平,使蜂鸣器停止发声。

2、给岀完整的C语言程序代码以下是一个基于STC89C52单片机的蜂鸣器报警程序代码示例:c#include <reg52.h> //包含单片机头文件sbit beep=P2^0; //定义蜂鸣器引脚unsigned int threshold=500; //报警阈值unsigned int count=0;void delay(unsigned int t) //延时函数{while(t--);}void main(){P2=0x00; //初始化P2口为输岀模式 beep=1; //关闭蜂鸣器while(1) //循环检测{count++;if(count>=threshold) //判断是否超过阈值{beep=0; //蜂鸣器报警delay(1000); //延时1秒count=0; //重新计数}}}3、通过对程序进行测试和调试,验证程序的正确性和可行性在完成程序设计和编码后,我们需要对程序进行测试和调试,以验证其正确性和可行性。

首先,我们可以将程序下载到单片机中进行实际运行,观察蜂鸣器是否能够正常工作。

其次,我们可以改变输入信号的值,测试程序对不同情况的响应是否正确。

最后,我们还可以通过调整报警阈值和其他参数,以满足实际需求的变化。

基于单片机的音乐喷泉控制系统的设计一、引言随着科技的进步和人们生活品质的提高,音乐喷泉作为一种独特的艺术形式,已经成为了城市公共空间和私人花园中不可或缺的一部分。

音乐喷泉通过与音乐的节奏和旋律相结合,创造出令人惊叹的视觉效果。

而单片机作为一种高效的控制器件,能够为音乐喷泉提供精确且可靠的控制。

本文将探讨基于单片机的音乐喷泉控制系统的设计。

二、系统需求分析一个基于单片机的音乐喷泉控制系统需要实现以下功能:1、读取音乐信号:系统需要能够从音频输入设备中读取音乐信号。

2、喷泉控制:根据音乐信号的强度和频率,系统需要控制喷泉的喷水高度、频率和色彩。

3、实时显示:系统需要在显示屏上实时显示音乐波形和喷泉状态。

4、远程控制:系统需要能够通过遥控器或手机APP进行远程控制。

5、节能环保:系统应考虑节能和环保要求,降低功耗和减少水的浪费。

三、系统硬件设计1、微控制器:选择一款性能稳定、易于编程的单片机作为微控制器,如Arduino或STM32。

2、音频输入模块:选择一款具有高分辨率、低噪声的音频输入模块,如MAX9814或TLV320AIC23。

3、喷泉驱动模块:根据喷泉设备的特性,选择合适的驱动模块,如继电器驱动或PWM驱动。

4、显示屏:选择一款具有高清晰度、防水防尘的显示屏,如OLED或LED。

5、遥控器和手机APP接口:实现与遥控器的通信,以及通过手机APP 进行远程控制。

6、电源管理模块:选择稳定的电源管理模块,确保系统的稳定运行。

四、系统软件设计1、音频处理:实现音频信号的读取、滤波和放大,提取音乐信号的特征。

2、喷泉控制:根据音乐信号的特征,控制喷泉设备的动作。

3、实时显示:在显示屏上实时显示音乐波形和喷泉状态。

4、远程控制:实现遥控器和手机APP的通信接口,接收并处理远程控制指令。

5、节能环保:实现节能模式,如无人时自动关闭喷泉设备,降低功耗和水的浪费。

五、结论基于单片机的音乐喷泉控制系统能够实现高效、精确的控制,满足音乐喷泉的各种需求。

通过实现远程控制、实时显示和节能环保等功能,提高了系统的便利性和实用性。

单片机的高性能和稳定性也保证了系统的可靠性和稳定性。

因此,基于单片机的音乐喷泉控制系统是一种理想的音乐喷泉控制方案。

相关文档
最新文档