我手里有一段生日快乐歌的C程序,给你参考下。晶振5.3M,P1.1
输出。
#include
#define uchar unsigned char
#define uint unsigned int
sbit BEEP=P1^1;
//生日快乐歌的音符频率表,不同频率由不同的延时来决定
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 { BEEP=~BEEP; //SONG_TONE延时表决定了每个音符的频率 for(k=0;k } DelayMS(300); i++; } } void main() { BEEP=0; while(1) { PlayMusic(); //播放生日快乐 DelayMS(300); //播放完后暂停一段时间 } } /************************************************************************** SOUND PLAY FOR 51MCU COPYRIGHT (c) 2005 BY JJJ. -- ALL RIGHTS RESERVED -- File Name: SoundPlay.h Author: Jiang Jian Jun Created: 2005/5/16 Modified: NO Revision: 1.0 *******************************************************************************/ /*说明************************************************************************** 曲谱存贮格式 unsigned char code MusicName{音高,音长,音高,音长...., 0,0}; 末尾:0,0 表示结束(Important) 音高由三位数字组成: 个位是表示 1~7 这七个音符 十位是表示音符所在的音区:1-低音,2-中音,3-高音; 百位表示这个音符是否要升半音: 0-不升,1-升半音。 音长最多由三位数字组成: 个位表示音符的时值,其对应关系是: |数值(n): |0 |1 |2 |3 | 4 | 5 | 6 |几分音符: |1 |2 |4 |8 |16 |32 |64 音符=2^n 十位表示音符的演奏效果(0-2): 0-普通,1-连音,2-顿音 百位是符点位: 0-无符点,1-有符点 调用演奏子程序的格式 Play(乐曲名,调号,升降八度,演奏速度); |乐曲名 : 要播放的乐曲指针,结尾以(0,0)结束; |调号(0-11) : 是指乐曲升多少个半音演奏; |升降八度(1-3) : 1:降八度, 2:不升不降, 3:升八度; |演奏速度(1-12000): 值越大速度越快; ***************************************************************************/ #ifndef __SOUNDPLAY_H_REVISION_FIRST__ #define __SOUNDPLAY_H_REVISION_FIRST__ //************************************************************************** #define SYSTEM_OSC 12000000 //定义晶振频率12000000HZ #define SOUND_SPACE 4/5 //定义普通音符演奏的长度分率,//每4分音符间隔 sbit BeepIO = P3^7; //定义输出管脚 unsigned int code FreTab[12] = { 262,277,294,311,330,349,369,392,415,440,466,494 }; //原始频率表 unsigned char code SignTab[7] = { 0,2,4,5,7,9,11 }; //1~7在频率表中的位置 unsigned char code LengthTab[7]= { 1,2,4,8,16,32,64 }; unsigned char Sound_Temp_TH0,Sound_Temp_TL0; //音符定时器初值暂存 unsigned char Sound_Temp_TH1,Sound_Temp_TL1; //音长定时器初值暂存 //************************************************************************** void InitialSound(void) { BeepIO = 0; Sound_Temp_TH1 = (65535-(1/1200)*SYSTEM_OSC)/256; // 计算TL1应装入的初值 (10ms的初装值) Sound_Temp_TL1 = (65535-(1/1200)*SYSTEM_OSC)%256; // 计算TH1应装入的初值 TH1 = Sound_Temp_TH1; TL1 = Sound_Temp_TL1; TMOD |= 0x11; ET0 = 1; ET1 = 0; TR0 = 0; TR1 = 0; EA = 1; } void BeepTimer0(void) interrupt 1 //音符发生中断 { BeepIO = !BeepIO; TH0 = Sound_Temp_TH0; TL0 = Sound_Temp_TL0; } //************************************************************************** void Play(unsigned char *Sound,unsigned char Signature,unsigned Octachord,unsigned int Speed) { unsigned int NewFreTab[12]; //新的频率表 unsigned char i,j; unsigned int Point,LDiv,LDiv0,LDiv1,LDiv2,LDiv4,CurrentFre,Temp_T,SoundLength; unsigned char Tone,Length,SL,SH,SM,SLen,XG,FD; for(i=0;i<12;i++) // 根据调号及升降八度来生成新的频率表 { j = i + Signature; if(j > 11) { j = j-12; NewFreTab[i] = FreTab[j]*2; } else NewFreTab[i] = FreTab[j]; if(Octachord == 1) NewFreTab[i]>>=2; else if(Octachord == 3) NewFreTab[i]<<=2; } SoundLength = 0; while(Sound[SoundLength] != 0x00) //计算歌曲长度 { SoundLength+=2; } Point = 0; Tone = Sound[Point]; Length = Sound[Point+1]; // 读出第一个音符和它时时值 LDiv0 = 12000/Speed; // 算出1分音符的长度(几个10ms) LDiv4 = LDiv0/4; // 算出4分音符的长度 LDiv4 = LDiv4-LDiv4*SOUND_SPACE; // 普通音最长间隔标准 TR0 = 0; TR1 = 1; while(Point < SoundLength) { SL=Tone%10; //计算出音符 SM=Tone/10%10; //计算出高低音 SH=Tone/100; //计算出是否升半 CurrentFre = NewFreTab[SignTab[SL-1]+SH]; //查出对应音符的频率 if(SL!=0) { if (SM==1) CurrentFre >>= 2; //低音 if (SM==3) CurrentFre <<= 2; //高音 Temp_T = 65536-(50000/CurrentFre)*10/(12000000/SYSTEM_OSC);//计算计数器初值 Sound_Temp_TH0 = Temp_T/256; Sound_Temp_TL0 = Temp_T%256; TH0 = Sound_Temp_TH0; TL0 = Sound_Temp_TL0 + 12; //加12是对中断延时的补偿 } SLen=LengthTab[Length%10]; //算出是几分音符 XG=Length/10%10; //算出音符类型(0普通1连音2顿音) FD=Length/100; LDiv=LDiv0/SLen; //算出连音音符演奏的长度(多少个10ms) if (FD==1) LDiv=LDiv+LDiv/2; if(XG!=1) if(XG==0) //算出普通音符的演奏长度 if (SLen<=4) LDiv1=LDiv-LDiv4; else LDiv1=LDiv*SOUND_SPACE; else LDiv1=LDiv/2; //算出顿音的演奏长度 else LDiv1=LDiv; if(SL==0) LDiv1=0; LDiv2=LDiv-LDiv1; //算出不发音的长度 if (SL!=0) { TR0=1; for(i=LDiv1;i>0;i--) //发规定长度的音 { while(TF1==0); TH1 = Sound_Temp_TH1; TL1 = Sound_Temp_TL1; TF1=0; } } if(LDiv2!=0) { TR0=0; BeepIO=0; for(i=LDiv2;i>0;i--) //音符间的间隔 { while(TF1==0); TH1 = Sound_Temp_TH1; TL1 = Sound_Temp_TL1; TF1=0; } } Point+=2; Tone=Sound[Point]; Length=Sound[Point+1]; } BeepIO = 0; } //************************************************************************** #endif 自动生成的软件不知道 这些程序也是前辈们写好的,我们直接用着玩,可能有别的高手知道怎么自动生成吧。 不过详细不步骤已经写的很清楚了,自己编写也能编的出来的。
/***************************************************
程序名称:音乐演奏器
简要说明:P1.0口输出各音调的频率方波
编 写: MMC
更新时间:09\05
***************************************************/
#include
#define SPK P0_0 //定义方波输出口
#define LED P1_1
#define shumaguan P0 //定义数码管段码输出
unsigned int tone1,tone2;
/*****标准音符表*****/
//用于使定时器初值变化以产生相应频率的定时
unsigned char code yinfu[]={
0xfb,0xe9, //Do
0xfc,0x5c, //Re
0xfc,0xc1, //Mi
0xfc,0xef, //Fa
0xfd,0x45, //So
0xfd,0x92, //La
0xfd,0xd0, //Si
0xfd,0xee, //Do#
0xfa,0x14, //So低
0xfa,0xb9, //La低
0xfb,0x4d, //Si低
0x00,0x00 //音符之间的间隔,只要间隔时间小于65ms时,
//喇叭不会发出声音,用作拍子之间的短暂停顿
};
/*****军港之夜音调表*****/
unsigned char code shengri_tone[]={9,3,3,1,2,3,2,3,3,10,9,1,2,1,3,5,5,3,6,5,3,
3,3,2,1,2,3,2,3,11,9,10,11,10,1,11,3,3,11,10,11,10,11,3,3,11,
11,10,11,10,2,10,1,11,10,9,10,9,3,5,5,3,6,5,6,5,3,5,3,1,3,3,3,5,
3,5,5,3,3,2,3,2,11,10,11,10,9,3,3,5,5,3,6,5,6,5,3,5,3,1,3,
3,5,3,5,5,3,3,3,2,3,2,11,10,11,10,9,1 //0代表不发声,即停顿;数字即为音调
};
/*****军港之夜节拍表*****/
unsigned char code shengri_beat[]={24,24,24,24,12,12,48,24,48,24,24,12,12,86,24,24,24,24,24,48,24,
48,12,12,24,12,12,86,48,24,24,12,12,48,24,24,24,24,24,12,12,72,24,24,24,24,
24,12,12,48,24,12,12,24,24,12,12,86,24,24,24,24,24,12,12,48,12,24,12,24,12,12,12,48,
24,24,24,24,24,12,12,48,24,12,12,24,24,86,24,24,24,24,24,12,12,48,12,24,12,24,24,
24,72,24,24,24,12,12,24,12,12,48,24,12,12,24,24,86 //节拍,即tone表各音调的延时
};
/*****自动演示音调表*****/
unsigned char code yanshi_tone[]={ 1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,
8,0,7,0,6,0,5,0,4,0,3,0,2,0,1,0
};
/*****自动演示节拍表*****/
unsigned char code yanshi_beat[]={ 48,1,48,1,48,1,48,1,48,1,48,1,48,1,48,2,
48,1,48,1,48,1,48,1,48,1,48,1,48,1,48,2
};
/*****15ms延时子程序,用于节拍*****/
void delay(void)
{
unsigned char n=15;
while(n--)
{
unsigned char i;
for(i=0;i<125;i++);
}
}
/*****定时器0初始化*****/
void initTimer(void)
{
TMOD=0x01; //定时器0,工作方式1;定时器1,工作方式1
TH0=tone1;
TL0=tone2;
}
/*****定时器0中断服务程序*****/
void timer0(void) interrupt 1
{
TH0=tone1;
TL0=tone2;
SPK=~SPK; //取反,以产生方波
}
/*****演奏子程序1*****/
void play1(void)
{
unsigned char m=0;
unsigned char s;
unsigned char a=1;
while(1)
{
EA=0;
LED=0;
a=shengri_tone[m]; //取音符
s=shengri_beat[m]; //取节拍
tone1=yinfu[2*a-2];
tone2=yinfu[2*a-1];
EA=1;
while(s--)
{
delay();
}
LED=1;
m++;
if(m>=119) return; //数值是shengri相关表中的元素数量
}
}
/*****演奏子程序2*****/
void play2(void)
{
unsigned char m=0;
unsigned char s;
unsigned char a=1;
while(1)
{
EA=0;
LED=0;
a=yanshi_tone[m];
s=yanshi_beat[m];
tone1=yinfu[2*a-2];
tone2=yinfu[2*a-1];
EA=1;
while(s--)
{
delay();
}
LED=1;
m++;
if(m>=32) return;
}
}
/*****按键检测*****/
void check_key(void)
{
P2=0xff;
P3=0xff; //设置为输入状态
switch(P2) //检测按键,输出数码管、载入定时器初值、允许中断
{
case 0xfe:shumaguan=0xF9;tone1=0xfb;tone2=0x90;EA=1;break;
case 0xfd:shumaguan=0xA4;tone1=0xfc;tone2=0xc;EA=1;break;
case 0xfb:shumaguan=0xB0;tone1=0xfc;tone2=0x7b;EA=1;break;
case 0xf7:shumaguan=0x99;tone1=0xfc;tone2=0xad;EA=1;break;
case 0xef:shumaguan=0x92;tone1=0xfd;tone2=0xa;EA=1;break;
case 0xdf:shumaguan=0x82;tone1=0xfd;tone2=0x5d;EA=1;break;
case 0xbf:shumaguan=0xF8;tone1=0xfd;tone2=0xa7;EA=1;break;
case 0x7f:play1();break;
default: EA=0;SPK=0;shumaguan=0xff;//如果没有键按下则关闭中断和数码管
}
switch(P3)
{
case 0xfe:shumaguan=0x79;tone1=0xfd;tone2=0xc8;EA=1;break;
case 0xfd:shumaguan=0x24;tone1=0xfe;tone2=0x6;EA=1;break;
case 0xfb:shumaguan=0x30;tone1=0xfe;tone2=0x3e;EA=1;break;
case 0xf7:shumaguan=0x19;tone1=0xfe;tone2=0x57;EA=1;break;
case 0xef:shumaguan=0x12;tone1=0xfe;tone2=0x85;EA=1;break;
case 0xdf:shumaguan=0x02;tone1=0xfe;tone2=0xaf;EA=1;break;
case 0xbf:shumaguan=0x78;tone1=0xfe;tone2=0xd4;EA=1;break;
case 0x7f:play2();break;
default: EA=0;SPK=0;shumaguan=0xff;//如果没有键按下则关闭中断和数码管
}
}
/*****主程序*****/
void main(void)
{
initTimer();
// shumaguan=0xff;
TR0=1;
ET0=1;
SPK=0;
while(1)
{
check_key();
}
}
/*****END*****/
这个不仅有你说的功能,还集有了一首《军港之夜》的歌在里面
注:我也是在网上找的,自己修改了一下
有个视频用蜂鸣器播放任意音乐,网页链接