单片机蜂鸣器播放音乐

合集下载

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

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

课程设计:嵌入式系统应用题目名称:利用蜂鸣器实现音乐播放功能姓名:学号:班级:完成时间: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置位,蜂鸣器停止发声。

蜂鸣器简单连接方法

蜂鸣器简单连接方法

蜂鸣器简单连接方法蜂鸣器是一种常用的电子元件,可用于发出声音信号。

它常用于电子设备、警报器、报警设备等,具有广泛的应用领域。

本文将介绍蜂鸣器的简单连接方法,以帮助初学者快速上手使用蜂鸣器。

材料准备在进行蜂鸣器连接之前,我们首先需要准备以下材料:- 蜂鸣器:蜂鸣器可以分为有源蜂鸣器和无源蜂鸣器两种类型。

有源蜂鸣器需要外部电源驱动,而无源蜂鸣器可以直接使用单片机的IO口来驱动。

根据需要选择相应类型的蜂鸣器。

- 连线:根据连接方式的不同,可以选择杜邦线、导线、焊接等方式进行蜂鸣器与其他电子设备的连接。

连接步骤有源蜂鸣器连接方法有源蜂鸣器常用于电子设备中,需要外部电源进行驱动。

以下是有源蜂鸣器的简单连接步骤:1. 准备有源蜂鸣器、面包板、杜邦线和电源。

2. 将有源蜂鸣器的正极(一般为红色线)连接至电源的正极。

3. 将有源蜂鸣器的负极(一般为黑色线)连接至电源的负极。

4. 将有源蜂鸣器的信号线(一般为白色线或单色线)连接至需要发出声音的电子设备的输出端。

5. 打开电源,观察有源蜂鸣器是否发出声音。

如果没有声音,请检查连接是否正确,并确保电源正常。

无源蜂鸣器连接方法无源蜂鸣器与有源蜂鸣器相比,它可以直接使用单片机的IO口来驱动,不需要外接电源。

以下是无源蜂鸣器的简单连接步骤:1. 准备无源蜂鸣器、面包板、杜邦线和单片机。

2. 将无源蜂鸣器正极(一般为红色线)连接至单片机的IO口。

3. 将无源蜂鸣器负极(一般为黑色线)连接至单片机的GND端口,即地。

4. 通过编程控制单片机的IO口输出高低电平,即可控制无源蜂鸣器发声或停止发声。

需要注意的是,无源蜂鸣器在直流电压下无法直接发声,需要通过单片机的IO 口连接时序开关电路,即周期性地控制IO口的高低电平来模拟声音。

进一步应用蜂鸣器作为一种常用的声音发生器,广泛应用于警报声、提醒声等场合。

在了解了蜂鸣器的简单连接方法之后,我们可以进一步应用蜂鸣器来实现一些有趣的功能,如:- 定时闹钟:通过编程控制蜂鸣器发出一定频率的声音,实现定时提醒的功能。

Arduino控制蜂鸣器播放音乐设计说明书

Arduino控制蜂鸣器播放音乐设计说明书

目录一、新媒体装置艺术1、作品原理介绍 (1)2、作品设计的意义 (1)3、作品的主要内容 (1)4、制作方法和流程 (1)5、成果 (9)6、价值和影响 (9)7、创新点 (10)二、结论8、技术方面 (10)9、艺术方面 (10)10、不足及展望 (11)11、谢辞 (11)设计说明书一、作品原理介绍:能感应到物体靠近并且低于50CM时,蜂鸣器发出音乐,并且小灯泡随着音乐节奏变化。

二、作品设计意义:将它应用到图书馆或者购物的地方,当人们靠近的时间能只能朗读出这一栏或者这一块区域有什么东西,能够更加让人们更快速的找到自己需要的东西,很大程度上节约的时间! 也可以应用到车上,当开车的人快要和前面或者后面的物体撞上的时候,会发出声音来提醒你,从而给生命和财产加上保护套。

三、作品主要内容:1、Arduino控制蜂鸣器播放音乐、小灯泡。

2、小灯泡跟随蜂鸣器的音乐节奏变化。

3、当物体离超声波低于50CM时发出指令,控制蜂鸣器、小灯泡。

四、制作方法和流程:adruino uno一块(其他Arduino板子也可,注意引脚就行),面保线若干条,蜂鸣器或小喇叭一个。

原理:首先讲下简单的乐理知识,知道音乐是怎么演奏出来的自然就可以通过代码来进行编排了。

1.演奏单音符的原理一首音乐由若干音符组成,每一个音符唯一对应一个频率。

如果我们知道了音符相对应的频率,再让 Arduino 按照这个频率输出到蜂鸣器或喇叭,蜂鸣器或喇叭就会发出相应频率下的声音。

Arduino官方网站给出了不同音符对应的不同频率的头文件,具体请见下文介绍。

2.音符演奏的持续时间每个音符都会播放一定的时间,这样才能构成一首优美的曲子,而不是每个音符都播放一样长的时间。

如何确定每个音符演奏的单位时间呢?我们知道,音符节奏分为1拍、1/2拍、1/4拍、1/8拍等等,我们规定一拍音符的时间为1;半拍为0.5;1/4拍为0.25;1/8拍为0.125……,所以我们可以为每个音符赋予这样的拍子播放出来,音乐就成了。

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;

单片机MSP430G2课程设计音乐播放器

单片机MSP430G2课程设计音乐播放器

单片机期末检测报告学生姓名:***学生学号:********专业班级:自动化12-2班基于MSP430G2的音乐切换器一、内容通过MSP430G2播放自己所设置的歌曲,并通过按键S2切换另一首歌曲二、思路与方法(1)思路:通过老师上课所讲的F6638音乐器播放实例,想利用MSP430G2来进行音乐播放,阅读网上单片机播放音乐例程并加以改编,并试想利用按键S2来进行歌曲的实时切换(2)音乐:通过MSP430蜂鸣器音高音长对照表,将自己喜欢的音乐通过音乐简谱改成相应代码,利用播放函数play_song()进行歌曲播放。

演奏乐曲对于一个音符应该包括两个部分,声调用简单的延时-电平翻转来实现,改变了延时的时间就改变了声调,而时间通过计数比较来实现,当计数值相等时就跳出循环演奏下一个音符。

(3)按键S2:通过中断服务、事件检测、事件处理函数,通过按键S2切换歌曲(4)硬件:无源蜂鸣器、MSP430G2单片机有流程图:三、程序调试(1)遇到的问题与解决方法按键S2切换歌曲开始不能进行实时切换,首先是我将实验是检测按键的程序进行整改加入主程序中while(1),这样只有长按S2键才能播放下一曲。

后来查阅书籍关于MSP430G2中断服务的程序后,调用这些函数,并设置变量i放在两个音乐播放函数中,通过判断i=1或0进行选歌。

开始编曲时候并未按照音高、音长对照表进行编曲,所以导致歌曲无调子,后在搜集到资料后进行整改进行改曲。

开始蜂鸣器声音略小,后发现是正负导线接反所致。

