我手里有一段生日快乐歌的C程序,给你参考下。晶振5.3M,P1.1
输出。
#include<stc11.h>
#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=0t<120t++)
}
//播放函数
void PlayMusic()
{uint i=0,j,k
while(SONG_LONG[i]!=0||SONG_TONE[i]!=0)
{ //播放各个音符,SONG_LONG为拍子长度
for(j=0j<SONG_LONG[i]*20j++)
{
BEEP=~BEEP
//SONG_TONE延时表决定了每个音符的频率
for(k=0k<SONG_TONE[i]/3k++)
}
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=0i<12i++) // 根据调号及升降八度来生成新的频率表
{
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=LDiv1i>0i--) //发规定长度的音
{
while(TF1==0)
TH1 = Sound_Temp_TH1
TL1 = Sound_Temp_TL1
TF1=0
}
}
if(LDiv2!=0)
{
TR0=0 BeepIO=0
for(i=LDiv2i>0i--) //音符间的间隔
{
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
自动生成的软件不知道 这些程序也是前辈们写好的,我们直接用着玩,可能有别的高手知道怎么自动生成吧。
不过详细不步骤已经写的很清楚了,自己编写也能编的出来的。
这里修改:sbit key1=P1^0
sbit key2=P1^1
void Play_Song(unsigned char i)
{
unsigned char Temp1,Temp2
unsigned int Addr
Count = 0 //中断计数器清0
Addr = i * 217
while(1)
{
if(key2==0)break 毁森 //插入这一句
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
}
}
}
}
}
PlaySong2(................
{
....
whlie(1)
{
if(key1==0)break //插入这一句
.....
}
}
void main()
{
Time0_Init() //定时器0中断初始化
while(1)
{
闹丛 Play_Song(0) //播放1
纤弯亩Play_Song2(0) //播放2
}
}
#include <瞎含reg51.h> //头文件unsigned char count//定义全局变量,是一个计数器,0-255计数,仔神档用来计时一个频率的时长
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//工作方式,16位定时器
IE=0x82 //允许定时器T0中断和总中断
TH0=0xDC //定时器初值高8位,具体时间需要根据晶振计算
TL0=0x00 //定时器初值低8位
}
void time1_int() interrupt 1 //定时器中断函数
{
TH0=0xDC //定时器重赋初值高8位
TL0=0x00 //定时器重赋初值低8位
count++ //计数器自加
}
void delay_xms(unsigned int x) //延时函数
{
unsigned int i,j//定义延时函数局部变量
for(i=0i<xi++)//用for循环实现延时
for(j=0j<3j++) //数值很小,不应该是ms级的,此函数用来改变蜂鸣器频率
}
void play_song(unsigned char i) //歌曲播放函数
{
unsigned char temp1,temp2 //定义局部变量,temp1为频率缓存器,temp2为时长缓存器
unsigned int addr //定义局部变量,是一个数据地址缓存器
count=0 //计数器清零
addr=i*217 //两首歌,数组中0~216为歌曲0,
while(1) //此循环
{
temp1=song[addr++] //从0开始,取偶数地址中的值(频率)放到缓存器temp1,addr自加
if(temp1==0xff) //如果遇到0xff
{
TR0=0 //不允许定时器T0计时
delay_xms(100) //延时,此处0xff可能是停顿符号
}
else if(temp1==0x00) //否则如果遇到0x00
{
return //函数返回,此处的0x00应该是歌曲播完的符号
}
else//否则
{
temp2=song[addr++] //取奇数地址中的值放到缓存器temp2,addr自加
TR0=1 //允许定时器T0计时
while(1) //此循环用来调节蜂鸣器发声频率和时长
{
speak=~speak//蜂鸣器取反,此蜂鸣器为它激式,可以用改变取反频率方式实现发出不同音调
delay_xms(temp1) //延时相应频率所需时长
if(temp2==count)//如果相应频率发声时长与计数器数值相同
{
count=0//计数器清零
break//跳出该while循环
}
}
}
}
}
void main() //主函数
{
time0_init() //调用定时器初始化函数
while(1) //单片机进入正常工作死循环
{
play_song(0) //调用播放歌曲函数,并且播放歌曲0
}
}
/*****
/*我的注释中其实有个错误:取偶数地址中的值放到temp1,取奇数地址中的值放到temp2。因为有0xff停顿符号和0x00停止符号存在于歌曲代码中,以至于偶数地址中并非全是频率代码、奇数地址中并非全是时长代码。但我这样写是方便你理解,所以最后作此说明。*/
*****/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)