基于51单片机蜂鸣器发声的-C语言程序

合集下载

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

蜂鸣器音乐程序设定C语言

蜂鸣器音乐程序设定C语言

4.4.4.4.1111 音调音调音调音调、、、、节拍节拍节拍节拍以及以及以及以及编码的确定编码的确定编码的确定编码的确定方法方法方法方法一般说来,单片机演奏音乐基本都是单音频率,它不包含相应幅度的谐波频率,也就是说不能像电子琴那样能奏出多种音色的声音。

因此单片机奏乐只需弄清楚两个概念即可,也就是“音调”和节拍表示一个音符唱多长的时间。

4.4.4.4.1111.1.1.1.1 音调的确定音调的确定音调的确定音调的确定不同音高的乐音是用C、D、E、F、G、A、B来表示,这7个字母就是音乐的音名,它们一般依次唱成DO、RE、MI、FA、SO、LA、SI,即唱成简谱的1、2、3、4、5、6、7,相当于汉字“多来米发梭拉西”的读音,这是唱曲时乐音的发音,所以叫“音调”,即Tone。

把C、D、E、F、G、A、B这一组音的距离分成12个等份,每一个等份叫一个“半音”。

两个音之间的距离有两个“半音”,就叫“全音”。

在钢琴等键盘乐器上,C–D、D–E、F–G、G–A、A–B两音之间隔着一个黑键,他们之间的距离就是全音;E–F、B–C两音之间没有黑键相隔,它们之间的距离就是半音。

通常唱成1、2、3、4、5、6、7的音叫自然音,那些在它们的左上角加上﹟号或者b号的叫变化音。

﹟叫升记号,表示把音在原来的基础上升高半音,b叫降记音,表示在原来的基础上降低半音。

例如高音DO的频率(1046Hz)刚好是中音DO的频率(523Hz)的一倍,中音DO的频率(523Hz)刚好是低音DO频率(266 Hz)的一倍;同样的,高音RE的频率(1175Hz)刚好是中音RE的频率(587Hz)的一倍,中音RE的频率(587Hz)刚好是低音RE频率(294 Hz)的一倍。

1)要产生音频脉冲,只要算出某一音频的周期(1/频率),然后将此周期除以2,即为半周期的时间。

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

单片机蜂鸣器音乐

单片机蜂鸣器音乐

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

51单片机四路抢答器(LED灯,数码管显示,蜂鸣器提示音)C语言源程序

51单片机四路抢答器(LED灯,数码管显示,蜂鸣器提示音)C语言源程序

51单片机四路抢答器(LED灯,数码管显示,蜂鸣器提示音)C语言源程序2009-10-31 10:53其实就是在原有的基础上,加入数码管显示及蜂鸣器,当然根据自己的要求,适当使用单片机I/O口接线为:P0 P2 来控制数码管显示,其中P0为数码管显字控制,P2用来选择位(第几个数码管)P1用来控制8个LED灯P3,独立按键(可以根据需要修改)P3^5(找了一个没有用到的I/O口,当然,可以用键盘扫描的方式来实现,这样的话,可以实现4*4=16路的抢答器,了解原理,做相应修改即可。

#include <reg52.h>sbit key1=P3^0; //这里采用独立按键(4路)sbit key2=P3^1;sbit key3=P3^2;sbit key4=P3^3;sbit SPK=P3^5; //蜂鸣器,最好在ISP编程时先不接入,(我用的是杜邦线,可以设置跳线控制)void delay(unsigned int cnt){while(--cnt);}void speak(unsigned int j){unsigned int i;for(i=0;i<j;i++)//喇叭发声的时间循环,改变大小可以改变发声时间长短{delay(180);//参数决定发声的频率,估算值,自行修改到不刺耳的声调SPK=!SPK;}SPK=1; //喇叭蜂鸣器停止工作,叫一声即可。

}void main(){bit Flag;while(!Flag){if(!key1){P1=0xFE;Flag=1;speak(300);P2=0;P0=0x06;} //LED1,数码管1显示1,蜂鸣器叫else if(!key2){P1=0xFD;Flag=1;speak(300);P2=1;P0=0x5b;}//LED2,数码管2显示2,蜂鸣器叫else if(!key3){P1=0xFB;Flag=1;speak(300);P2=2;P0=0x4f;}//LED3,数码管3显示3,蜂鸣器叫else if(!key4){P1=0xF7;Flag=1;speak(300);P2=3;P0=0x66;}LED4,数码管4显示4,蜂鸣器叫}while(Flag);}测试完,手动复位即可,当然可设置相应的按键来控制标志:Flag,进行继续抢答。

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

51单片机实现蜂鸣器警车、救护车、消防车声

51单片机实现蜂鸣器警车、救护车、消防车声

/*-----------------------------------------------
名称:喇叭
论坛:
编写:shifang
日期:2009.5
修改:无
内容:模救护车发声
------------------------------------------------*/
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}/*------------------------------------------------
定时器中断子程序
------------------------------------------------*/
main()
{
unsigned char i;
Init_Timer0(); //初始化定时器
while(1)
{ቤተ መጻሕፍቲ ባይዱ
frq=0;
for(i=0;i<60;i++)//播放1s左右一种频率
{
DelayMs(10);
}
frq=100;
for(i=0;i<60;i++)//播放1s左右另外一种频率
{
DelayMs(10);
//TH0=0x00;//给定初值
//TL0=0x00;
EA=1; //总中断打开
ET0=1; //定时器中断打开
TR0=1; //定时器开关打开
}
/*------------------------------------------------
主函数

基于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里面的数组来达到连续播放各种不同音调的音的目的,这样听起来就像一首曲子了。
以下是我按以上方法编写的一个小程序:

基于51单片机蜂鸣器发声的-C语言程序

基于51单片机蜂鸣器发声的-C语言程序

本例使用延时函数实精品文档按键发音/* 名称:按键发音说明:按下不同的按键会是SOUNDE 发出不同频率的声音。

现不同频率的声音输出,以后也可使用定时器*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit BEEP=P3A 7;sbit K 仁 P1A4;sbit K2=P1A5;sbit K3=P1A6;sbit K4=P1A7;// 延时void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}// 按周期 t 发音void Play(uchar t){uchar i;for(i=0;i<100;i++){BEEP=~BEEP;DelayMS(t);}BEEP=0;}void main(){P1=0xff;BEEP=0;while(1){if(K1==0) Play(1);if(K2==0) Play(2);if(K3==0) Play(3);if(K4==0) Play(4);}}精品文档播放音乐/* 名称:播放音乐说明:程序运行时播放生日快乐歌, 未使用定时器中断,所有频率完全用延时 实现*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit BEEP=P3A7;// 生日快乐歌的音符频率表,不同频率由不同的延时来决定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_LON为拍子长度for(j=0;j<SONG_LONG[i]*20;j++){BEEP=~BEEP;〃SONG_TON延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);} DelayMS(10);i++;}}void main(){BEEP=0;while(1){PlayMusic(); // 播放生日快乐DelayMS(500); // 播放完后暂停一段时间}精品文档。

(原创)51单片机C语言程序设计--速学教程实例(进阶篇)之蜂鸣器奏乐

(原创)51单片机C语言程序设计--速学教程实例(进阶篇)之蜂鸣器奏乐

/******************************************************************//*名称:蜂鸣器奏乐演示程序*//*作者:苏涛*//*时间:2011-01-18*//*单位:安徽建筑工业学院电子与信息工程学院*//*07级电子信息工程专业二班*//******************************************************************/#include<reg52.h>//头文件调用,写程序时都要加上#include"SoundPlay.h"/******************************************************************//*名称:延时函数*//*功能:C语言中常用的for循环延时方法*//*延时长短可以通过变量初值来改变*//******************************************************************/void Delay1ms(uint count){uint i,j;for(i=0;i<count;i++)for(j=0;j<120;j++);}/*****************************Music***************************************************/ //挥着翅膀的女孩unsigned char code Music_Girl[]={0x17,0x02,0x17,0x03,0x18,0x03,0x19,0x02,0x15,0x03,0x16,0x03,0x17,0x03,0x17,0x03,0x17,0x03,0x18,0x03,0x19,0x02,0x16,0x03,0x17,0x03,0x18,0x02,0x18,0x03,0x17,0x03,0x15,0x02,0x18,0x03,0x17,0x03,0x18,0x02,0x10,0x03,0x15,0x03,0x16,0x02,0x15,0x03,0x16,0x03,0x17,0x02,0x17,0x03,0x18,0x03,0x19,0x02,0x1A,0x03,0x1B,0x03,0x1F,0x03,0x1F,0x03,0x17,0x03,0x18,0x03,0x19,0x02,0x16,0x03,0x17,0x03,0x18,0x03,0x17,0x03,0x18,0x03,0x1F,0x03,0x1F,0x02,0x16,0x03,0x17,0x03,0x18,0x03,0x17,0x03,0x18,0x03,0x20,0x03,0x20,0x02,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,0x15,0x03,0x15,0x03,0x17,0x03,0x16,0x66,0x17,0x04,0x18,0x04,0x18,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x00,0x1A,0x03,0x1A,0x03,0x1A,0x03,0x1B,0x03,0x1B,0x03,0x1A,0x03,0x19,0x03,0x19,0x02,0x17,0x03,0x15,0x17,0x15,0x03,0x16,0x03,0x17,0x03,0x18,0x03,0x17,0x04,0x18,0x0E,0x18,0x03,0x17,0x04,0x18,0x0E,0x18,0x66,0x17,0x03,0x18,0x03,0x17,0x03,0x18,0x03,0x20,0x03,0x20,0x02,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x04,0x1B,0x0E,0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,0x15,0x03,0x15,0x03,0x17,0x03,0x16,0x66,0x17,0x04,0x18,0x04,0x18,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x00,0x18,0x02,0x18,0x03,0x1A,0x03,0x19,0x0D,0x15,0x03,0x15,0x02,0x18,0x66,0x16,0x02,0x17,0x02,0x15,0x00,0x00,0x00}; //同一首歌unsigned char code Music_Same[]={0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x66,0x18,0x03,0x17,0x02,0x15,0x02,0x16,0x01,0x15,0x02,0x10,0x02,0x15,0x00,0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x02,0x17,0x03,0x18,0x03,0x19,0x02,0x15,0x02,0x18,0x66,0x17,0x03,0x19,0x02,0x16,0x03,0x17,0x03,0x16,0x00,0x17,0x01,0x19,0x02,0x1B,0x02,0x1B,0x70,0x1A,0x03,0x1A,0x01,0x19,0x02,0x19,0x03,0x1A,0x03,0x1B,0x02,0x1A,0x0D,0x19,0x03,0x17,0x00,0x18,0x66,0x18,0x03,0x19,0x02,0x1A,0x02,0x19,0x0C,0x18,0x0D,0x17,0x03,0x16,0x01,0x11,0x02,0x11,0x03,0x10,0x03,0x0F,0x0C,0x10,0x02,0x15,0x00,0x1F,0x01,0x1A,0x01,0x18,0x66,0x19,0x03,0x1A,0x01,0x1B,0x02,0x1B,0x03,0x1B,0x03,0x1B,0x0C,0x1A,0x0D,0x19,0x03,0x17,0x00,0x1F,0x01,0x1A,0x01,0x18,0x66,0x19,0x03,0x1A,0x01,0x10,0x02,0x10,0x03,0x10,0x03,0x1A,0x0C,0x18,0x0D,0x17,0x03,0x16,0x00,0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x70,0x18,0x03,0x17,0x02,0x15,0x03,0x15,0x03,0x16,0x66,0x16,0x03,0x16,0x02,0x16,0x03,0x15,0x03,0x10,0x02,0x10,0x01,0x11,0x01,0x11,0x66,0x10,0x03,0x0F,0x0C,0x1A,0x02,0x19,0x02,0x16,0x03,0x16,0x03,0x18,0x66,0x18,0x03,0x18,0x02,0x17,0x03,0x16,0x03,0x19,0x00,0x00,0x00};//两只蝴蝶unsigned char code Music_Two[]={0x17,0x03,0x16,0x03,0x17,0x01,0x16,0x03,0x17,0x03,0x16,0x03,0x15,0x01,0x10,0x03,0x15,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,0x10,0x0E,0x15,0x04,0x0F,0x01,0x17,0x03,0x16,0x03,0x17,0x01,0x16,0x03,0x17,0x03,0x16,0x03,0x15,0x01,0x10,0x03,0x15,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,0x15,0x03,0x16,0x01,0x17,0x03,0x16,0x03,0x17,0x01,0x16,0x03,0x17,0x03,0x16,0x03,0x15,0x01,0x10,0x03,0x15,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,0x10,0x0E,0x15,0x04,0x0F,0x01,0x17,0x03,0x19,0x03,0x19,0x01,0x19,0x03,0x1A,0x03,0x19,0x03,0x17,0x01,0x16,0x03,0x16,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,0x10,0x0D,0x15,0x00,0x19,0x03,0x19,0x03,0x1A,0x03,0x1F,0x03,0x1B,0x03,0x1B,0x03,0x1A,0x03,0x17,0x0D,0x16,0x03,0x16,0x03,0x16,0x0D,0x17,0x01,0x17,0x03,0x17,0x03,0x19,0x03,0x1A,0x02,0x1A,0x02,0x10,0x03,0x17,0x0D,0x16,0x03,0x16,0x01,0x17,0x03,0x19,0x03,0x19,0x03,0x17,0x03,0x19,0x02,0x1F,0x02,0x1B,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x17,0x02,0x1A,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x1A,0x03,0x19,0x03,0x17,0x03,0x16,0x03,0x17,0x0D,0x16,0x03,0x17,0x03,0x19,0x01,0x19,0x03,0x19,0x03,0x1A,0x03,0x1F,0x03,0x1B,0x03,0x1B,0x03,0x1A,0x03,0x17,0x0D,0x16,0x03,0x16,0x03,0x16,0x03,0x17,0x01,0x17,0x03,0x17,0x03,0x19,0x03,0x1A,0x02,0x1A,0x02,0x10,0x03,0x17,0x0D,0x16,0x03,0x16,0x01,0x17,0x03,0x19,0x03,0x19,0x03,0x17,0x03,0x19,0x03,0x1F,0x02,0x1B,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x17,0x02,0x1A,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x17,0x16,0x1A,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x1A,0x03,0x19,0x03,0x17,0x03,0x16,0x03,0x0F,0x02,0x10,0x03,0x15,0x00,0x00,0x00};/*************************************************************************************/ void main(){InitialSound();while(1){Play(Music_Girl,0,3,360);Delay1ms(500);Play(Music_Same,0,3,360);Delay1ms(500);Play(Music_Two,0,3,360);Delay1ms(500);}}/******************************************************************//*头文件:SOUNDPLAY.H*//*说明:此头文件来源于网络*//******************************************************************//*说明**************************************************************曲谱存贮格式unsigned char code MusicName{音高,音长,音高,音长....,0,0};末尾:0,0表示结束(Important)音高由三位数字组成:个位是表示1~7这七个音符十位是表示音符所在的音区:1-低音,2-中音,3-高音;百位表示这个音符是否要升半音:0-不升,1-升半音。

单片机-利用蜂鸣器演奏音乐

单片机-利用蜂鸣器演奏音乐

实验三-利用蜂鸣器演奏音乐一、实验目的1.了解BlueSkyC51单片机实验板中蜂鸣器的硬件电路2.学会利用蜂鸣器实现音乐的演奏3.掌握蜂鸣器实现音乐演奏的编程二、实验硬件设计及电路1. BlueSkyC51单片机实验板2.单片机最小系统3.蜂鸣器电路连接三极管主要是做驱动用的。

因为单片机的IO口驱动能力不够让蜂鸣器发出声音,所以我们通过三极管放大驱动电流,从而可以让蜂鸣器发出声音,你要是输出高电平,三极管导通,集电极电流通过蜂鸣器让蜂鸣器发出声音,当输出低电平时,三极管截止,没有电流流过蜂鸣器,所以就不会发出声音。

三、实验原理1.音调及节拍用一个口,输出方波,这个方波输入进蜂鸣器就会产生声音,通过控制方波的频率、时间,就能产生简单的音乐。

一般说来,单片机演奏音乐基本都是单音频率,因此单片机奏乐只需控制音调和节拍。

(1)音调的确定音调是由频率来确定的。

通过单片机的定时器定时中断,将单片机上对应蜂鸣器的I/O 口来回取反,从而让蜂鸣器发出不同频率的声音。

只需将定时器给以不同的定时值就可实现。

通过延时,即可发出所需要的频率。

(2)节拍的确定一拍的时长大约为400—500ms,每个音符的时长通过节拍来计算。

详细见程序代码。

2.软件设计相关(1)头文件#include<reg51.h>#define uint unsigned int#define uchar unsigned char#define ulong unsigned longsbit beep=P1^4; //蜂鸣器与P1.4口连接(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]={ //音阶码表0xf7,0xd8, //440hz , 1 //00xf8,0x50, //466hz , 1# //10xf8,0xbc, //494hz , 2 //20xf9,0x26, //524hz , 2# //30xf9,0x85, //554hz , 3 //40xf9,0xe5, //588hz , 4 //50xfa,0x3d, //622hz , 4# //60xfa,0x92, //660hz , 5 //70xfa,0xdd, //698hz , 5# //80xfb,0x29, //740hz , 6 //90xfb,0x70, //784hz , 6# //100xfb,0xb0, //830hz , 7 //110xfb,0xef, //880hz , 1 //120xfc,0x29, //932hz , 1# //130xfc,0x62, //988hz , 2 //140xfc,0x95, //1046hz, 2# //150xfc,0xc7, //1108hz, 3 //160xfc,0xf5, //1174hz, 4 //170xfd,0x20, //1244hz, 4# //180xfd,0x4c, //1318hz, 5 //190xfd,0x72, //1396hz, 5# //200xfd,0x97, //1480hz, 6 //210xfd,0xbb, //1568hz, 6# //220xfd,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.Keil开发过程(1)新建“Project”,选择单片机为“AT89C52”(2)新建c文件“music.c”,并添加到工程中(3)设置生成“HEX”文件,编译(4)将生成的“HEX”文件烧录到单片机,检验(5)不断修改完善2.编译实验相关问题(1)实际发音颤音重解决方法为修改蜂鸣器的驱动频率.(2)实际节奏过快或者过慢调整延时四、C51程序代码(部分来源于网络)#include<reg51.h>#define uint unsigned int#define uchar unsigned char#define ulong unsigned longsbit beep=P1^4; //蜂鸣器与P1.4口连接uchar 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# //30xf9,0x85, //554hz , 3 //40xf9,0xe5, //588hz , 4 //50xfa,0x3d, //622hz , 4# //60xfa,0x92, //660hz , 5 //70xfa,0xdd, //698hz , 5# //80xfb,0x29, //740hz , 6 //90xfb,0x70, //784hz , 6# //100xfb,0xb0, //830hz , 7 //110xfb,0xef, //880hz , 1 //120xfc,0x29, //932hz , 1# //130xfc,0x62, //988hz , 2 //140xfc,0x95, //1046hz, 2# //150xfc,0xc7, //1108hz, 3 //160xfc,0xf5, //1174hz, 4 //170xfd,0x20, //1244hz, 4# //180xfd,0x4c, //1318hz, 5 //190xfd,0x72, //1396hz, 5# //200xfd,0x97, //1480hz, 6 //210xfd,0xbb, //1568hz, 6# //220xfd,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};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){uchar i,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单片机通过按键控制蜂鸣器发生详解

51单片机通过按键控制蜂鸣器发生详解

单片机开发报告院系:电子工程学院专业:自动化班级:自动化1401学号: 2姓名:赵越指导老师:刘星光2018年 01 月04 日.系统任务按键控制蜂鸣器发声二.电路原理图三.程序设计内容“叮咚”电子门铃实验程序:常有的家用电子门铃在有客人来访时候,假如按压门铃按钮时,室内会发出“叮咚”声音,本实验程序模拟电子门铃的发音,当我们按压实验板上的K1按钮时候,蜂鸣器发出“叮咚”音乐声,是一个比较适用的程序。

使用无源蜂鸣器输出7个基本音阶声音是由物体振动所产生的。

不过因为物体的资料以及振幅、频次不同,而产生不一样的声音。

声音的响度是由振幅决定的,而音调则是由频次决定的,那么我们只要要控制物体振动的频次,就能够发出固定的腔调。

五.汇编程序ORG0000HAJMPSTARTORG 000BH INCM OV 20H ;中止服务TH0,#0D8H,中止计数器加1MOVTL0,#0F0H 12M晶振,形成10毫秒中止RETIORG001BHLJMPINTT1;跳转到T1中止服务程序START: MOV DPTR,#00H;初始化程序MOV A,#00HOBUF1 EQU30HOBUF2 EQU31HOBUF3 EQU32HOBUF4 EQU33HFLAGB BIT00HSTOPB BIT01HMOV SP,#50HMOV TH0,#0D8HMOV TL0,#0F0HMOV TMOD,#21HMOV TH1,#09HMOV TL1,#09HMOV IE,#8AHAJMPLOOPLOOP:JNB P3.2,MUSIC0JNB P3.1,MAINAJMPLOOPMAIN:JBP3.1,MAIN;检测按钮LCALLJBYS10MP3.1,MAIN;延时去颤动SETB MOV MOV MOV MOV CLR CLRJNBTR1OBUF1,#00HOBUF2,#00HOBUF3,#00HOBUF4,#00HFLAGBSTOPBSTOPB,$;按钮有效AJMP START ;发出“叮咚”完成,返回从头检测按钮YS10M: ;10ms延时子程序MOVR6,#20 D1:MOVDJNZDJNZ R7,#100 R7,$R6,D1RETTING:AJMPSTARTINTT1:;准时器T1中止服务程序INCMOVCJNEMOVINCMOVCJNEMOVJBCPLAJMPOBUF3 ;中止服务程序中发出一声“叮咚”响声A,OBUF3A,#100,NEXTOBUF3,#00HOBUF4 A,OBUF4A,#20,NEXT OBUF4,#00H FLAGB,PGSTP FLAGB NEXTPGSTP:SETBCLRSTOPBTR1LJMPINT0RET NEXT:JBINCMOVCJNEMOVCPLLJMP FLAGB,SOU2 OBUF2A,OBUF2A,#04H,INT0RET OBUF2,#00HINT0RETSOU2: INCMOVCJNEMOVCPL OBUF1A,OBUF1A,#05H,INT0RET OBUF1,#00HINT0RET:RETIMUSIC0:JBLCALLJBp3.2,MUSIC0YS10Mp3.2,MUSIC0NOPMOV DPTR,#DAT表头地点送DPTRM OV20H,#0 0H;中止计数器清MOVMAIN2: JNBB,#00H ;表序号清P3.3,TINGCLRAMOVCA,@A+DPTR;查表代替码JZ END0 ;是00H,则结束CJNEA,#0FFH,MUSIC5LJMPMUSIC3MUSIC5:NOPMOV R6,AINC DPTRMOV A,BMOVCA,@A+DPTR;取节拍代码送R7MOV R7,ASETBTR0 MUSIC2: NOP CPL MOV A,R6MOV R3,A;启动计数LCALLDELMOVA,R7CJNEA,20H,MUSIC2;中止计数器(20H)=R7否?MOV 20H,#00HINC DPTRINCBLJMPMAIN2;不等,则持续循环;等于,则取下一代码MUSIC3:;休止100毫秒NOPCLRTR0MOV R2,#0DHMUSIC4:NOPMOV R3,#0FFHLCALLDELDJNZR2,MUSIC4INC DPTRLJMPMAIN2END0:NOPMOV R2,#0FFH;歌曲结束,延时MUSIC6:MOV R3,#00HLCALLDELDJNZR2,MUSIC6CLR TR0LJMPLOOPDEL:NOPDEL3:MOV R4,#03HDEL4:NOPDJNZR4,DEL4NOPDJNZR3,DEL3RETDENG1: MOV R3,#64HDJNZR3,$AJMPMAINDAT:DB 30h,30h,26h,26h,20h,20h,1ch,1ch,1ah,1ah,18h,18h,00hEND六、程序下载及调试步骤:1.点击translate按钮预编译2.点击build 按钮编译3.点击rebuild 按钮编译全部目标4.翻开普中烧录软件5.点击程序下载四.程序流程图开始初始化设置中止程序按键判断Yes中止变电平循环体中止51单片机通过按键控制蜂鸣器发生详解结束判断NoYes结束21 / 2121。

51蜂鸣器发不同的声音程序

51蜂鸣器发不同的声音程序

51蜂鸣器发不同的声音程序/****************************************************同时用两个定时器,控制蜂鸣器发声,T0控制频率,T1控制同一频率发声的时长(2s),不同依次频率为1,10,50,100,200,400,800,1k(hz),占空比均为0.5T0的计时周期为125us;T1的计时周期为50ms 。

不同频率对应的num依次为:4000,400,80,40,20,10,5,4*****************************************************/#includesbit beep=P2^3; //unsigned int num=4000,//决定蜂鸣器的响应周期,从而决定不同的频率counter0=0,//T0控制频率counter1=0; //T1控制周期void main(void){ //TMOD=0x12; //T0用自动重装初值的8位计数方式,T1用16位计数方式TH0=0x83; //T0计数周期125usTL0=0x83;TH1=(65536-50000)/256; //T1计数周期50msTL1=(65536-50000)%256;ET0=1; //允许T0中断ET1=1; //允许T1中断EA=1; //开总中断TR0=1; //T0开始计数TR1=1; //T1开始计数beep=1;while(1);}void time0() interrupt 1 {counter0++;if(counter0==num){counter0=0;beep=~beep;}}void time1() interrupt 3 {TH1=(65536-50000)/256; TL1=(65536-50000)%256; counter1++;if(counter1==40){counter1=0;switch(num){case 4000:num=400 ;break; case 400 :num=80 ;break; case 80 :num=40 ;break; case 40 :num=20 ;break; case 20 :num=10 ;break; case 10 :num=5 ;break; case 5 :num=4 ;break; case 4 :num=4000;break; }}}。

基于51单片机蜂鸣器发声的-C语言程序

基于51单片机蜂鸣器发声的-C语言程序
for(i=0;i<100;i++)
{
BEEP=~BEEP;
DelayMS(t);
}
BEEP=0;
}
void main()
{
P1=0xff;
BEEP=0;
while(1)
{
if(K1==0) Play(1);
if(K2==0) Play(2);
if(K3==0) Play(3);
if(K4==0) Play(4);
}
}
播放音乐
/*名称:播放音乐
说明:程序运行时播放生日快乐歌,未使用定时器中断,所有频率完全用延时实现
*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsign7;
//生日快乐歌的音符频率表,不同频率由不同的延时来决定
for(k=0;k<SONG_TONE[i]/3;k++);
}
DelayMS(10);
i++;
}
}
void main()
{
BEEP=0;
while(1)
{
PlayMusic(); //播放生日快乐
DelayMS(500); //播放完后暂停一段时间
}
}
按键发音
/*名称:按键发音
说明:按下不同的按键会是SOUNDER发出不同频率的声音。本例使用延时函数实现不同频率的声音输出,以后也可使用定时器
*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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};
//生日快乐歌节拍表,节拍决定每个音符的演奏长短
sbit K1=P1^4;
sbit K2=P1^5;
sbit K3=P1^6;
sbit K4=P1^7;
//延时
void DelayMS(uint x)
{
uchar t;
while(x--) for(t=0;t<120;t++);
}
//按周期t发音
void Play(uchar t)
{
uchar i;
}
//播放函数
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延时表决定了每个音符的频率
}
}
播放音乐
/*名称:播放音乐
说明:程序运行时播放生日快乐歌,未使用定时器中断,所有频率完全用延时实现
*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit BEEP=P3^7;
//生日快乐歌的音符频率表,不同频率由不同的延时来决定
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++);
for(k=0;k<SONG_TONE[i]/3;k++);
}
DelayMS(10);
i++;
}
}
void main()
{
BEEP=0;
while(1)
{
PlayMusic(); //播放生日快乐
DelayMS(500); //播放完后暂停一段时间
}
}
for(i=0;i<100;i++)
{
BEEP=~BEEP;
DelayMS(t);
}
BEEP=0;
}
void main()
{
P1=0xff;
BEEP=0;
while(1)
{
if(K1==0) Play(1);
if(K2==0) Play(2);
if(K3==0) Play(3);
if(K4==0) Play(4Байду номын сангаас;
按键发音
/*名称:按键发音
说明:按下不同的按键会是SOUNDER发出不同频率的声音。本例使用延时函数实现不同频率的声音输出,以后也可使用定时器
*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit BEEP=P3^7;
相关文档
最新文档