(2)程序段/*********************************************时钟频率务必为8MHz,定时器为8分频*********************************************/#include<msp430g2553.h>typedef unsigned char uchar;#include"music.h"//乐曲1#include"te.h"//乐曲2#define Buzzer BIT3#define Buzzer_Port P2OUT#define Buzzer_DIR P2DIRuchar counter;void Play_Song(void);void Delay_Nms(uchar n);void ss(void);void P1_IODect();void P13_Onclick();static int i=0;/***************主函数****************/void main(void){WDTCTL = WDTPW + WDTHOLD;//关闭看门狗P1DIR |= BIT0;P1OUT |= BIT0;P1REN |= BIT3;P1OUT |=BIT3;P1DIR &=~BIT3;P1IES |= BIT3;P1IE |= BIT3;BCSCTL1=CALBC1_8MHZ; //晶振选择DCO中的8MHzDCOCTL=CALDCO_8MHZ; //选择系统主时钟为8MHz//CCTL0 = CCIE;CCR0 = 7200; //设定拍速TACTL |= TASSEL_2 + ID_3; //TimerA定时器分频要选8分频 Buzzer_DIR |= Buzzer; //设置控制蜂鸣器的IO方向为输出 _EINT(); //打开全局中断//循环演奏歌曲while(1){if (i==0)//按键没按下{Play_Song();}else {ss();}}}/*******************************************函数名称:TimerA_ISR功能:定时器A的中断服务函数********************************************/#pragma vector = TIMER0_A0_VECTOR__interrupt void TimerA_ISR(void){counter++;}/*******************************************函数名称:PORT1_ISR功能:响应p1口的外部中断服务********************************************/#pragma vector =PORT1_VECTOR__interrupt void PORT1_ISR(void){P1_IODect();P1IFG=0;}/*******************************************函数名称:P1_IODect()功能:判断具体引发中断的I/O,并调用相应I/O的中断事件处理函数********************************************/void P1_IODect(){unsigned int Push_Key=0;Push_Key=P1IFG&(~P1DIR);__delay_cycles(10000);if((P1IN&Push_Key)==0){switch(Push_Key){case BIT3:P13_Onclick(); break;default: break;}}}/*******************************************函数名称:P13_Onclick()功能:事件处理函数********************************************/void P13_Onclick(){if(i==0){i=1;}elsei=0;P1OUT ^=BIT0;}/*******************************************函数名称:Delay_Nms功能:延时N个ms的函数参数:n--延时长度返回值:无********************************************/void Delay_Nms(uchar n){uchar i,j;for( i = 0;i < n; i++ ){for( j = 0;j < 3;j++ )_NOP();}}/*******************************************函数名称:Play_Songss********************************************/void Play_Song(void){uchar Temp1,Temp2;//Temp1放音调决定了音调的高低,Temp2放音长决定了某个音的演奏时间uchar addr = 0; //SONG数组中每两个为一组第一字节为音调,第二字节为音长counter = 0; //中断计数器清0while(i==0){Temp1 = songsong[addr++];if ( Temp1 == 0xFF ) //休止符{TACTL &=~MC_1; //停止计数Delay_Nms(100);}else if ( Temp1 == 0x00 ) //歌曲结束符{return;}else{Temp2 = songsong[addr++];TACTL |=MC_1; //开始计数while(1){Buzzer_Port ^= Buzzer;Delay_Nms(Temp1);if ( Temp2 == counter ){counter = 0;break;}}}}}void ss(void){uchar Temp1,Temp2;//Temp1放音调决定了音调的高低,Temp2放音长决定了某个音的演奏时间uchar addr = 0; //SONG数组中每两个为一组第一字节为音调,第二字节为音长counter = 0; //中断计数器清0while(i==1){Temp1 = gg[addr++];if ( Temp1 == 0xFF ) //休止符{TACTL &=~MC_1; //停止计数Delay_Nms(100);}else if ( Temp1 == 0x00 ) //歌曲结束符{return;}else{Temp2 = gg[addr++];TACTL |=MC_1; //开始计数while(1){Buzzer_Port ^= Buzzer;Delay_Nms(Temp1);if ( Temp2 == counter ){counter = 0;break;}}}}}const unsigned char songsong[]= //歌曲1 CCR0=7200,格式为: 频率常数, 节拍常数, 频率常数, 节拍常数,{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,}; const unsigned char gg[]= //歌曲2 CCR0=7200{ 0x18, 0x30, 0x1C , 0x10, 0x20, 0x40, 0x1C , 0x10,0x18, 0x10, 0x20 , 0x10,0x1C, 0x10, 0x18 , 0x40,0x1C, 0x20, 0x20 , 0x20,0x1C, 0x20, 0x18 , 0x20,0x20, 0x80, 0xFF , 0x20,0x30, 0x1C, 0x10 , 0x18,0x20, 0x15, 0x20 ,0x1C,0x20, 0x20, 0x20 , 0x26,0x40, 0x20, 0x20 , 0x2B,0x20, 0x26, 0x20 , 0x20,0x20, 0x30, 0x80 , 0xFF,0x20, 0x20, 0x1C , 0x10,0x18, 0x10, 0x20 , 0x20,0x26, 0x20, 0x2B , 0x20,0x30, 0x20, 0x2B , 0x40,0x20, 0x20, 0x1C , 0x10,0x18, 0x10, 0x20 , 0x20,0x26, 0x20, 0x2B , 0x20,0x30, 0x20, 0x2B , 0x40,0x20, 0x30, 0x1C , 0x10,0x18, 0x20, 0x15 , 0x20,0x1C, 0x20, 0x20 , 0x20,0x26, 0x40, 0x20 , 0x20,0x2B, 0x20, 0x26 , 0x20,0x20, 0x20, 0x30 , 0x80,0x20, 0x30, 0x1C, 0x10,0x20, 0x10, 0x1C , 0x10,0x20, 0x20, 0x26 , 0x20,0x2B, 0x20, 0x30 , 0x20,0x2B, 0x40, 0x20 , 0x15,0x1F, 0x05, 0x20 , 0x10,0x1C, 0x10, 0x20 , 0x20,0x26, 0x20, 0x2B , 0x20,0x30, 0x20, 0x2B , 0x40,0x20, 0x30, 0x1C , 0x10,0x18, 0x20, 0x15 , 0x20,0x1C, 0x20, 0x20 , 0x20,0x26, 0x40, 0x20 , 0x20,0x2B, 0x20, 0x26 , 0x20,0x20, 0x20, 0x30 , 0x30,0x20, 0x30, 0x1C , 0x10,0x18, 0x40, 0x1C , 0x20,0x20, 0x20, 0x26 , 0x40,0x13, 0x60, 0x18, 0x20,0x15, 0x40, 0x13 , 0x40,0x18, 0x80, 0x00 };四、调试结果红灯亮第一首歌:红灯灭第二首歌:结果说明:调试结果,达到了预期通过S2切换歌曲的功能五、总结与体会通过这次的课程设计,我学会了MSP430单片机定时器、中断服务模块、I/O 输入输出系统等。

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

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

