没人答,自己答一下吧!
我这有自己做的一个唱两只老虎的,只有音调,用的无源蜂鸣器
// 蜂鸣器唱两只老虎
#include<reg51.h>
#include<intrins.h>
sbit p=P2^3
int t
void js() interrupt 1
{
TH0=(65636-500)/256
TL0=(65636-500)%256
t++
}
void fm(int a)
{
int c
p=1
c=10-a
TMOD=1
TH0=(65636-500)/256
TL0=(65636-500)%256
EA=1
ET0=1
TR0=1
while(1)
{
if(t==12*50)break
if(a==0)continue
if(t%c==0)p=0
if(t%c>0)p=1
}
t=0
}
void main()
{
int c=0,i=0,k,
code unsigned char b[]={1,2,3,1,1,2,3,1,3,4,5,0,3,4,5,0,5,6,5,4,3,3,1,1,5,6,5,4,3,1,0,3,-1,1,0,3,-1,1,0}
while(1)
{
if(c==40)c=0
k=(c<17||c>29)
switch(k){
case(1): fm (b[c]),fm (b[c])break
case(0): fm (b[c])
}
p=1
c++
}
}
/*成都理工大学工程技术学院通信工程--漆洋
本程序用于播放音乐,采用定时器1控制时间
从而改变频率实现音节播放
*/
#include<AT89X52.H>
#define uchar unsigned char
#define uint unsigned int
#define buz P3_1
code uint ton[][7]= //定音所有音阶
{
{0xE600,0xE8D5,0xEB5E,0xEC86,0xEEA7,0xF08B,0xF23C},
{0xF2E6,0xF46D,0xF5BF,0xF61C,0xF750,0xF866,0xF905},
{0xf91E,0xf9DE,0xFA89,0xfaD8,0xfb68,0xfbe8,0xfc5a}, //中音C
{0xFC8F,0xFCEF,0xFD51,0xFD80,0xFDB5,0xFDF4,0xFE2D},
{0xFE47,0xFE77,0xFEA2,0xFEB6,0xFEDA,0xFEF3,0xFF17},
{0xFF24,0xFF3C,0xFF51,0xFF5B,0xFF6D,0xFF7D,0xFF8B},
}
/*
格式:音调+音阶+拍数
*/
code uint m1[]=
{ //《最炫民族风》
262,261,251,262,261,311,312,321,311,261,0,
312,311,252,311,322,331,351,351,331,322,331,0,
361,361,361,351,331,331,312,261,261,261,332,321,0,
331,332,351,331,321,331,321,311,262,251,261,0,
262,261,251,262,261,311,312,321,311,261,0,
312,311,252,311,322,331,351,351,331,322,331,0,
361,361,361,351,331,331,312,261,261,261,332,321,0,
331,331,351,331,331,351,351,361,362,352,362,0,
262,261,252,261,312,321,331,321,311,321,332,0,
332,361,361,352,331,321,312,321,333,0,
312,261,261,321,262,331,351,331,321,312,0,
262,311,321,332,321,311,261,251,263,0,
8
}
code uint m2[]=
{ //《爱情买卖》
262,261,231,221,232,0,
222,231,221,211,222,0,
252,251,251,231,251,251,251,231,262,231,231,221,232,0,
262,261,231,221,231,0,
221,231,221,211,221,0,
251,251,251,231,251,251,251,231,271,251,251,231,251,0,
231,261,311,311,232,261,311,0,
271,261,271,261,221,231,0,
222,221,221,161,221,231,251,231,271,271,251,232,0,
232,261,311,311,231,261,311,0,
331,321,331,321,311,321,0,
331,331,321,311,321,321,311,271,251,231,251,261,263,0,
8
}
code uint m3[]=
{
//《北京爱情故事》滴答滴答
252,262,311,322,331,312,331,323,0,
322,322,321,0,321,311,311,0,262,263,0,
252,261,312,322,332,351,332,323,0,
322,321,322,321,311,312,261,331,333,0,
362,361,362,361,351,351,331,323,0,
321,321,322,321,312,311,352,351,332,0,
362,361,362,411,332,321,332,321,0,
321,321,321,321,311,261,261,311,313,0,
8
}
code uint m4[]=
{ //《同桌的你》
252,251,251,251,231,241,251,272,0,
262,261,261,261,241,262,252,0,
252,251,251,251,271,261,251,241,0,
241,241,241,241,241,231,221,212,0,
252,251,251,251,231,241,251,272,0,
262,261,261,261,241,262,252,0,
252,251,251,251,271,261,251,241,0,
241,241,241,241,241,231,221,212,0,
311,311,311,312,251,261,311,311,332,0,
322,321,321,321,311,271,262,0,
272,271,271,271,311,321,252,0,
271,271,311,321,311,271,313,0,
311,311,311,312,251,261,311,311,332,0,
322,321,321,321,311,271,262,0,
272,271,271,271,311,321,252,0,
271,271,311,321,311,271,313,0,
8
}
code uint m5[]=
{ //《荷塘月色》
211,212,161,152,161,211,211,221,233,0,
221,222,211,221,221,252,251,231,231,221,233,0,
211,212,161,152,252,231,221,231,221,212,0,
222,221,211,221,221,232,221,211,161,221,212,0,
211,212,161,152,161,211,211,221,233,0,
221,222,211,221,221,252,251,231,231,221,233,0,
211,212,161,152,252,231,221,231,221,212,0,
222,221,211,221,221,232,221,211,161,221,212,0,
231,252,251,252,251,261,251,231,221,212,0,
261,311,261,251,231,221,211,162,221,221,232,221,0,
231,252,251,252,251,261,251,231,221,212,0,
162,211,161,151,222,231,212,0,
8
}
code uint m6[]= //《猪八戒背媳妇》
{
262,331,351,332,261,312,0,
261,311,261,311,332,331,321,332,311,262,0,
332,351,361,361,361,331,352,0,
332,351,332,351,361,361,362,331,352,0,
352,261,352,261,331,331,312,0,
321,0,321,0,321,311,321,332,351,361,432,331,432,0,
331,431,331,431,331,331,313,0,
321,0,321,0,322,311,321,331,351,364,0,
8
}
void musicdelay(int q)
{
while(--q)
}
/*音乐播放程序*/
void musicplay(uint *a)
{
uint b,i,j,k,l,m,p
uchar KEY=0
TMOD=0x11
p=0
buz=1
while(1) //如果其它键,停止播放音乐
{
if(a[p]==8)break //判断休止符,结束播放
if(a[p]!=8) //音频识别与转换
{
i=a[p]/100
j=a[p]%100/10
l=a[p]%10
if(a[p]!=0){b=ton[i][j-1]} //转换后存入变量b中
if(a[p]==0){b=0}
p++
}
k=(230414/(0xffff-b))*l //拍数
/******************播放当前音节*******************/
if(b!=0) //判断为正常音符,播放
{
TH1=0TL1=0TR1=1
for(m=0m<km++)
{
TH1=((b&0xff00)>>8) //装定时器初值
TL1=(b&0x00ff)
while(TF1==0)
buz=~buz //实现一次蜂鸣器震动
TF1=0
}
TR1=0
buz=1
}
/************************************************/
if(b==0) //判断为0(停顿),延时一节拍
{
buz=1
for(m=0m<80m++)
{
musicdelay(300)}
}
}
}
void musicplayc(uchar i)
{
if(i==1){musicplay(m1)}
if(i==2){musicplay(m2)}
if(i==3){musicplay(m3)}
if(i==4){musicplay(m4)}
if(i==5){musicplay(m5)}
if(i==6){musicplay(m6)}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)