📄 music.c.bak
字号:
/*实验名称:放音乐
功能:从蜂鸣里放出音乐
作者:http://www.mcuprog.com
05-02-01
*/
#include <reg52.h>
#include <ctype.h>
#pragma ot(0)
#define uint unsigned int
#define uchar unsigned char
#define OSFREQ 11059200 //所使用的晶振频率
/**************音符频率表****************/
uint code notefreq[]={ 523, 587, 659, 698, 784, 880, 988,
1047,1175,1319,1396,1568,1760,1976,
2093,2349,2637,2793,3136,3520,3961};
/*************音名***************/
uchar code notename[]={'c','d','e','f','g','a','b',
'1','2','3','4','5','6','7',
'C','D','E','F','G','A','B',0};
/*************半音频率表*****************/
uint code halfnotefreq[]={ 554, 622, 740, 831, 933,
1109,1245,1480,1161,1865,
2218,2489,2960,3322,3729};
/*************音名***************/
uchar code halfnotename[]={'c','d','f','g','a',
'1','2','4','5','6',
'C','D','F','G','A',0};
sbit BEEP_PWR=P3^3;
uchar FreqSandH,FreqSandL; /*产生方波的定时器的初值*/
uchar timer1cnt; /*定时器延时计数 */
uchar timer1cntflg; /*定时器定时完成标志 */
/*********定时器0用来产生方波***************/
void timer0int () interrupt 1
{
TH0=FreqSandH;
TL0=FreqSandL;
BEEP_PWR=!BEEP_PWR;
}
/**********定时器用来进行比较准确的延时************/
void timer1int() interrupt 3
{
TH1=0xe0;
TL1=0x00;
timer1cnt++;
if(timer1cnt>=(OSFREQ/1500000l))
{timer1cntflg=1; TR1=0;}
}
void delay(uchar time)
{
uchar i;
uint j;
for(i=0;i<time;i++)
for(j=0;j<0x900;j++);
/***
uchar i;
for(i=0;i<time;i++)
{timer1cnt=0; timer1cntflg=0;
TR1=1;
while(!timer1cntflg);
}
***/
}
void Sound(uint freq)
{
uint timreg;
timreg=65536-(OSFREQ/(25*freq));
FreqSandH=timreg/256;
FreqSandL=timreg&0x00ff;
TR0=1; ET0=1;
}
void SoundOff(void)
{
TR0=0; ET0=0;
BEEP_PWR=0;
}
uint GetFreq(uchar ch,uchar flg)
{
uchar * pn,i=0;
uint * pf;
if(flg) {pn=halfnotename; pf=halfnotefreq;}
else {pn=notename; pf=notefreq;}
while(1)
{if(pn[i]==0) return 0;
if(ch==pn[i]) return pf[i];
i++;
}
}
void Play(char * str)
{
uchar i=0,ch,halfflg=0;
uchar lasttime;
uint freq;
while(1)
{for(;;i++)
{ch=str[i]; //允许曲谱用空格符 '|'符,换行回车等分隔以便阅读
if((ch==' ')||(ch=='|')||(ch=='\r')||(ch=='\n')) {i++;
continue;}
if(!ch) {SoundOff(); return;} //乐曲结束则播放完毕
if(ch=='#') {halfflg=1; continue;} //半音标志
if(isdigit(ch)||isalpha(ch))
{freq=GetFreq(ch,halfflg); //从音名获取频率
lasttime=16;
break;
}
else {halfflg=0; continue;}
}
i++;
ch=str[i]; //从下一个符号获取额外音长符号
while(1)
{if(!ch) break;
if(isdigit(ch)||isalpha(ch)) break; //非音长符号则下次处理
if(ch=='-') lasttime+=8; //额外延时一拍
if(ch=='.') lasttime+=4; //额外延时半拍
if(ch=='_') lasttime/=2; //下划线相当于简谱中音名下面的下划线,延时减半
if(ch=='=') lasttime/=4; //双下划线相当于简谱中音名下面的双下划线,延时减为1/4
i++;
ch=str[i];
}
if(freq!=0) Sound(freq); //发声
else SoundOff();
delay(lasttime); //延时
}
}
//编谱说明,低音(简谱中数字下面有一个点的)1234567对应的为小写cdefgab
//中音(简谱中数字上下都没有点的)1234567对应的也为1234567
//高音(简谱中数字上面有一个点的)1234567对应的为大写CDEFGAB
//对于降音符b或声音符#一律用#+合适的音名例如#5
//一个音符本身为一拍,加下划线后为半拍加等号为1/4拍 如:65_ 4= 则音6为一拍,音5为半拍,音4为1/4拍
//下划线或等号连续书写则音长连续变短
//音符后加-或.表示延长。‘-’延长一拍‘.’延长半拍多加则延长连续增加
void main(void)
{
//uint i;
TMOD=0x11; ET1=1; ET0=1; EA=1;
while(1)
{
// Play("1_1_5_5_6_6_5 4_4_3_3_2_2_1 5_5_4_4_3_3_2 5_5_4_4_3_3_21_1_5_5_6_6_5 4_4_3_3_2_2_1"); //满天都是小星星
Play("1_2_3_1_ 1_2_3_1_ 3_4_5 3_4_5 5=6=5=4=3_1_ 5=6=5=4=3_1_ 2_g_12_g_1"); //两只老虎
//Play("a-a1-a2--a-b1b13-2a--a-- a-33-12--a-b1b13-21--1-- 5-55432--a-b1-12123--3-- 1-1_1_1235--4-32-b3-2a--a-- a-66565--4-34-56543--3--1-1_1_1235--4-32-b3-2a--a--"); //山楂树
Play("5._3=2_1_5-12_3_g-5.3_23_5_1a_3_2-356.5_352._3=2_1_a32_21_a1g05.3_6562_3_50"); //学习雷锋好榜样
//Play("C-53.2_1530C-53.2_1650 5_C6_5_C05_C6_5_6_0_3_C.6_53C.6_C0C53_6_5_3_2.1_30_5_C56_C_6_5_33_1_6-60C._C=5_5_2._3=5_5_6.5_6DC6_5_C6_5_33_5_C-");
// Play("3- 2_3_4_3 3- 2_3_4_3 3- 4- 3_4_5_4 4- 3-2- 3- 2_3_4_3 3- 2_3_4_3 3-4- 3_4_5_4 4- 3-2");//许巍-星空前奏
Play("543 3_2_1_2_30 g53 3_2_1_2_30 a65 4_3_2_3_4 1 b a g");//许巍-时光
SoundOff();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -