基于c51单片机编写简单蜂鸣器音乐程序的方法
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); //播放}}。
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;
单片机蜂鸣器发声代码
单片机蜂鸣器发声代码蜂鸣器是一种常见的电子元件,可以发出短促的蜂鸣声用于提醒或警示。
在单片机应用中,蜂鸣器可以用来作为声音提示的一种方式。
下面介绍一段单片机蜂鸣器的发声代码。
1. 引脚连接在使用单片机控制蜂鸣器发声之前,需要将蜂鸣器与单片机的引脚连接。
通常情况下,蜂鸣器有两个引脚,其中一个为正极,一个为负极。
将蜂鸣器的正极连接到单片机的一个IO口上,将负极连接到单片机的GND上。
2. 代码实现单片机发出频率为f的声音,需要将IO口以1/f的频率高低电平切换。
以下是一个基本的单片机蜂鸣器发声代码:#include 'reg51.h' //单片机头文件#define BEEP P1_5 //定义蜂鸣器连接的IO口void Delay(unsigned int Time) //延时函数{unsigned int i,j;for(i=Time;i>0;i--)for(j=110;j>0;j--);}void Beep(unsigned int Time) //发出一次蜂鸣声{BEEP = 0; //使IO口输出低电平Delay(Time); //延时BEEP = 1; //使IO口输出高电平Delay(Time); //延时}void main(void){while(1){Beep(100); //发出100ms的蜂鸣声}}在上述代码中,我们首先定义了蜂鸣器连接的IO口为P1_5,然后定义了一个延时函数Delay和一个发出蜂鸣声函数Beep。
Delay函数用于延时,Beep函数用于发出一次蜂鸣声。
在main函数中,我们使用一个while循环不停地发出蜂鸣声。
在Beep函数中,我们首先使IO口输出低电平,然后延时一段时间,再使IO口输出高电平,再延时一段时间,从而在IO口上形成一个频率为1/(2*Time)的方波信号,从而发出蜂鸣声。
3. 修改参数如果我们想要修改蜂鸣器发出的声音的频率和时长,只需要在Beep函数中修改Time的值即可。
51单片机项目教程项目 5 蜂鸣器实验
图5- 9蜂鸣器实物结果
当SM0、SM1=01时,串行口设为方式1的双机串行通信。TXD脚和 RXD脚分别用于发送和接收数据。
5.2技术准备
方式1发送时,数据位由TXD端输出,发送一帧信息为10位:1位起始 位0,8位数据位(先低位)和1位停止位1。当CPU执行一条数据写 SBUF的指令,就启动发送。发送开始时,内部发送控制信号变为有 效,将起始位向TXD脚(P3.0)输出,此后每经过一个TX时钟周期, 便产生一个移位脉冲,并由TXD引脚输出一个数据位。8位数据位全部 发送完毕后,中断标志位TI置1。 方式1接收时(REN = 1),数据从RXD(P3.1)引脚输入。当检测到 起始位的负跳变,则开始接收。当一帧数据接收完毕后,同时满足以 下两个条件,接收才有效。 (1)RI = 0,即上一帧数据接收完成时,RI = 1发出的中断请求已被 响应,SBUF中的数据已被取走,说明“接收SBUF”已空。 (2)SM2 = 0或收到的停止位 = 1(方式1时,停止位已进入RB8), 则将接收到的数据装入SBUF和RB8(装入的是停止位),且中断标 志RI置“1”。
5.2技术准备
5.2.2 了解实验板蜂鸣器电路
图5- 3蜂鸣器电路
5.2技术准备
5.2.3 蜂鸣器驱动电路
蜂鸣器驱动电路如图5-4所示。
图5- 4蜂鸣器驱动电路
5.2技术准备
5.2.4串行口的结构
单片机串口结构如图5-5所示。有两个物理上独立的接收、发送缓冲器 SBUF(属于特殊功能寄存器),可同时发送、接收数据。控制寄存器共 有两个:特殊功能寄存器SCON和PCON。发送和接收引脚分别是TXD (P3.0)和RXD(P3.1)。
SM0 0 0 1 1 SM1 0 1 0 1 工作 方式 0 1 2 3 功能简介 移位寄存器 8位UART 9位UART 9位UART 比特率 OSC/12 可变 OSC/32或 OSC/64 可变
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单片机C语言程序设计--速学教程实例(进阶篇)之蜂鸣器奏乐_
本文由sutao130贡献 pdf文档可能在WAP端浏览体验不佳。
建议您优先选择TXT,或下载源文件到本机查看。
/******************************************************************/ /* /* /* /* /* #include <reg52.h> #include "SoundPlay.h" /******************************************************************/ /* /* /* 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, 名称:延时函数 功能:C 语言中常用的 for 循环延时方法 延时长短可以通过变量初值来改变 */ */ */ 名称:蜂鸣器奏乐演示程序 作者:苏涛 时间:2011-01-18 单位:安徽建筑工业学院 电子与信息工程学院 07 级电子信息工程专业 二班 */ */ */ */ */ /******************************************************************/ //头文件调用,写程序时都要加上 /******************************************************************/ 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-升半音。
基于c51单片机编写简单蜂鸣器音乐程序的方法
uint i;
for(i=0;i<jp;i++)
{
sound(s);
if(s==0xff)break;
}
}
//*******************************************************************
void main()//主函数
{
while(1)
{
c=0;//用来使数组从头开始读取
/*3*/case 0x0d:j=s/330;break;case 0x0e:j=s/659;break;case 0x0f:j=s/1318;break;
/*4*/case 0x10:j=s/349;break;case 0x11:j=s/370;break;case 0x12:j=s/698;break;case 0x13:j=s/740;break;case 0x14:j=s/1379;break;case 0x15:j=s/1480;break;
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} ;//*
uint code music[]=//这是一个“童话”谱子的数组,与m_t()子程序中的十六进制数一一对应,0xff代表每次音乐的停顿,0x00表示音乐的结束
{0x16,0x10,0x0d,0xff, 0x0d,0x10,0x0d, 0xff, 0x0d,0x10,0x0d,0x10,0x0d,0x07,0x01,0xff,
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>#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);//播放}}。
基于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语言程序设计--速学教程实例(进阶篇)之蜂鸣器奏乐
/******************************************************************//*名称:蜂鸣器奏乐演示程序*//*作者:苏涛*//*时间: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-升半音。
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单片机蜂鸣器发声的-C语言程序
{
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、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
据我所知,声音的音调不同是因为声音的频率不同造成的。那么,就可以通过单片机发送不同频率的脉冲信号给蜂鸣器,来达到让蜂鸣器发出不同音调声音的目的。不同频率的脉冲信号就意外着每个脉冲之间必须有着不一样的时差,这可以通过延时或者定时计数器定时的方式来实现。
#include<reg52.H>
#include <intrins.h> //内部包含延时函数_nop_();
typedef unsigned char uchar;
typedef unsigned int uint;
uint c;
sbit spk =P2^0 ; //定义p2.0口为电平信号输出端
{
uint k;
k=m_t(s);
if(k!=0);
{
spk=0;
delay_us(k/2);
spk=1;
delay_us(k/2);
}
if(k==0)
{
delay_ms(100) ;
}
}
void play_sound(uint s,uint jp)//该子程序用来进行一个拍子的发声,jp是用来调节一个拍子的长短参数
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} ;//*
/*5*/case 0x16:j=s/392;break;case 0x17:j=s/425;break;case 0x18:j=s/784;break;case 0x19:j=s/831;break;case 0x1a:j=s/1568;break;case 0x1b:j=s/1661;break;
{
uint i;
for(i=0;i<jp;i++)
{
sound(s);
if(s==0xff)break;
}
}
//*******************************************************************
void main()//主函数
{
while(1)
{
c=0;//用来使数组从头开始读取
{
for(i=0;i<128;i++);
}
}
uint m_t(uint i)//*这是一个用来把音调与延时的时长一一对应的子程序
{
uint j,s;
s=100000;
switch (i)//*下面让不同音高的音调与各十六进制数相对应,例如其中“1“有6种音高可选择(不懂音乐,我自己的说法)
{
/*1*/case 0x01:j=s/262;break;case 0x02:j=s/277;break;case 0x03:j=s/523;break;case 0x04:j=s/554;break;case 0x05:j=s/1046;break;case 0x06:j=s/1109;break;
最后在主程序中运行子程序,通过读rom里面的数组来达到连续播放各种不同音调的音的目的,这样听起来就像一首曲子了。
以下是我按以上方法编写的一个小程序:
/***********************************************************************************************
uint code music[]=//这是一个“童话”谱子的数组,与m_t()子程序中的十六进制数一一对应,0xff代表每次音乐的停顿,0x00表示音乐的结束
{0x16,0x10,0x0d,0xff, 0x0d,0x10,0x0d, 0xff, 0x0d,0x10,0x0d,0x10,0x0d,0x07,0x01,0xff,
/*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等十六进制数是对应的
/*2*/case 0x07:j=s/294;break;case 0x08:j=s/311;break;case 0x09:j=s/587;break;case 0x0a:j=s/622;break;case 0x0b:j=s/1175;break;case 0x0c:j=s/1245;break;
以下是本人编写的一段简单音乐程序,程序已经在板子上调试成功,水平有限,如有不正确的地方请多包涵
*******************************************************************************************************/
/*3*/case 0x0d:j=s/330;break;case 0x0e:j=s/659;break;case 0x0f:j=s/1318;break;
/*4*/case 0x10:j=s/349;break;case 0x11:j=s/370;break;case 0x12:j=s/698;break;case 0x13:j=s/740;break;case 0x14:j=s/1379;break;case 0x15:j=s/1480;break;
/*6*/case 0x1c:j=s/440;break;case 0x1d:j=s/466;break;case 0x1e:j=s/880;break;case 0x1f:j=s/932;break;case 0x20:j=s/1760;break; case 0x21:j=s/1865;break;
定时计数器定时的方法是相对精确的,然而我们人耳对于声音在相位上的变化的感应是不敏感的,而且普通蜂鸣器在商业上的用途不是专门用来发音乐,误差较大,因此通过延时程序延时的方法往往也能达到想要的目的。
因此我认为,使一个蜂鸣器发出一定频率的程序是可以这样写的:
首先是了解你要发出声音音调的频率。可以通过一张简单的表得知:
从表中你可以计算出你要的音的周期,例如低1DO,为262hz,那么它的周期为1/262s
也就是每次脉冲取反的中间延时半个周期即可。
然后,编写一个能发相应频率声音的子程序。这个子程序中往往须包括以下二个部分:一,控制脉冲形成的代码,电平取反过程中含有相应的延时;二,控制脉冲持续时间的代码,持续的时间时候常常和音乐的拍子有相关性。
//****************************************
void delay_us(uint i)//延时nus的程序
{
uchar j;
for(j=0;j<i;j++)
_nop_();
}
void delay_ms(uint j)//延时nms的子程序
{
uint i;
while(j--)
while(music[c])
{
play_sound(music[c],500);
c++;
}
}
友情提示:谱子可以上网查,或者通过360浏览器获得:
}