高分!跪求C51单片机蜂鸣器播放《国际歌》的完整程序,请不要把其他曲子复制了

高分!跪求C51单片机蜂鸣器播放《国际歌》的完整程序,请不要把其他曲子复制了,第1张

#include <reg52.h>sbit speaker = P2^0unsigned char timer0h, timer0l, time//--------------------------------------//单片机晶振采用11.0592MHz// 频率-半周期数据表 高八位 本软件共保存了四个八度的28个频率数据code unsigned char FREQH[] = {0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8,//低音12345670xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC,//1,2,3,4,5,6,7,i0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE,//高音 2345670xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF} //超高音 1234567// 频率-半周期数据表 低八位code unsigned char FREQL[] = {0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6,//低音12345670x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D, //高音 2345670x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16} //超高音 1234567//--------------------------------------//国际歌数据表 要想演奏不同的乐曲, 只需要修改这个数据表code unsigned char sszymmh[] = {5,2,4,1,3,6,7,2,2,2,3,2,1,3,2,5,2,2,3,2,2,6,2,6,6,2,2,4,2,4,0,2,2,6,2,2,2,3,6,1,3,2,7,2,2,6,2,2,5,2,2,4,2,2,3,2,12,5,2,4,1,3,6,7,2,2,2,3,2,1,3,2,5,2,2,3,2,2,6,2,8,4,2,2,6,2,2,2,3,2,1,3,2,7,2,4,2,3,4,4,3,4,7,2,4,1,3,8,1,3,2,0,2,2,3,3,2,2,3,2,7,2,8,6,2,2,7,2,2,1,3,2,6,2,2,7,2,8,5,2,2,5,2,2,4,2,2,5,2,2,6,2,6,6,2,2,2,3,6,1,3,2,7,2,8,7,2,2,0,2,2,2,3,4,2,3,1,7,2,4,5,2,4,5,2,4,4,2,3,5,2,1,3,3,2,1,3,2,6,2,3,7,2,1,1,3,2,7,2,2,2,3,2,1,3,1,6,2,1,5,2,4,5,2,6,0,1,2,3,3,6,2,3,2,1,3,6,5,2,2,3,2,4,6,2,4,4,2,3,0,2,1,2,3,2,1,3,2,7,2,4,6,2,4,5,2,3,5,2,1,5,2,2,0,2,1,5,2,1,3,3,2,2,3,2,5,2,2,1,3,2,7,1,4,7,2,4,6,2,3,5,2,1,6,2,2,2,3,1,2,3,1,2,3,2,0,2,2,3,3,2}//--------------------------------------void t0int() interrupt 1 //T0中断程序,控制发音音调{TR0 = 0//先关闭T0speaker = !speaker //输出方波, 发音TH0 = timer0h //下次的中断时间, 这个时间, 控制音调高低TL0 = timer0l TR0 = 1//启动T0}//--------------------------------------void delay(unsigned int t) //延时程序,控制发音的时间长度{unsigned char t1 unsigned long t2 for(t1 = 0t1 <tt1++)//双重循环, 共延时t个半拍 for(t2 = 0t2 <3000t2++)//延时期间, 可进入T0中断去发音TR0 = 0 //关闭T0, 停止发音}//--------------------------------------void song() //演奏一个音符{TH0 = timer0h //控制音调TL0 = timer0l TR0 = 1//启动T0, 由T0输出方波去发音delay(time)//控制时间长度}//--------------------------------------void main(void){unsigned int k, i TMOD = 1 //置T0定时工作方式1ET0 = 1//开T0中断EA = 1 //开CPU中断while(1) { i = 0 time = 1 while(time) {k = sszymmh[i] + 7 * sszymmh[i + 1] - 1 //第i个是音符, 第i+1个是第几个八度timer0h = FREQH[k] //从数据表中读出频率数值timer0l = FREQL[k] //实际上, 是定时的时间长度time = sszymmh[i + 2] //读出时间长度数值i += 3 song() //发出一个音符} } }

没人答,自己答一下吧!

我这有自己做的一个唱两只老虎的,只有音调,用的无源蜂鸣器

// 蜂鸣器唱两只老虎

#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)}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8076164.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存