#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=0xd8TL0=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+1delayms(100),TR0=0goto 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
}
}
#include <逗橘行reg51.h>sbit speaker=P1^2
unsigned char timer0h,timer0l,time
//生日歌
code unsigned char sszymmh[]={5,1,1, 5,1,1, 6,1,2, 5,1,2, 1,2,2, 7,1,4,
5,1,1, 5,1,1, 6,1,2, 5,1,2, 2,2,2, 1,2,4,
5,1,1, 5,1,1, 5,2,2, 3,2,2, 1,2,2, 7,1,2, 6,1,2,
4,2,1, 4,2,1, 3,2,2, 1,2,2, 2,2,2, 1,2,4}
// 音阶频率表 高八位
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=0t1<tt1++)
{
for(t2=0t2<8000t2++)
{
}
}
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
while(i<75){ /山哗/音乐数组长度 ,唱完从头再来
k=sszymmh[i]+7*sszymmh[i+1]-1
timer0h=FREQH[k]
timer0l=FREQL[k]
time=sszymmh[i+2]
i=i+3
song()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)