求单片机 音乐播放的c代码

2024-11-07 11:25:14
推荐回答(5个)
回答1:

#include "reg52.h"
unsigned char Count;
sbit _Speak =P2^3 ; //讯响器控制脚

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,

};

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++ );
}
}

/*-------------------------------------------------
功能:歌曲播放子程序i为播放哪一段曲目
-------------------------------------------------*/
void Play_Song(unsigned char i)
{
unsigned char Temp1,Temp2;
unsigned int 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;
}
}
}
}
}
/*-------------------------------------------------
功能:主程序
-------------------------------------------------*/
void main()
{
Time0_Init(); //定时器0中断初始化
while(1)
{
Play_Song(0); //播放
}
}

回答2:

#include
sbit speaker=P3^7;
unsigned char timer0h,timer0l,time,led=1,j=0;
unsigned char flagd=0;
//世上只有妈妈好数据表
code unsigned char sszymmh[]={ 6,2,3, 5,2,1, 3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1,
6,2,4, 3,2,2, 5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1,
6,1,1, 5,2,1, 3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2,
5,2,1, 6,2,1, 3,2,2, 2,2,2, 1,2,4, 5,2,3, 3,2,1,
2,2,1, 1,2,1, 6,1,1, 1,2,1, 5,1,6, 0,0,0
} ;
// 音阶频率表 高八位
code unsigned char FREQH[]={
0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,
0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i
0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,
0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,
} ;
// 音阶频率表 低八位
code unsigned char FREQL[]={
0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,
0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, //1,2,3,4,5,6,7,8,i
0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,
0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,
};
void delay(unsigned char t)
{
unsigned char t1;
unsigned long t2;
for(t1=0;t1 {

for(t2=0;t2<8000;t2++)
{
;
}
}
TR0=0;
}
void t0int() interrupt 1
{
TR0=0;
speaker=!speaker;
TH0=timer0h;
TL0=timer0l;
TR0=1;
}
void song()
{
TH0=timer0h;
TL0=timer0l;
TR0=1;
delay(time);
}
void main(void)
{
unsigned char k,i;
TMOD=1; //置CT0定时工作方式1
EA=1;ET0=1;//IE=0x82 //CPU开中断,CT0开中断
while(1)
{
i=0;
time=1;
while(time)
{
if(j==8)
{
//led=1;
j=0;flagd=~flagd;
if(flagd)
{
led=0x80;
}
else
{
led=1;
}
}
else
{
P1=~led;
if(flagd)
{
led=led>>1;
}
else
{
led=led<<1;
}
j++;
}
k=sszymmh[i]+7*sszymmh[i+1]-1;
timer0h=FREQH[k];
timer0l=FREQL[k];
time=sszymmh[i+2];
i=i+3;
song();
}
}
}

回答3:

代码的格式,是由编程者设计的,并没有统一的规范。
下面的链接可供参考。
http://hi.baidu.com/%d7%f6%b6%f8%c2%db%b5%c0/blog/item/88bfff323ec42ef21b4cff09.html

回答4:

一般说来,单片机配合外接的蜂鸣器可以发出基本的单音频率。由于蜂鸣器发出的声音不包含相应幅度的谐波频率,因此采用这种方法不能演奏出多种音色的声音。使用单片机配合蜂鸣器来发音,只需弄清楚两个概念即可,也就是“音调”和“节拍”。其中,音调表示一个音符唱多高的频率;节拍表示一个音符唱多长的时间。下面分别介绍音调和节拍的相关知识,以及如何使用单片机来实现音调和节拍。
节拍表示一个音符唱多长的时间,其同样是音乐学中的名词。在一张完整乐谱的开头,都有如1=C 、1=G …… 等的标识。例如1=G 的节拍示意图。
这里的、用来表示节拍,而1=C、1=G表示一个乐谱的曲调,简单地说就是跟音调有关系。
前面介绍了基本音符的音调和节拍,并详细讲解了如何在单片机系统中来实现一个基本的音符。在单片机上实现音乐播放,一般只需逐个播放音符即可。用单片机播发音乐的方法如下:
(1)初始化单片机定时器;
(2)将乐谱中的每个音符的音调及节拍变换成相应的音调参数和节拍参数;
(3)将乐谱中音符的参数做成数据表格,存放在存储器中;
(4)通过程序取出一个音符的相关参数,驱动蜂鸣器播放该音符;
(5)该音符唱完后,接着取出下一个音符的相关参数……,如此直到播放完毕最后一个音符,根据需要也可循环不停地播放整个乐曲。

#include

sbit Y=P1^3; //播放按键

sbit Speaker=P3^7; //扬声器
static unsigned char bdata StateREG;
sbit m=StateREG^0;
unsigned char code * data Mymusic;
unsigned char data l;

void delay(void); //延时函数声明

unsigned char code music[30]=
{0xFF,0xFF,0xFB,0x90,0xFC,0x0C,0xFC,0x44,0xFC,0xAC,0xFD,0x09,0xFD,0x34,0xFD,0x82,0xFD,0xC8,0xFE,0x06,0xFE,0x22,0xFA,0X15,0XFB,0x04,0xFA,0x67,0xFE,0x85};

unsigned char code Mmusic[]=
{
0x35,0x31,0x34,0x34,0x24,0x35,0x32,0x32,0x24,0x38,
0x44,0x58,0x48,0x34,0x31,0x34,0x34,0x24,0x38,0x34,
0x7F,0x32,0x34,0x32,0x34,0x24,0x38,0x34,0x24,0x38,
0x43,0x58,0x48,0x34,0x32,0x34,0x38,0x24,0x38,0x34,
0x7F,0x16,0x4C,0x74,0x78,0x64,0x54,0x48,0x54,0x64,
0x58,0x44,0x34,0x24,0x38,0x24,0x14,0x12,0x21,0x14,
0x78,0x68,0x3F,0x4C,0x74,0x78,0x64,0x52,0x42,0x48,
0x53,0x64,0x58,0x44,0x34,0x24,0x38,0x24,0x24,0x38,
0x44,0x58,0x48,0x3C,0xFF
};

//主程序
void main()
{
unsigned int data j;
unsigned char data i;
unsigned char data k;
unsigned char data p11;
unsigned char data p33;
TMOD=0x01; //初始化
IE=0x82;
while(1)
{
j=0;
m=0;
while(Y==1)
{
for(i=0;i<5;i++)
{
delay(); //延时
}
p11=P1;
p33=P3;
while(*(Mmusic+j)!=0xFF)
{
k=*(Mmusic+j)&0x0F;
l=*(Mmusic+j)>>4;
if((p11!=P1)||((p33&0x0f)!=(P3&0x0f)))
{
goto Next;
}
TH0=music[2*l];
TL0=music[2*l+1];
TR0=1;
if ((music[2*l]==0xff)&&(music[2*l+1]==0xff))
{
TR0=0;
}
for(i=k;i>0;--i)
{
delay();
}
TR0=0;
j++;
}
Next: ;
}
}
}

/*单个音符的发声*/
void timer0() interrupt 1 using 1
{
TH0=music[2*l];
TL0=music[2*l+1];
Speaker = !Speaker;
}

/*汇编语句进行精确延时,延时1/4拍时间即187ms*/
void delay(void)
{
#pragma ASM
MOV R3,#02H
D1: MOV R2,#7DH
D2: MOV R1,#0F8H
DJNZ R1,$
DJNZ R2,D2
DJNZ R3,D1
#pragma ENDASM
}

回答5:

我的博客上有三首连放的,你只要加两首就行了。