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

高分求一段用c语言编写的用于单片机音乐播放的程序代码
已知:蜂鸣器控制脚 3.7
晶振频率 12MHZ
用内部计时
含5段歌曲
播放可以清晰的听出是什么音乐
速度,最好一天内给出
快又好的有加分!
添加已知:单片机是STC 89C52
后面四条是要求
只要c编写的

#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); //播放
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-10-13
#include <reg51.h>
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<t;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();
}
}
}
第2个回答  2020-02-21
代码的格式,是由编程者设计的,并没有统一的规范。
下面的链接可供参考。
http://hi.baidu.com/%d7%f6%b6%f8%c2%db%b5%c0/blog/item/88bfff323ec42ef21b4cff09.html
第3个回答  2009-10-14
一般说来,单片机配合外接的蜂鸣器可以发出基本的单音频率。由于蜂鸣器发出的声音不包含相应幅度的谐波频率,因此采用这种方法不能演奏出多种音色的声音。使用单片机配合蜂鸣器来发音,只需弄清楚两个概念即可,也就是“音调”和“节拍”。其中,音调表示一个音符唱多高的频率;节拍表示一个音符唱多长的时间。下面分别介绍音调和节拍的相关知识,以及如何使用单片机来实现音调和节拍。
节拍表示一个音符唱多长的时间,其同样是音乐学中的名词。在一张完整乐谱的开头,都有如1=C 、1=G …… 等的标识。例如1=G 的节拍示意图。
这里的、用来表示节拍,而1=C、1=G表示一个乐谱的曲调,简单地说就是跟音调有关系。
前面介绍了基本音符的音调和节拍,并详细讲解了如何在单片机系统中来实现一个基本的音符。在单片机上实现音乐播放,一般只需逐个播放音符即可。用单片机播发音乐的方法如下:
(1)初始化单片机定时器;
(2)将乐谱中的每个音符的音调及节拍变换成相应的音调参数和节拍参数;
(3)将乐谱中音符的参数做成数据表格,存放在存储器中;
(4)通过程序取出一个音符的相关参数,驱动蜂鸣器播放该音符;
(5)该音符唱完后,接着取出下一个音符的相关参数……,如此直到播放完毕最后一个音符,根据需要也可循环不停地播放整个乐曲。

#include <reg51.h>

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
}
第4个回答  2009-10-13
我的博客上有三首连放的,你只要加两首就行了。

求单片机 音乐播放的c代码
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,0...

求一个用C语言编写的51单片机音乐程序代码。
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...

单片机音乐代码
如果是从中央C开始的,那么频率是(单位HZ)C:261.6256,D:293.6648,E:329.6276,F:49.2282,G:391.9954,A:440,B:493.8833 这是音乐的频率,你只要知道频率,输出不同频率的方波就等输出不同的音阶。 那么还有就是每个音阶的长度。方波延续的时间就是音阶的长度。很容易实现的。

简单的单片机音乐程序
Play(Music_Girl,0,3,360); Delay1ms(500); Play(Music_Same,0,3,360); Delay1ms(500); Play(Music_Two,0,3,360); Delay1ms(500); }}\/*** SOUND PLAY FOR 51MCU COPYRIGHT (c) 2005 BY JJJ. -- ALL RIGHTS RESERVED -- File Name: SoundPlay.h Author: Jiang Jian Jun Created: 2005\/5\/...

单片机播放音乐里的音乐代码是怎么得到的?有什么软件能将歌曲直接转换成...
把简谱中每个唱名对应的频率找出来,比如C调的1对应264Hz左右。然后把这些频率通过公式转换成初值再换算成十六进制。比如你用的是定时的工作方式1,则可以这位算:(假设晶振是12MHz)X=2^16-10^6\/(264*2)=63642因为要计算的是半周期对应的初值,所以频率就是2倍。十六进制就是:F89A 好像没有...

求51单片机蜂鸣器播放音乐的教程 C语言
uchar code jie7[8]={0,12,14,16,17,19,21,23}; \/\/C调的7个值 diao=*song;for(i=0,i1=0;;){ gaodi=0; \/\/高低=0 banyin=0;\/\/半音=0 yinchang=4;\/\/音长1拍 if((*(song+i)=='|') || (*(song+i)==' ')) i++;\/\/拍子间隔和一个空格过滤 switch(*(song+i)){...

怎样用stc89c52单片机控制蜂鸣器发出音乐的c语言程序?
include\\r\\n#include\\r\\n\/\/本例采用89C52,晶振为11.0592MHZ\\r\\n\/\/关于如何编制音乐代码,其实十分简单,各位可以看以下代码.\\r\\n\/\/频率常数即音乐术语中的音调,而节拍常数即音乐术语中的多少拍;\\r\\n\/\/所以拿出谱子,试探编吧!\\r\\nsbitBeep=P1^5;\\r\\n\\r\\nunsignedcharn=0;\/\/n为节拍常数...

求89C51单片机播放音乐的C语言代码!
flag置0,准备frq递增 delayms(15);} } void timer0() interrupt 1 using 1 { TH0 = 0xfe;TL0 = frq;SPK = ~SPK;} void delayms(unsigned char ms)\/\/ 延时子程序 { unsigned char i;while(ms--){ for(i = 0; i < 120; i++);} } 这是蜂鸣器报警的代码 ...

请高手帮忙解读一下51单片机音乐代码
\/*这个是播放声音代码了*\/ void PlayMusic(){ uint i =0,j,k;while(SONG_LONG[i]!=0||SONG_TONE[i]!=0){ for(j=0;j<SONG_LONG[i]*20;j++){ BEEP = ~BEEP;for(k=0;k<SONG_TONE[i]\/3;k++);} DelayMS(10);i++;} } void main(){ while(1){ PlayMusic();DelayMS(...

基于单片机AT89S52.编写一段程序,要求播放一段音乐,歌曲名《铃儿响叮 ...
uchar bai_c,shi_c,ge_c;uchar a,b,c;uchar i1,i2,i3;uchar code MUSIC[]={ 0,0,0,0,16,16,1,1, 15,15,16,16,1,1,1,1, 5,5,6,6,4,4,3,3, 2,2,2,2,2,2,5,5, 5,5,2,2,4,4,3,3, 2,2,1,2,4,4,4,4,16,16,1,1,2,2,4,4, 2...

相似回答