📄 music.lst
字号:
C51 COMPILER V6.23a MUSIC 02/19/2005 11:41:15 PAGE 1
C51 COMPILER V6.23a, COMPILATION OF MODULE MUSIC
OBJECT MODULE PLACED IN music.OBJ
COMPILER INVOKED BY: C:\TOOLS\Keil\C51\BIN\C51.EXE music.c BROWSE DEBUG OBJECTEXTEND
stmt level source
1 #include <reg52.h>
2 #include <ctype.h>
3 #pragma ot(0)
4 #define uint unsigned int
5 #define uchar unsigned char
6 #define OSFREQ 11059200 //所使用的晶振频率
7
8 /**************音符频率表****************/
9 uint code notefreq[]={ 523, 587, 659, 698, 784, 880, 988,
10
11 1047,1175,1319,1396,1568,1760,1976,
12
13 2093,2349,2637,2793,3136,3520,3961};
14 /*************音名***************/
15 uchar code notename[]={'c','d','e','f','g','a','b',
16
17 '1','2','3','4','5','6','7',
18
19 'C','D','E','F','G','A','B',0};
20 /*************半音频率表*****************/
21 uint code halfnotefreq[]={ 554, 622, 740, 831, 933,
22
23 1109,1245,1480,1161,1865,
24
25 2218,2489,2960,3322,3729};
26 /*************音名***************/
27 uchar code halfnotename[]={'c','d','f','g','a',
28
29 '1','2','4','5','6',
30
31 'C','D','F','G','A',0};
32
33 sbit BEEP_PWR=P3^3;
34 uchar FreqSandH,FreqSandL; /*产生方波的定时器的初值*/
35 uchar timer1cnt; /*定时器延时计数 */
36 uchar timer1cntflg; /*定时器定时完成标志 */
37
38 /*********定时器0用来产生方波***************/
39 void timer0int () interrupt 1
40 {
41 1 TH0=FreqSandH;
42 1 TL0=FreqSandL;
43 1 BEEP_PWR=!BEEP_PWR;
44 1 }
45
46 /**********定时器用来进行比较准确的延时************/
47 void timer1int() interrupt 3
48 {
49 1 TH1=0xe0;
50 1 TL1=0x00;
51 1 timer1cnt++;
52 1 if(timer1cnt>=(OSFREQ/1500000l))
53 1 {timer1cntflg=1; TR1=0;}
54 1 }
55
C51 COMPILER V6.23a MUSIC 02/19/2005 11:41:15 PAGE 2
56 void delay(uchar time)
57 {
58 1 uchar i;
59 1 uint j;
60 1 for(i=0;i<time;i++)
61 1 for(j=0;j<0x900;j++);
62 1 /***
63 1 uchar i;
64 1 for(i=0;i<time;i++)
65 1 {timer1cnt=0; timer1cntflg=0;
66 1 TR1=1;
67 1 while(!timer1cntflg);
68 1 }
69 1 ***/
70 1 }
71
72 void Sound(uint freq)
73 {
74 1 uint timreg;
75 1 timreg=65536l-(OSFREQ/(24l*freq));
76 1 FreqSandH=timreg/256;
77 1 FreqSandL=timreg&0x00ff;
78 1 TR0=1; ET0=1;
79 1 }
80
81
82 void SoundOff(void)
83 {
84 1 TR0=0; ET0=0;
85 1 BEEP_PWR=0;
86 1 }
87
88 uint GetFreq(uchar ch,uchar flg)
89 {
90 1 uchar * pn,i=0;
91 1 uint * pf;
92 1 if(flg) {pn=halfnotename; pf=halfnotefreq;}
93 1 else {pn=notename; pf=notefreq;}
94 1 while(1)
95 1 {if(pn[i]==0) return 0;
96 2 if(ch==pn[i]) return pf[i];
97 2 i++;
98 2 }
99 1 }
100
101 void Play(char * str)
102 {
103 1 uchar i=0,ch,halfflg=0;
104 1 uchar lasttime;
105 1 uint freq;
106 1 while(1)
107 1 {for(;;i++)
108 2 {ch=str[i]; //允许曲谱用空格符 '|'符,换行回车等分隔以便阅读
109 3 if((ch==' ')||(ch=='|')||(ch=='\r')||(ch=='\n')) {i++;
110 4 continue;}
111 3 if(!ch) {SoundOff(); return;} //乐曲结束则播放完毕
112 3 if(ch=='#') {halfflg=1; continue;} //半音标志
113 3 if(isdigit(ch)||isalpha(ch))
114 3 {freq=GetFreq(ch,halfflg); //从音名获取频率
115 4 lasttime=16;
116 4 break;
117 4 }
C51 COMPILER V6.23a MUSIC 02/19/2005 11:41:15 PAGE 3
118 3 else {halfflg=0; continue;}
119 3 }
120 2 i++;
121 2 ch=str[i]; //从下一个符号获取额外音长符号
122 2 while(1)
123 2 {if(!ch) break;
124 3 if(isdigit(ch)||isalpha(ch)) break; //非音长符号则下次处理
125 3 if(ch=='-') lasttime+=8; //额外延时一拍
126 3 if(ch=='.') lasttime+=4; //额外延时半拍
127 3 if(ch=='_') lasttime/=2; //下划线相当于简谱中音名下面的下划线,延时减半
128 3 if(ch=='=') lasttime/=4; //双下划线相当于简谱中音名下面的双下划线,延时减为1/4
129 3 i++;
130 3 ch=str[i];
131 3 }
132 2 if(freq!=0) Sound(freq); //发声
133 2 else SoundOff();
134 2 delay(lasttime); //延时
135 2 }
136 1 }
137
138 //编谱说明,低音(简谱中数字下面有一个点的)1234567对应的为小写cdefgab
139 //中音(简谱中数字上下都没有点的)1234567对应的也为1234567
140 //高音(简谱中数字上面有一个点的)1234567对应的为大写CDEFGAB
141 //对于降音符b或声音符#一律用#+合适的音名例如#5
142 //一个音符本身为一拍,加下划线后为半拍加等号为1/4拍 如:65_ 4= 则音6为一拍,音5为半拍,音4为1/4拍
143 //下划线或等号连续书写则音长连续变短
144 //音符后加-或.表示延长。‘-’延长一拍‘.’延长半拍多加则延长连续增加
145 void main(void)
146 {
147 1 //uint i;
148 1 TMOD=0x11; ET1=1; ET0=1; EA=1;
149 1 while(1)
150 1 {
151 2 // 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"); //满天
-都是小星星
152 2 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"); //两只老虎
153 2 // 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--"); //山楂树
154 2 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"); //学习雷锋好榜样
155 2 //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-");
156 2 // 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");//许巍
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -