蜂鸣器天空之城代码(蜂鸣器播放天空之城)

http://www.itjxue.com  2023-01-25 00:03  来源:未知  点击次数: 

怎样得到蜂鸣器的歌曲代码?

蜂鸣器播放乐曲主要取决于两个参数, 音调和音长. 音调为do, re, mi等, 音长是同样的音调持续的时间.

蜂鸣器的歌曲播放的驱动方式就变成碰到do就输出多少频率(音调即频率), 然后保证这个音调持续多少时间. 每个人写的驱动都有可能不一样(例如有些人do, re, mi只用1,2,3表示, 然后再查频率表, 有些人则直接用相应输出频率的设置值)

所以针对你的歌曲驱动方法(你必须先要了解), 就可以将歌曲转换为代码.

举个例子: 8-8-5-5-3-3-1-1-5-4-3-2-1-- (1: do, 8代表高do, -代表一个节拍), 假设我的驱动方法是1的对应的频率设置0x55, 2对应0x65, 以此类推 8为0xC5, 另假设一个节拍"-"对应的时间长度设置为0x30, 则上面的简谱可以转换为 {{0xC5, 0x30}, {0xC5, 0x30}, {0x95, 0x30}, {0x95, 0x30}, {0x75, 0x30}, {0x75, 0x30}, {0x55, 0x30}, {0x55, 0x30}, {0x95, 0x30}, {0x85, 0x30},{0x75, 0x30}, {0x65, 0x30}, {0x55, 0x60}}

知道规律后就可以在电脑上面写一个程序专门将简谱转换成代码.

求STC89C52单片机的蜂鸣器演奏 (歌曲随便)C程序

蜂鸣器唱《祝你平安》 *

; *

;********************************************************************************/

SPK bit P2.3

ORG 0000H

LJMP START

ORG 000BH

INC 20H ;中断服务,中断计数器加1

MOV TH0,#0D8H

MOV TL0,#0EFH ;12M晶振,形成10毫秒中断

RETI

START:

MOV SP,#50H

MOV TH0,#0D8H

MOV TL0,#0EFH

MOV TMOD,#01H

MOV IE,#82H

MUSIC0:

NOP

MOV DPTR,#DAT ;表头地址送DPTR

MOV 20H,#00H ;中断计数器清0

MOV B,#00H ;表序号清0

MUSIC1:

NOP

CLR A

MOVC A,@A+DPTR ;查表取代码

JZ END0 ;是00H,则结束

CJNE A,#0FFH,MUSIC5

LJMP MUSIC3

MUSIC5:

NOP

MOV R6,A

INC DPTR

MOV A,B

MOVC A,@A+DPTR ;取节拍代码送R7

MOV R7,A

SETB TR0 ;启动计数

MUSIC2:

NOP

CPL SPK

MOV A,R6

MOV R3,A

LCALL DEL

MOV A,R7

CJNE A,20H,MUSIC2 ;中断计数器(20H)=R7否?

;不等,则继续循环

MOV 20H,#00H ;等于,则取下一代码

INC DPTR

; INC B

LJMP MUSIC1

MUSIC3:

NOP

CLR TR0 ;休止100毫秒

MOV R2,#0DH

MUSIC4:

NOP

MOV R3,#0FFH

LCALL DEL

DJNZ R2,MUSIC4

INC DPTR

LJMP MUSIC1

END0:

NOP

MOV R2,#64H ;歌曲结束,延时1秒后继续

MUSIC6:

MOV R3,#00H

LCALL DEL

DJNZ R2,MUSIC6

LJMP MUSIC0

DEL:

NOP

DEL3:

MOV R4,#02H

DEL4:

NOP

DJNZ R4,DEL4

NOP

DJNZ R3,DEL3

RET

NOP

DAT:

db 26h,20h,20h,20h,20h,20h,26h,10h,20h,10h,20h,80h,26h,20h,30h,20h

db 30h,20h,39h,10h,30h,10h,30h,80h,26h,20h,20h,20h,20h,20h,1ch,20h

db 20h,80h,2bh,20h,26h,20h,20h,20h,2bh,10h,26h,10h,2bh,80h,26h,20h

db 30h,20h,30h,20h,39h,10h,26h,10h,26h,60h,40h,10h,39h,10h,26h,20h

db 30h,20h,30h,20h,39h,10h,26h,10h,26h,80h,26h,20h,2bh,10h,2bh,10h

db 2bh,20h,30h,10h,39h,10h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,20h

db 20h,10h,20h,10h,2bh,10h,26h,30h,30h,80h,18h,20h,18h,20h,26h,20h

db 20h,20h,20h,40h,26h,20h,2bh,20h,30h,20h,30h,20h,1ch,20h,20h,20h

db 20h,80h,1ch,20h,1ch,20h,1ch,20h,30h,20h,30h,60h,39h,10h,30h,10h

db 20h,20h,2bh,10h,26h,10h,2bh,10h,26h,10h,26h,10h,2bh,10h,2bh,80h

db 18h,20h,18h,20h,26h,20h,20h,20h,20h,60h,26h,10h,2bh,20h,30h,20h

db 30h,20h,1ch,20h,20h,20h,20h,80h,26h,20h,30h,10h,30h,10h,30h,20h

db 39h,20h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,10h,40h,10h,20h,10h

db 20h,10h,2bh,10h,26h,30h,30h,80h,00H

END

能提供几首单片机的歌曲代码?用蜂鸣器唱的

#include reg52.h

#include intrins.h

//本例采用89C52, 晶振为11.0592MHZ

//关于如何编制音乐代码, 其实十分简单,各位可以看以下代码.

//频率常数即音乐术语中的音调,而节拍常数即音乐术语中的多少拍;

//所以拿出谱子, 试探编吧!

sbit out=P2^3;

unsigned char n=0; //n为节拍常数变量

unsigned char code music_tab[] ={

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

};

void int0() interrupt 1 //采用中断0 控制节拍

{ TH0=0xd8;

TL0=0xef;

n--;

}

void delay (unsigned char m) //控制频率延时

{

unsigned i=3*m;

while(--i);

}

void delayms(unsigned char a) //豪秒延时子程序

{

while(--a); //采用while(--a) 不要采用while(a--); 各位可编译一下看看汇编结果就知道了!

}

void main()

{ unsigned char p,m; //m为频率常数变量

unsigned char i=0;

TMOD=0x0f;

TMOD|=0x01;

TH0=0xd8;TL0=0xef;

IE=0x82;

play:

while(1)

{

a: p=music_tab[i];

if(p==0x00) { i=0, delayms(1000); goto play;} //如果碰到结束符,延时1秒,回到开始再来一遍

else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a;} //若碰到休止符,延时100ms,继续取下一音符

else {m=music_tab[i++], n=music_tab[i++];} //取频率常数 和 节拍常数

TR0=1; //开定时器1

while(n!=0) out=~out,delay(m); //等待节拍完成, 通过P1口输出音频(可多声道哦!)

TR0=0; //关定时器1

}

}

给一段用BEEP()调用蜂鸣器播放简单乐谱的源代码。。我喜欢通过读代码学程序。。。分数不多,好人一生平安

#includereg52.h

#define uchar unsigned char

#define uint unsigned int

uchar Count;

sbit Speak =P1^2; //蜂鸣器器控制脚  

/*以下数组是音符编码*/

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,

//路边的野华不要采

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,

};

/********************************************************************

* 名称 : Time0_Init()

* 功能 : 定时器的初始化,定时时间可用光碟中软件计算,11.0592MZ晶振,10ms

* 输入 : 无

* 输出 : 无

***********************************************************************/

void Time0_Init()

{

TMOD = 0x01;

IE = 0x82;

TH0 = 0xDC;

TL0 = 0x00;

}

/********************************************************************

* 名称 : Time0_Int()

* 功能 : 定时器中断,中断中实现 Count 加一

* 输入 : 无

* 输出 : 无

***********************************************************************/

void Time0_Int() interrupt 1

{

TH0 = 0xDC;

TL0 = 0x00;

Count++; //长度加1

}

/********************************************************************

* 名称 : Delay_xMs()

* 功能 : 延时子程序,经过软件调试,测得延时程序大概为55us.

* 输入 : x

* 输出 : 无

***********************************************************************/

void Delay_xMs(uint x)

{

uint i,j;

for(i=0; ix; i++)

{

for(j=0; j3; j++);

}

}

/********************************************************************

* 名称 : Play_Song()

* 功能 : 播放蜂鸣器控制程序

* 输入 : i (选择播放哪首歌曲,0为“祝你平安”,1为“路边的野花你不要采”

* 输出 : 无

***********************************************************************/

void Play_Song(uchar i)

{

uchar Temp1,Temp2;

uint Addr;

Count = 0; //中断计数器清0

Addr = 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;

}

}

}

}

}

/********************************************************************

* 名称 : Main()

* 功能 : 主函数

* 输入 : 无

* 输出 : 无

***********************************************************************/

void Main()

{

Time0_Init(); //定时器0中断初始化

while(1)

{

Play_Song(0); //Play_Song(0)为祝你平安 Play_Song(1)为路边的野花你不要裁

}

}

单片机设计倒计时蜂鸣器报警的编程代码

int sec=60;

int cnt=0;

char Buzzer=1;//关闭蜂鸣器

void Timer0Init(void) //5毫秒@11.0592MHz

{

AUXR = 0x7F; //定时器时钟12T模式

TMOD = 0xF0; //设置定时器模式

TL0 = 0x00; //设置定时初值

TH0 = 0xEE; //设置定时初值

TF0 = 0; //清除TF0标志

ET0 = 1;//开T0中断

EA = 1;//开总中断

TR0 = 1; //定时器0开始计时

}

void Timer0Interrupt(void) interrupt 1

{

cnt++;

if(cnt=200)

{

cnt=0;sec--;

if(sec==0)EA=0;

}

}

其它就自己写了。

if(sec==0)Buzzer=0;

(责任编辑:IT教学网)

更多

推荐excel文章