单片机驱动蜂鸣器音乐生日快乐的播放程序;音乐生日快乐的播放;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文件。

单片机蜂鸣器音乐

单片机蜂鸣器音乐

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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); //播放}}。

单片机课程设计之音乐播放器

单片机课程设计之音乐播放器

文档从互联网中收集,已重新修正排版,word格式支持编辑,如有帮助欢迎下载支持。

单片机课程设计之音乐播放器学生姓名学号所在学院专业名称班级指导教师成绩目录一、设计功能 ................................................................................................................. 错误!未定义书签。

(一)基本功能 ..................................................................................................... 错误!未定义书签。

(二)扩展功能 ..................................................................................................... 错误!未定义书签。

二、设计原理 ................................................................................................................. 错误!未定义书签。

(一)单片机产生不同频率脉冲信号的原理:...................................................... 错误!未定义书签。

三、方案与论证 ............................................................................................................. 错误!未定义书签。

四、设计内容 ................................................................................................................. 错误!未定义书签。

单片机蜂鸣器发声原理

单片机蜂鸣器发声原理

单片机蜂鸣器发声原理
单片机蜂鸣器发声原理可以简单描述为以下步骤:
1. 准备工作:首先,需要将蜂鸣器连接到单片机的一个输出口(一般为I/O引脚)。

蜂鸣器有两个引脚,一个为正极(VCC),一个为负极(GND)。

2. 产生方波:在单片机中,通过对蜂鸣器所连接的引脚设置为高电平和低电平之间的快速切换,能够产生一个频率一定、占空比可调节的方波信号。

这个方波信号就是蜂鸣器声音的基准信号。

3. 驱动蜂鸣器:通过不断地将方波信号发送到连接蜂鸣器的引脚,单片机就能够驱动蜂鸣器发出声音。

当方波信号为高电平时,蜂鸣器内部的振膜向前位移,当方波信号为低电平时,蜂鸣器内部的振膜向后位移。

这种间隔的振动就会产生声音。

4. 控制频率和占空比:通过改变方波信号的高电平和低电平之间的时间长度,可以控制蜂鸣器发声的频率和占空比。

频率决定了声音的音调,而占空比则决定了声音的响度和持续时间。

通过上述步骤,单片机可以通过控制蜂鸣器的引脚输出方波信号,从而产生不同频率、不同音调的声音。

这就是单片机蜂鸣器发声的原理。

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

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

单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安/*生日快乐歌曲*/#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;//播放}}。

单片机控制蜂鸣器唱歌的原理

单片机控制蜂鸣器唱歌的原理

单片机控制蜂鸣器唱歌的原理Document serial number【NL89WT-NY98YT-NC8CB-NNUUT-NUT108】一般说来,单片机演奏音乐基本都是单音频率,它不包含相应幅度的谐波频率,也就是说不能象电子琴那样能奏出多种音色的声音。

因此单片机奏乐只需弄清楚两个概念即可,也就是“音调”和“节拍”。

音调表示一个音符唱多高的频率,节拍表示一个音符唱多长的时间。

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,THL为定时器待确定的计数初值。

因此定时器的高低计数器的初值为: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 就是用来表示节拍的。

单片机按键控制蜂鸣器发声程序

