单片机控制蜂鸣器鸣奏音乐
单片机蜂鸣器播放音乐
#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
单片机蜂鸣器唱歌程序(二)引言概述:本文档主要介绍了单片机蜂鸣器唱歌程序(二),包括使用单片机控制蜂鸣器发出不同音乐的方法和具体实现步骤。
本文将从五个大点进行阐述,每个大点包含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. 展望将来的改进和研究方向总结:本文详细介绍了单片机蜂鸣器唱歌程序(二)的实现方法和步骤。
通过连接设置、编程环境配置、发声原理及代码实现、优化和调试、实验结果及总结等五个大点的阐述,读者可以深入了解单片机控制蜂鸣器发声的原理和方法,并通过实验得到具体的唱歌效果。
同时,读者在实践过程中也要注意优化和调试,不断尝试和改进,以实现更好的音效效果。
希望本文对读者有所帮助,为单片机蜂鸣器唱歌程序的开发提供了指导和参考。
单片机课程设计报告(利用蜂鸣器播放音乐)
课程设计:嵌入式系统应用题目名称:利用蜂鸣器实现音乐播放功能姓名:学号:班级:完成时间: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.蜂鸣器及其工作原理:蜂鸣器按其结构分主要分为压电式蜂鸣器和电磁式蜂鸣器两种类型。
电磁式蜂鸣器由振荡器、电磁线圈、磁铁、振动膜片及外壳等组成。
接通电源后,振荡器产生的音频信号电流通过电磁线圈,使电磁线圈产生磁场,振动膜片在电磁线圈和磁铁的相互作用下,周期性地振动发声。
本实验采用的是电磁式蜂鸣器。
蜂鸣器按其是否带有信号源又分为有源和无源两种类型。
有源蜂鸣器只需要在其供电端加上额定直流电压,其内部的震荡器就可以产生固定频率的信号,驱动蜂鸣器发出声音。
无源蜂鸣器可以理解成与喇叭一样,需要在其供电端上加上高低不断变化的电信号才可以驱动发出声音。
本实验采用的是有源蜂鸣器。
(蜂鸣器与单片机连接电路图)2.2 软件设计过程1.蜂鸣器发声原理本实验由于采用有源蜂鸣器,只需将引脚端口P3^4清零,蜂鸣器即可发声;P3^4置位,蜂鸣器停止发声。
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单片机蜂鸣器播放音乐代码(生日快乐 两只蝴蝶 祝你平安)精编版
/*生日快乐歌曲*/#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); //播放}}。
单片机 利用蜂鸣器演奏音乐
实验三-操纵蜂鸣器演奏音乐之青柳念文创作一、实验目标二、实验硬件设计及电路1. BlueSkyC51单片机实验板三极管主要是做驱动用的.因为单片机的IO口驱动才能不敷让蜂鸣器发出声音,所以我们通过三极管放大驱动电流,从而可让蜂鸣器发出声音,你要是输出高电平,三极管导通,集电极电畅通过蜂鸣器让蜂鸣器发出声音,当输出低电平时,三极管截止,没有电流流过蜂鸣器,所以就不会发出声音.三、实验原理用一个口,输出方波,这个方波输入进蜂鸣器就会发生声音,通过节制方波的频率、时间,就可以发生简单的音乐.一般说来,单片机演奏音乐基本都是单音频率,因此单片机奏乐只需节制音调和节拍.(1)音调的确定音调是由频率来确定的.通过单片机的定时器定时中断,将单片机上对应蜂鸣器的I/O口往返取反,从而让蜂鸣器发出分歧频率的声音.只需将定时器给以分歧的定时值便可实现.通过延时,即可发出所需要的频率.(2)节拍的确定一拍的时长大约为400—500ms,每一个音符的时长通过节拍来计算.详细见程序代码.(1)头文件#include<reg51.h>#define uint unsigned int#define uchar unsigned char#define ulong unsigned long(2)歌曲曲谱uchar code diaodata[30]={ //音调代码0x10,0x0e,0x0c,0x10,0x0e,0x0c,0x10,0x0e,0x10,0x0c,0x10,0x15,0x13,0x10,0x13,0x15,0x15,0x13,0x15,0x13,0x10,0x0e,0x0c,0x0e,0x0e,0x10,0x0e,0x0c,0x0e,0x00};uchar code jiedata[30]={ //音长代码0x04,0x04,0x08,0x06,0x02,0x08,0x04,0x04,0x04,0x04,0x02,0x06,0x08,0x02,0x02,0x04,0x04,0x04,0x04,0x08,0x02,0x02,0x04,0x04,0x02,0x02,0x04,0x04,0x0c,0x00};(3)音阶表uchar code freq[36*2]={ //音阶码表0xf8,0x50, //466hz , 1# //1 0xf8,0xbc, //494hz , 2 //2 0xf9,0x26, //524hz , 2# //3 0xf9,0x85, //554hz , 3 //4 0xf9,0xe5, //588hz , 4 //5 0xfa,0x3d, //622hz , 4# //6 0xfa,0x92, //660hz , 5 //7 0xfa,0xdd, //698hz , 5# //8 0xfb,0x29, //740hz , 6 //9 0xfb,0x70, //784hz , 6# //10 0xfb,0xb0, //830hz , 7 //11 0xfb,0xef, //880hz , 1 //12 0xfc,0x29, //932hz , 1# //13 0xfc,0x62, //988hz , 2 //14 0xfc,0x95, //1046hz, 2# //15 0xfc,0xc7, //1108hz, 3 //16 0xfc,0xf5, //1174hz, 4 //17 0xfd,0x20, //1244hz, 4# //18 0xfd,0x4c, //1318hz, 5 //19 0xfd,0x72, //1396hz, 5# //20 0xfd,0x97, //1480hz, 6 //210xfd,0xdc, //1662hz, 7 //230xfd,0xfb, //1769hz, `1 //240xfe,0x18, //1864hz, `1# //250xfe,0x34, //1976hz, `2 //260xfe,0x4e, //2092hz, `2# //270xfe,0x67, //2218hz, `3 //280xfe,0x7d, //2350hz, `4 //290xfe,0x94, //2488hz, `4# //300xfe,0xa8, //2639hz, `5 //310xfe,0xbc, //2794hz, `5# //320xfe,0xcf, //2960hz, `6 //330xfe,0xe0, //3136hz, `6# //340xfe,0xf1, //3322hz, `7 //35};(4)音符发生程序(中断服务程序)void timer0() interrupt 1 //用于发生音符的T0中断服务程序{TH0=th0_f;TL0=tl0_f;beep=~beep; //取反beep引脚,发声}三、实验过程及问题(1)新建“Project”,选择单片机为“AT89C52”(2)新建c文件“music.c”,并添加到工程中(3)设置生成“HEX”文件,编译(4)将生成的“HEX”文件烧录到单片机,检验(5)不竭修改完善(1)实际发音颤音重处理方法为修改蜂鸣器的驱动频率.(2)实际节拍过快或者过慢调整延时四、C51程序代码(部分来历于网络)#include<reg51.h>#define uint unsigned int#define uchar unsigned char#define ulong unsigned longuchar th0_f; //中断装载T0高8位uchar tl0_f; //T0低8位uchar code freq[36*2]={ //音阶码表0xf7,0xd8, //440hz , 1 //00xf8,0x50, //466hz , 1# //10xf8,0xbc, //494hz , 2 //20xf9,0x26, //524hz , 2# //3 0xf9,0x85, //554hz , 3 //4 0xf9,0xe5, //588hz , 4 //5 0xfa,0x3d, //622hz , 4# //6 0xfa,0x92, //660hz , 5 //7 0xfa,0xdd, //698hz , 5# //8 0xfb,0x29, //740hz , 6 //9 0xfb,0x70, //784hz , 6# //10 0xfb,0xb0, //830hz , 7 //11 0xfb,0xef, //880hz , 1 //12 0xfc,0x29, //932hz , 1# //13 0xfc,0x62, //988hz , 2 //14 0xfc,0x95, //1046hz, 2# //15 0xfc,0xc7, //1108hz, 3 //16 0xfc,0xf5, //1174hz, 4 //17 0xfd,0x20, //1244hz, 4# //18 0xfd,0x4c, //1318hz, 5 //19 0xfd,0x72, //1396hz, 5# //20 0xfd,0x97, //1480hz, 6 //21 0xfd,0xbb, //1568hz, 6# //22 0xfd,0xdc, //1662hz, 7 //23 0xfd,0xfb, //1769hz, `1 //240xfe,0x18, //1864hz, `1# //250xfe,0x34, //1976hz, `2 //260xfe,0x4e, //2092hz, `2# //270xfe,0x67, //2218hz, `3 //280xfe,0x7d, //2350hz, `4 //290xfe,0x94, //2488hz, `4# //300xfe,0xa8, //2639hz, `5 //310xfe,0xbc, //2794hz, `5# //320xfe,0xcf, //2960hz, `6 //330xfe,0xe0, //3136hz, `6# //340xfe,0xf1, //3322hz, `7 //35};uchar code diaodata[30]={ //音调代码0x10,0x0e,0x0c,0x10,0x0e,0x0c,0x10,0x0e,0x10,0x0c,0x10,0x15,0x13,0x10,0x13,0x15,0x15,0x13,0x15,0x13,0x10,0x0e,0x0c,0x0e,0x0e,0x10,0x0e,0x0c,0x0e,0x00};uchar code jiedata[30]={ //音长代码0x04,0x04,0x08,0x06,0x02,0x08,0x04,0x04,0x04,0x04,0x02,0x06,0x08,0x02,0x02,0x04,0x04,0x04,0x04,0x08,0x02,0x02,0x04,0x04,0x02,0x02,0x04,0x04,0x0c,0x00};void timer0() interrupt 1 //用于发生音符的T0中断服务程序{TH0=th0_f;TL0=tl0_f;beep=~beep; //取反beep引脚,发声}void main(void){uchari,j,k=0;uint n;TMOD=0X01; //T0方式1TR0=0; //关闭T0(不发声)ET0=1; //允许T0中断EA=1; //允许总中断while(1){TR0=1; //开T0for(i=0;diaodata[i]!=0;i++){th0_f=freq[diaodata[i]*2];tl0_f=freq[diaodata[i]*2+1];for(j=0;j<20000;n++);//(jiedata*20000)TR0=0;for(n=0;n<256;n++); //音符之间的短暂延时TR0=1;}TR0=0;}}。
51单片机蜂鸣器播放单音节音乐
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;
STC15单片机无源蜂鸣器演奏歌曲RGB灯跟随节奏闪烁程序
敬告:没有51单片机基础的人请慎重下载高质量实用性51单片机STC15W系列程序(2),STC8A系列可参考STC15单片机无源蜂鸣器演奏歌曲RGB灯跟随节奏闪烁程序节选歌曲《骑上我心爱的小摩托》开头部分一段整体程序:#include "STC15.h"#include "intrins.h"#define MAIN_Fosc 11059200Lsbit BUZZ = P3^7;sbit KEY=P2^5;sbit R_color = P1^4;sbit G_color = P1^5;sbit B_color = P3^4;unsigned char cycle_value;bit flag;bit enableBuzzer;bit keyflag;unsigned int code note[]={523, 587, 659, 698, 784, 880, 988,};unsigned int code notefre[]={65536-((MAIN_Fosc/523)/2), 65536-((MAIN_Fosc/587)/2), 65536-((MAIN_Fosc/659)/2),65536-((MAIN_Fosc/698)/2),65536-((MAIN_Fosc/784)/2),65536-((MAIN_Fosc/880)/2),65536-((MAIN_Fosc/988)/2) };void Timer0_config();void PlayMusic();void delay_ms(unsigned int x) {unsigned i,j;for(i=0;i<x;i++){for(j=0;j<1100;j++);}}void Key_Scan(){if(KEY==1){delay_ms(10);if(KEY==1){keyflag = 1;}}}void RGB(){switch(cycle_value){case 0:R_color = 1;G_color = 0;B_color = 0;cycle_value = 1;break;case 1:R_color = 0;G_color = 1;B_color = 0;cycle_value = 2;break;case 2:R_color = 0;G_color = 0;B_color = 1;cycle_value = 0;break;default:break;}}void main(){P3M1 &= 0x7F; P3M0 &= 0x7F;P2M1 &= 0xDF; P2M0 &= 0xDF;P1M1 &= 0xCF; P1M0 |= 0x30;//P14,P15 pull output P3M1 &= 0xEF; P3M0 |= 0x10;//P34 pull outputTimer0_config();EA = 1;while(1){Key_Scan();if(keyflag){keyflag = 0;PlayMusic();}}}void PlayMusic(){unsigned char beat;//present beatunsigned int timer;//timerunsigned int totalTime;//timerunsigned int soundTime;//sount timeunsigned char code MotoNote[] = {0,1, 2,3, 4,3,3, 4,5, 5, 0, 0,0,6, 6,6, 6, 5, 4, 5, 0, 0, };unsigned char code MotoBeat[] = {2,2, 2,2, 2,1,1, 2,2, 6, 4, 4,2,2, 2,2, 2, 4, 2, 6, 4, 4,};for(beat=0;beat<sizeof(MotoBeat);){while(!flag);flag = 0;if(timer==0){totalTime =(note[MotoNote[beat]]*MotoBeat[beat])>>2;soundTime = totalTime - (totalTime>>2);TH0 = notefre[MotoNote[beat]]>>8;TL0 = notefre[MotoNote[beat]];enableBuzzer = 1;timer++;}else{if(timer>=totalTime){timer = 0;beat++;}else{timer++;if(timer==soundTime){enableBuzzer = 0;}}}}}void Timer0_config(){TMOD &= 0xF0;//16bit auto reloadAUXR |= 0x80;//1TTH0 = 0xFF;TL0 = 0x00;ET0 = 1;TR0 = 1;}void Timer0_interrupt() interrupt 1 {if(enableBuzzer){BUZZ = ~BUZZ;RGB();}else{BUZZ = 1;R_color = 0;G_color = 0;B_color = 0;}flag = 1;}。
51单片机蜂鸣器奏乐(生日快乐)
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、单片机:我们选用的是AT89C51单片机,它具有低功耗、高性能的特点,非常适合用于音乐播放器。
2、蜂鸣器:蜂鸣器是用来发出声音的,我们将其连接在单片机的输出口上。
3、存储芯片:为了能够播放存储在芯片中的音乐,我们需要将音乐以某种格式存储在芯片中。
常用的存储芯片有EEPROM和Flash芯片。
4、按键:为了能够选择播放不同的音乐,我们需要添加一个按键。
二、软件部分1、音乐编码:我们需要将音乐转换成二进制编码,这样才能被单片机读取并播放。
常用的音乐编码格式有MIDI、WAV等。
2、音乐播放:当按下按键时,单片机读取存储芯片中的音乐数据,并通过蜂鸣器播放。
3、音乐选择:通过按键可以选择不同的音乐进行播放。
4、音量控制:我们可以通过编程来控制蜂鸣器的音量大小。
三、调试与测试1、硬件调试:检查连接是否正确,确保没有短路或断路的情况。
2、软件调试:将程序下载到单片机中进行调试,确保能够正常播放音乐。
3、综合测试:将所有硬件和软件都连接起来进行测试,确保能够正常工作。
四、总结与展望通过本次实验,我们成功地制作了一个基于单片机的蜂鸣器音乐播放器。
它具有简单、实用的特点,可以用来播放存储在芯片中的音乐。
未来,我们可以进一步扩展其功能,例如添加更多的按键来选择不同的音乐、添加显示屏来显示歌曲名称等。
我们也可以将其应用到其他领域,例如智能家居、智能安防等。
单片机蜂鸣器唱歌程序在许多应用中,单片机蜂鸣器经常被用来发出声音或音乐。
下面是一个使用单片机蜂鸣器唱歌的程序示例。
我们需要确定单片机和蜂鸣器的连接方式。
通常,单片机具有一个内置的蜂鸣器输出引脚,可以将蜂鸣器连接到这个引脚上。
在以下的示例中,我们将假设单片机具有一个内置蜂鸣器输出引脚,并将其连接到P1.0端口上。
基于msp430控制蜂鸣器演奏乐曲的设计论文_本科论文
基于MSP430控制蜂鸣器演奏乐曲的设计指导老师:队员:学校:延安大学院系:物理与电子信息学院摘要 : 本作品为了实现蜂鸣器演奏音乐,设计基于单片机MSP430,以高低电平控制音调,电流通过蜂鸣器的电磁线圈,从而带动振动膜发声为方案,达到演奏美妙音乐的目的。
技术报告以蜂鸣器演奏音乐的设计为主线,包括蜂鸣器演奏音乐的的框架设计,软硬件设计,以及对灯光随高低电平闪烁的研究等。
基于可靠的硬件设计和稳定的软件算法,实现题目要求。
关键词:单片机MSP430 高低电平蜂鸣器Abstract:This work in order to achieve a buzzer play music, design based on the single chip microcomputer MSP430, with high and low level control tones, the current through the buzzer electromagnetic coils, thus promote vibration membrane voice for plan to meet wonderful music playing the purpose. Technical report to play music of the design of the buzzer main line, including playing music of the framework of the buzzer design, software and hardware design, and with high and low level of lights flashing. Based on reliable hardware design and stable software algorithm, and realize the topic request.Keywords: MSP430 microcontroller High and low level buzzer一、设计要求(1)基于单片机MSP430为主控芯片,利用蜂鸣器发声,以高低电平控制音调,来实现演奏乐曲的功能。
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); //播放}}。
单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安
/生日快乐歌曲/ 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;//播放}}。
ATmega16控制蜂鸣器播放音乐
N6,32,/**/H3,24,N6,8,/**/H2,24,H3,4,H2,4,/**/H1,16,H1,4,N6,4,H1,4,H2,4,/**/
{
unsigned i;
for(i=1;i<1714;i++);
}
void delay_ms(unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++)
{
delay_1ms();
}
}
音乐数据
这段是《孤星独吟》,自己对着简谱翻的
*************************************/
unsigned int song[]=
{
N6,12,N7,4,H1,8,H2,4,H3,4,/**/H2,32,/**/N7,12,N6,4,N5,8,N3,4,N5,4,/**/
#define H5 61709 //1568
#define H6 62127 //1760
#define H7 62500 //1976
#define HH1 62669 //2093
#define O 0 //休止符
/************************************
#include <iom16v.h>
#include <macros.h>
//计时值=65536-系统时钟/分频系数/音调频率/2
利用蜂鸣器和单片机演奏简单的音乐电路设计
利用蜂鸣器和单片机演奏简单的音乐电路设计利用蜂鸣器和单片机演奏简单的音乐实验原理图实验程序/* =========================================================== *//* ----------------------------------------------------------- *//* 曲谱存贮格式 uchar code 数组名{音高,音长,音高,音长....} *//* 音高由三位数字组成: *//* 个位是表示 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-11)是指乐曲升多少个半音演奏;升降八度(1-3)是指在演奏 */ /* 在哪个八度演奏: 1-降八度,2-不升不降,3-升八度.开始指针(0- ) */ /* 是从哪个音符开始演奏,结束指针是演奏到哪个音符为止. *//* ----------------------------------------------------------- */ //本程序用T0 来产生音调,用T1 产生音长#include <reg51.h>#define uchar unsigned char#define yx 4/5 /* 定义普通音符演奏的长度分率 */#define plen 2 /* 定义晶振的时钟周期(us) */#define uchar unsigned char#define uint unsigned intsbit speaker=P3^5;/* ------------------下面是曲谱 ------------------------------ */uchar code sound[100]={25,2,23,3,25,3,31,1,26,2,31,3,26,3,25,1,25,2,21,3,22,3,23,2,22,3,21,3,22, 0,25,2,23,3,25,3,31,102,27,3,26,2,31,2,25,1,25,2,22,3,23,3,24,102,17,3, 21,0};uchar tc0,tc1,sc0,sc1; /* 音长和音符两个计数器初值暂存 */void play(sound,dh,sj,speed,point1,point2)uchar code sound[]; /* 接受乐曲数组的地址 */uchar speed,sj,dh; /* 速度、八度、调号 */uint point1,point2; /* 乐曲开始、结束指针 */{uint code fftab[12]={262,277,294,311,330,349,369,392,415,440,466,494}; /* 频率表*/uchar code stab[7]={0,2,4,5,7,9,11}; /* 1~7 在频率表中的位置 */uchar code ltab[7]={1,2,4,8,16,32,64};uchar tl,ts,sl,sm,sh,slen,xg,ii,fd;uint point,hz,tc,sc,len,len0,len1,len2,len4,i,ftab[12]; speaker=1;for(i=0;i<12;i++) /* 根据调号及升降八度来计算音符频率 */ {ii=i+dh;if(ii>11){ii=ii-12;ftab[i]=fftab[ii]*2;}elseftab[i]=fftab[ii];if(sj==1) ftab[i]>>=2;if(sj==3) ftab[i]<<=2;}point=point1;ts=sound[point];tl=sound[point+1]; /* 读出第一个音符和它时时值 */tc=65535-10000/plen; /* 算出10ms 的初装值 */tc0=tc%256; /* 计算TL1 应装入的初值 */tc1=tc/256; /* 计算TH1 应装入的初值 */len0=12000/speed; /* 算出1 分音符的长度(几个10ms) */ len4=len0/4; /* 算出4 分音符的长度 */len4=len4-len4*yx; /* 普通音最长间隔标准 */TMOD=0x11;TH1=tc1; TL1=tc0;ET0=1; EA=1;TR0=0; TR1=1;while(point<=point2){sl=ts%10; /* 计算出音符 */sh=ts/100; /* 计算出是否升半 */sm=ts/10%10; /* 计算出高低音 */hz=ftab[stab[sl-1]+sh]; /* 查出对应音符的频率 */if(sl!=0){if (sm==1) hz>>=2; /* 若是低音 */if (sm==3) hz<<=2; /* 若是高音 */sc=(50000/hz)*10/plen; /* 计算脉冲个数 */sc=65536-sc; /* 计算计数器初值 */sc0=sc%256; /* 算出TL0 应装初值 */sc1=sc/256; /* 算出TH0 应装初值 */TH0=sc1; /* 装入初值 */TL0=sc0+12; /* 加12 是对中断延时的补偿 */}slen=ltab[tl%10]; /* 算出是几分音符 */xg=tl/10%10; /* 算出音符类型(0 普通1 连音2 顿音) */fd=tl/100;len=len0/slen; /* 算出连音音符演奏的长度(多少个10ms)*/ if (fd==1) len=len+len/2;if(xg!=1)if(xg==0) /* 算出普通音符的演奏长度 */if (slen<=4)len1=len-len4;elselen1=len*yx;elselen1=len/2; /* 算出顿音的演奏长度 */elselen1=len;if(sl==0) len1=0;len2=len-len1; /* 算出不发音的长度 */if (sl!=0)TR0=1;for(i=len1;i>0;i--) /* 发规定长度的音 */{while(TF1==0);TH1=tc1; TL1=tc0;TF1=0;}}if(len2!=0){TR0=0; speaker=1;for(i=len2;i>0;i--) /* 音符间的间隔 */{while(TF1==0);TH1=tc1; TL1=tc0;TF1=0;}}point+=2; /* 音符指针下移 */ts=sound[point]; tl=sound[point+1]; /* 读出下一个音符和它时时值 */}void yin() interrupt 1 /* 音符发生程序(中断服务程序)*/ {speaker=~speaker;TH0=sc1; TL0=sc0;}//==============================================main(){while(1){play(sound,0,2,60,0,57);play(sound,0,1,60,0,57);play(sound,0,3,60,0,57);play(sound,0,2,40,0,57);play(sound,5,2,60,0,57);play(sound,0,2,80,0,57);}}。
单片机蜂鸣器音乐代码
单片机蜂鸣器音乐代码在单片机的应用中,蜂鸣器是一个常用的音频输出设备。
它可以通过产生不同频率的声音来实现音乐播放、提醒和警报等功能。
本文将介绍如何使用单片机控制蜂鸣器播放音乐,并提供一个简单的音乐代码示例。
首先,让我们了解一下单片机蜂鸣器的工作原理。
蜂鸣器实际上是一个压电陶瓷元件,当电压作用于其上时,它会振动产生声音。
为了产生不同的音调,我们需要控制蜂鸣器的频率和占空比。
单片机通过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);}```以上代码中,我们首先定义了各个音符的频率,这些频率是通过实验测量得到的,具体数值可能因蜂鸣器型号、电源电压等因素而略有差异。
(完整word版)单片机控制蜂鸣器唱歌的原理
一般说来,单片机演奏音乐基本都是单音频率,它不包含相应幅度的谐波频率,也就是说不能象电子琴那样能奏出多种音色的声音。
因此单片机奏乐只需弄清楚两个概念即可,也就是“音调”和“节拍”。
音调表示一个音符唱多高的频率,节拍表示一个音符唱多长的时间。
1)音调的确定音调就是我们常说的音高。
它是由频率来确定的!我们可以查出各个音符所对应的相应的频率,那么现在就需要我们来用51 来发出相应频率的声音!我们常采用的方法就是通过单片机的定时器定时中断,将单片机上对应蜂鸣器的I/O 口来回取反,或者说来回清零,置位,从而让蜂鸣器发出声音,为了让单片机发出不同频率的声音,我们只需将定时器予置不同的定时值就可实现。
那么怎样确定一个频率所对应的定时器的定时值呢?以标准音高 A 为例:A 的频率 f = 440 Hz, 其对应的周期为: T = 1/ f = 1/440 =2272 μ s 那么,单片机上对应蜂鸣器的 I/O 口来回取反的时间应为:t = T/2 = 2272/2 = 1136 ,也μ就s是清零、置位在一个周期内完.成这个时间 t 也就是单片机上定时器应有的中断触发时间。
一般情况下,单片机奏乐时,其定时器为工作方式1,它以振荡器的十二分频信号为计数脉冲。
设振荡器频率为 f0 ,则定时器的予置初值由下式来确定:t = 12 * (TALL –THL ) / f0 式中 TALL= 216= 65536,T HL 为定时器待确定的计数初值。
因此定时器的高低计数器的初值为:TH =THL/ 256 = ( TALL –t*f0/12) / 256TL = THL % 256 = ( TALL –t*f0/12) %256将 t=1136 μs代入上面两式(注意:计算时应将时间和频率的单位换算一致)即可求出标准音高 A 在单片机晶振频率 f0=12Mhz,定时器在工作方式 1 下的时器高低计数器的予置初值为:TH440Hz = (65536–1136 * 12/12) /256 = FBHTL440Hz = (65536–1136 * 12/12)%256 = 90H就这样,我们通过延时,发出了我们所需要的频率以单片机 12MHZ 晶振为例,例出高中低音符与单片机计数 T0 相关的计数值如下表所示 :2)节拍的确定在一张乐谱中,我们经常会看到这样的表达式,如 1=C (4/4) 、1=G(3/4) ⋯⋯等等,这里 1=C (4/4),1=G(3/4) 表示乐谱的曲调,和我们前面所谈的音调有很大的关联, 4/4、 3/4 就是用来表示节拍的。
定时器实验(单片机控制蜂鸣器播放曲子——最炫民族风)
#include <REGX51.H>unsigned char h, l, time;sbit beep = P3^7;code unsigned char high[] = {0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8, //低音12345670xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, //1,2,3,4,5,6,7,0xFC, 0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, //高音i2345670xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF}; //超高音1234567code unsigned char low[] = {0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6, //低音12345670x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, //1,2,3,4,5,6,7,0x8F, 0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D, //高音i2345670x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16}; //超高音1234567code unsigned char music[] ={6,1,2, 6,1,1, 5,1,1, 6,1,2, 6,1,1, 1,2,1, 1,2,2, 2,2,1, 1,2,1, 6,1,4, 1,2,2, 1,2,1, 5,1,1, 1,2,1,2,2,1, 3,2,1, 5,2,1, 5,2,1, 3,2,1, 2,2,2, 3,2,4, 6,2,1, 6,2,1, 6,2,1, 5,2,1, 3,2,1, 3,2,2, 1,2,1, 6,1,1,6,1,1, 6,1,1, 3,2,1, 2,2,4, 3,2,1, 3,2,1, 5,2,1, 3,2,1, 2,2,1, 3,2,1, 2,2,1, 1,2,1, 6,1,1, 5,1,1, 6,1,4,3,2,1, 3,2,1, 5,2,1, 3,2,1, 3,2,1, 5,2,1, 5,2,1, 6,2,1, 1,3,1, 6,2,1, 5,2,2, 6,2,4, 6,1,2, 6,1,1, 5,1,1,6,1,2, 1,2,2, 2,2,1, 3,2,1, 2,2,1, 1,2,1, 2,2,1, 3,2,4, 6,1,1, 6,2,1, 6,2,1, 5,2,1, 2,2,1, 3,2,1, 2,2,1, 0,0,0,};void nint() interrupt 1 //T0中断程序,控制发音的音调{TR0 = 0; //先关闭T0beep =~beep; //输出方波, 发音TH0 = h; //下次的中断时间, 这个时间, 控制音调高低TL0 = l;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 < 4000; t2++); //延时期间, 可进入T0中断去发音TR0 = 0; //关闭T0, 停止发音}void song() //演奏一个音符{TH0 = h; //控制音调TL0 = l;TR0 = 1; //启动T0, 由T0输出方波去发音beep =~beep;delay(time); //控制时间长度}void main() //j表示第n+1首曲{ unsigned char k, i;i = 0;TMOD = 1; //置T0定时工作方式1ET0 = 1; //开T0中断EA = 1;time = 1;while(time) {k = music[i] + 7 * music[i + 1] - 1;//第i个是音符, 第i+1个是第几个八度h = high[k]; //从数据表中读出频率数值l = low[k]; //实际上, 是定时的时间长度time = music[i + 2]; //读出时间长度数值i += 3;song(); //发出一个音符}}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
单片机控制蜂鸣器鸣奏音乐
——中北大学:马政贵
首先介绍蜂鸣器的发声原理。
我们都知道,音调和音调的时长是音符的主要特征,通过产生不同的音调和音调的时长可以奏出不同的音符来。
然后一个个音符串联在一起就可以产生美妙的音乐来了。
音调主要由声音的频率决定,通过单片机给蜂鸣器不同的音频脉冲来产生不同的音调。
要产生音频脉冲,只要算出某一音频的周期(周期=1/频率),然后将此周期除以2即为半周期的时间。
利用单片机的定时器工作在计数模式MODE1下,设定TH0和TL0的值以产生这半个周期,每当计时到达时就将输出脉冲的I/O(即接蜂鸣器的那个管脚)反相,然后重复计时此半个周期再对I/O反相,就可以在I/O引脚上得到此频率的脉冲。
如果没有必要进行精确的计时,可以用for循环空语句来粗略计时即可(本文选用此法)。
当单片机使用11.0592Mhz的晶振时,for(i=0;i<115;i++);这个空循环延时约为1ms;当晶振选用12Mhz时,可使用for(i=0;i<125;i++);这个空循环来延时1ms。
在这个空循环外头再进行一次循环就可以实现延时若干ms。
如:
知道如何产生不同的频率的音调和进行音调的延时的之后,我们便可以编写程序来让单片机控制蜂鸣器来鸣奏音乐了。
下面附上不同音调所对应的频率表:
下文介绍了让单片机控制蜂鸣器进行鸣奏音乐的两种方法。
第一种方法是基础的方法,就是顺序地让蜂鸣器挨个地演奏每个音符。
方法一:(此歌曲是《莫斯哥郊外的晚上》的乐曲)
#include <reg52.h>
sbit fmq=P3^6;
void delay(unsigned int a)
{
unsigned char b;
while(a--)
{
for(b=0;b<115;b++) ;
}
}
void yanzou(unsigned char i,unsigned int pai)
{
unsigned char y;
unsigned int j;
for(j=0;j<pai;j++)
{
fmq=0;
for(y=0;y<i;y++) ;
fmq=1;
for(y=0;y<i;y++) ;
}
}
void main()
{
while(1)
{
yanzou(65,220); //1/2拍中音6
yanzou(55,262); //1/2拍高音1
yanzou(44,330); //1/2拍高音3
yanzou(55,262); //1/2拍高音1
yanzou(49,587); //1拍高音2
yanzou(55,262); //1/2拍高音1
yanzou(58,247); //1/2拍中音7
yanzou(44,660); //1拍高音3
yanzou(49,587); //1拍高音2
yanzou(65,880); //2拍中音6
yanzou(55,262); //1/2拍高音1
yanzou(44,330); //1/2拍高音3
yanzou(37,392); //1/2拍高音5
yanzou(37,392); //1/2拍高音5
yanzou(65,440); //1拍中音6
yanzou(37,392); //1/2拍高音5
yanzou(41,349); //1/2拍高音4
yanzou(44,2640); //4拍高音3
yanzou(41,698); //1拍高音4
yanzou(37,784); //1拍高音5
yanzou(29,492); //1/2拍高音7
yanzou(33,440); //1/2拍高音6
yanzou(44,660); //1拍高音3
delay(250);
yanzou(58,494); //1拍中音7
yanzou(65,220); //1/2拍中音6
yanzou(44,330); //1/2拍高音3
yanzou(49,293); //1/2拍高音2
yanzou(41,698); //1拍高音4
yanzou(41,698); //1拍高音4
yanzou(37,392); //1/2拍高音5
yanzou(41,349); //1/2拍高音4
yanzou(44,660); //1拍高音3
yanzou(49,293); //1/2拍高音2
yanzou(55,262); //1/2拍高音1
yanzou(44,660); //1拍高音3
yanzou(49,587); //1拍高音2
yanzou(65,880); //2拍中音6
yanzou(65,880); //2拍中音6
yanzou(41,698); //1拍高音4
yanzou(37,784); //1拍高音5
yanzou(29,492); //1/2拍高音7
yanzou(33,440); //1/2拍高音6
yanzou(44,660); //1拍高音3
delay(250);
yanzou(58,494); //1拍中音7
yanzou(65,220); //1/2拍中音6
yanzou(44,330); //1/2拍高音3
yanzou(49,293); //1/2拍高音2
yanzou(41,698); //1拍高音4
yanzou(41,698); //1拍高音4
yanzou(37,392); //1/2拍高音5
yanzou(41,349); //1/2拍高音4
yanzou(44,660); //1拍高音3
yanzou(49,293); //1/2拍高音2
yanzou(55,262); //1/2拍高音1
yanzou(44,660); //1拍高音3
yanzou(49,587); //1拍高音2
yanzou(65,880); //2拍中音6
yanzou(65,880); //2拍中音6
delay(5000);
}
}
观察方法一的主函数可以发现,每个语句的形式和实现的功能都是一样的,于是可以想到用数组来进行代码的简化。
方法二便是通过数组来实现的。
方法二:(此乐曲是《让我们荡起双桨》)
#include <reg52.h>
sbit fmq=P3^6;
sbit start=P1^4; //定义了一个按键进行控制,当按键按下时,开始演奏音乐
void delay(unsigned int i)
{
unsigned char j;
while(i--)
{
for(j=0;j<115;j++);
}
}
void yanzou(unsigned char pinlv,unsigned int jiepai)
{
unsigned char pl;
unsigned int jp;
if(jiepai==1) delay(250); //1/2拍暂停,即1/2拍的0
else if(jiepai==2) delay(500); //1拍暂停,即1拍的0
else
{
for(jp=0;jp<jiepai;jp++)
{
fmq=0;
for(pl=0;pl<pinlv;pl++);
fmq=1;
for(pl=0;pl<pinlv;pl++);
}
}
}
void main()
{
unsigned char i,x;
unsigned char code pinlv[]={131,110,98, 87, 73, 87, 110,98, 131,0,110,98, 87, 73, 73, 65, 98, 87,87,87,73,65,73,65,55,58,65,73,65,87,110,98,87,73,110,131,110,98,87,65,73,73,0,87,65,65,73,82,87, 98,87,73,131,110,98,0,110,98,87,73,65,55,58,65,73,87,65,65};
unsigned int code jiepai[]={110,131,147,494,196,165,131,294,440,1,131,147,165,588,196,440,294,660,330,165,196,880, 588,220,262,124,110,196,220,330,131,147,495,196,262,220,131,147,165,220,784,392,2,660,660,220,1 96,175,330,588,495,196,110,131,147,2,131,147,330,392,440,524,247,220,196,165,880,880};
i=68; //数组共有68个元素
if(start==0)
for(x=0;x<i;x++)
{
yanzou(pinlv[x],jiepai[x]);
}
}
方法二的代码比方法一的大大简化了,当乐曲的音符太多时,可以选用方法二,这样,即使乐曲再长,也不过是在数组里多添上几个元素罢了。
感兴趣的朋友,可以复制上述代码,编译成hex 文件后烧进单片机中就可以实现单片机控制蜂鸣器们鸣奏乐曲了。
(方法二中,有个按键功能,按下按键后才开始演奏。
)。