单片机按键控制蜂鸣器发声程序
void Time0_Init()//定时器T0方式1,定时10ms
{
TMOD = 0x01;
IE= 0x82;
TH0 = 0xDC;
TL0 = 0x00;
}
void Time0_Int() interrupt 1
{
TH0 = 0xDC;
TL0 = 0x00;
Count++;
}
void delay (uint8 k)//按键防抖延时
{
return;
}
else
{
Temp2 = SONG[Addr++];
TR0 = 1;
while(1)
{
Speak = ~Speak;
Delay_xMs(Temp1);
if(Temp2 == Count)
{CΒιβλιοθήκη unt = 0;break;
}
}
}
}
}
void keyscan (void)//按键切换声音函数
单片机按键控制蜂鸣器发声程序单片机蜂鸣器音乐程序单片机蜂鸣器程序单片机蜂鸣器报警程序蜂鸣器发声程序单片机按键程序单片机独立按键程序单片机按键扫描程序单片机按键消抖程序单片机按键中断程序
#include<reg52.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
{
uint8 Temp1,Temp2;
uint8 Addr;
Count = 0;//中断计数器清0
Addr = i *3;
while(1)
{
Temp1 = SONG[Addr++];

单片机控制蜂鸣器鸣奏音乐

单片机控制蜂鸣器鸣奏音乐

单片机控制蜂鸣器鸣奏音乐——中北大学:马政贵首先介绍蜂鸣器的发声原理。

我们都知道,音调和音调的时长是音符的主要特征,通过产生不同的音调和音调的时长可以奏出不同的音符来。

然后一个个音}}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++) ;yanzou(55,262); //1/2拍高音1yanzou(58,247); //1/2拍中音7 yanzou(44,660); //1拍高音3 yanzou(49,587); //1拍高音2yanzou(65,880); //2拍中音6yanzou(44,330); //1/2拍高音3 yanzou(37,392); //1/2拍高音5yanzou(37,392); //1/2拍高音5 yanzou(65,440); //1拍中音6yanzou(41,698); //1拍高音4yanzou(41,698); //1拍高音4yanzou(37,392); //1/2拍高音5yanzou(41,349); //1/2拍高音4yanzou(44,660); //1拍高音3yanzou(55,262); //1/2拍高音1 yanzou(44,660); //1拍高音3 yanzou(49,587); //1拍高音2 yanzou(65,880); //2拍中音6yanzou(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拍高音1yanzou(49,587); //1拍高音2yanzou(65,880); //2拍中音6yanzou(65,880); //2拍中音6delay(5000);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拍的0else if(jiepai==2) delay(500); //1拍暂停,即1拍的0elseunsigned 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 codejiepai[]={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,196,175,330,588,495,196,11 0,131,147,2,131,147,330,392,440,524,247,220,196,165,880,880};。

单片机蜂鸣器音乐代码

单片机蜂鸣器音乐代码

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

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

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

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

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

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

单片机通过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);}```以上代码中,我们首先定义了各个音符的频率,这些频率是通过实验测量得到的,具体数值可能因蜂鸣器型号、电源电压等因素而略有差异。

单片机课程设计-基于STC89C52RC单片机的音乐播放器设计

单片机课程设计-基于STC89C52RC单片机的音乐播放器设计

单⽚机课程设计-基于STC89C52RC单⽚机的⾳乐播放器设计基于STC89C52RC单⽚机的⾳乐播放器设计⼀、设计要实现的功能(1)利⽤I/O⼝产⽣⼀定频率的⽅波,驱动蜂鸣器,发出不同的⾳调,从⽽演奏歌曲(总共九⾸)。

(2)采⽤共阳七段数码管显⽰当前播放歌曲的序号。

(3)可以通过按键实现暂停、继续、上⼀⾸、下⼀⾸以及复位等基本操作。

⼆、设计所需基础知识储备1.要产⽣⾳频脉冲,只要计算某⼀⾳频的周期,然后将此周期处以2,即为半周期的时间。

利⽤定时器记时这个个半周期时间,每当记时到后就输出脉冲的I/O反相,然后重复记时此半周期的时间再对I/O 反相,就可在I/O脚上得到此频率的脉冲。

2.利⽤8051的内部定时器使其⼯作在计数器模式MODE1下,改变计数值TH0及TL0以产⽣不同频率的⽅法,例如频率为523HZ,其周期T=1/523=1912us,因此只要令计数器记时956us/1us=956,在每次计数956次时将I/O反相,就可得到中⾳DO(523HZ)。

计数脉冲值与频率的关系公式如下:N=Fi/2/Fr,其中N是计数值,Fi是8051内部频率,⼀般⽤的是1MHZ(因为8051内部记时⼀次时间为1us),Fr为要产⽣的频率。

3.计数初值的求法如下:T=65536-N=65536-Fi/2/Fr例如:求低⾳DO(261HZ)中⾳DO(523HZ)⾼⾳DO(1046HZ)的计数值。

T=65536-N=65536- Fi/2/Fr=65536-1000000/2/Fr=65536-50000/Fr低⾳DO的T=65536-50000/262=63267中⾳DO的T=65536-50000/523=64580⾼⾳DO的T=65536-50000/1047=65059三、硬件设计本次设计⽤到的主要器件有STC89C52,七段共阳数码管,⽆源蜂鸣器(详细列表在后⾯列出)。

硬件原理图也在后⾯列出。

四、功能说明(1)硬件电路中P1.0⼝为⾳频输出端⼝。

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

#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;}。

相关文档
最新文档