?? wyinyue.lst
字號(hào):
C51 COMPILER V7.05 WYINYUE 02/11/2006 11:23:06 PAGE 1
C51 COMPILER V7.05, COMPILATION OF MODULE WYINYUE
OBJECT MODULE PLACED IN WYINYUE.OBJ
COMPILER INVOKED BY: C:\SiLabs\MCU\IDEfiles\C51\BIN\C51.exe WYINYUE.C DB OE
stmt level source
1 //參考程序 B.WYINYUE.C 此程序?qū)⒛扯我魳酚锰囟ǖ念l率播放出來(lái)
2 #include <C8051F020.h>
3 #include <ctype.h>
4 #pragma ot(0)
5 #define uint unsigned int
6 #define uchar unsigned char
7 #define OSFREQ 6000000l /*所使用的晶振頻率*/
8 /**************音符頻率表****************/
9 uint code notefreq[]={ 523, 587, 659, 698, 784, 880, 988,
10 1047,1175,1319,1396,1568,1760,1976,
11 2093,2349,2637,2793,3136,3520,3961};
12 /*************音名***************/
13 uchar code notename[]={ 'c','d','e','f','g','a','b',
14 '1','2','3','4','5','6','7',
15 'C','D','E','F','G','A','B',0};
16 /*************半音頻率表*****************/
17 uint code halfnotefreq[]={ 554, 622, 740, 831, 933,
18 1109,1245,1480,1161,1865,
19 2218,2489,2960,3322,3729};
20 /*************音名***************/
21 uchar code halfnotename[]={ 'c','d','f','g','a',
22 '1','2','4','5','6',
23 'C','D','F','G','A',0};
24 //sbit BEEP_PWR=P1^0;
25 uchar FreqSandH,FreqSandL; /*產(chǎn)生方波的定時(shí)器的初值*/
26 uchar timer1cnt; /*定時(shí)器延時(shí)計(jì)數(shù) */
27 uchar timer1cntflg; /*定時(shí)器定時(shí)完成標(biāo)志 */
28 void timer0int () interrupt 1//定時(shí)器0中斷用來(lái)產(chǎn)生方波
29
30 {
31 1 TH0=FreqSandH;
32 1 TL0=FreqSandL;
33 1 P5 = P5 ^ 0x10; // change state of P5.4按位異或
34 1 }
35 void delay(uchar time)// 延時(shí)
36 {
37 1 uchar i;
38 1 uint j;
39 1 for(i=0;i<time;i++)
40 1 for(j=0;j<0x900;j++);
41 1 }
42 void Sound(uint freq)// 發(fā)聲
43 {
44 1 uint timreg;
45 1 timreg=65536l-(OSFREQ/(24l*freq));
46 1 FreqSandH=timreg/256;
47 1 FreqSandL=timreg&0x00ff;
48 1 TR0=1;
49 1 ET0=1;
50 1 }
51 void SoundOff(void)// 停止發(fā)聲
52 {
53 1 TR0=0;
54 1 ET0=0;
55 1 P5 = P5 & 0xef;
C51 COMPILER V7.05 WYINYUE 02/11/2006 11:23:06 PAGE 2
56 1 }
57 uint GetFreq(uchar ch,uchar flg)// 依據(jù)音名取對(duì)應(yīng)的頻率
58 {
59 1 uchar * pn,i=0;
60 1 uint * pf;
61 1 if(flg) {pn=halfnotename; pf=halfnotefreq;}
62 1 else {pn=notename; pf=notefreq;}
63 1 while(1)
64 1 {
65 2 if(pn[i]==0) return 0;
66 2 if(ch==pn[i]) return pf[i];
67 2 i++;
68 2 }
69 1 }
70 void Play(char * str)
71 {
72 1 uchar i=0,ch,halfflg=0;
73 1 uchar lasttime;
74 1 uint freq;
75 1 /*無(wú)效看門狗*/
76 1 WDTCN = 0xde;
77 1 WDTCN = 0xad;
78 1 OSCICN=0x14; /*使用內(nèi)部晶振(2MHZ)*/
79 1 while(1)
80 1 {
81 2 for(;;i++)
82 2 {
83 3 ch=str[i]; /*允許曲譜用空格符 '|'符,換行回車等分隔以便閱讀*/
84 3 if((ch==' ')||(ch=='|')||(ch=='\r')||(ch=='\n')) {i++;continue;}
85 3 if(!ch) {SoundOff(); return;} /*樂曲結(jié)束則播放完畢*/
86 3 if(ch=='#') {halfflg=1; continue;} /*半音標(biāo)志*/
87 3 if(isdigit(ch)||isalpha(ch))
88 3 {
89 4 freq=GetFreq(ch,halfflg); /*從音名獲取頻率*/
90 4 lasttime=16;
91 4 break;
92 4 }
93 3 else {halfflg=0; continue;}
94 3 }
95 2 i++;
96 2 ch=str[i]; /*從下一個(gè)符號(hào)獲取額外音長(zhǎng)符號(hào)*/
97 2 while(1)
98 2 {
99 3 if(!ch) break;
100 3 if(isdigit(ch)||isalpha(ch)) break; /*非音長(zhǎng)符號(hào)則下次處理*/
101 3 if(ch=='-') lasttime+=8; /*額外延時(shí)一拍*/
102 3 if(ch=='.') lasttime+=4; /*額外延時(shí)半拍*/
103 3 if(ch=='_') lasttime/=2; /*下劃線相當(dāng)于簡(jiǎn)譜中音名下面的下劃線,延時(shí)減半*/
104 3 if(ch=='=') lasttime/=4; /*雙下劃線相當(dāng)于簡(jiǎn)譜中音名下面的雙下劃線,延時(shí)減為1/4*/
105 3 i++;
106 3 ch=str[i];
107 3 }
108 2 if(freq!=0) Sound(freq); /*發(fā)聲*/
109 2 else SoundOff();
110 2 delay(lasttime); /*延時(shí)*/
111 2 SoundOff();
112 2 delay(1); /*兩個(gè)引之間的間歇*/
113 2 }
114 1 }
115 void main(void)
116 {
117 1 //uint i;
C51 COMPILER V7.05 WYINYUE 02/11/2006 11:23:06 PAGE 3
118 1 TMOD=0x01; //設(shè)定T0為方式1(16位計(jì)數(shù))
119 1 ET0=1; //允許TO中斷
120 1 EA=1; //開中斷
121 1 delay(10); //延時(shí)
122 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"); /*滿天都是
-小星星*/
123 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"); /*兩只老虎*/
124 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--"); /*山楂樹*/
125 1 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"); /*學(xué)習(xí)雷鋒好榜樣*/
126 1 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-"); /*團(tuán)結(jié)就是力量*/
127 1 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");/*許巍-星
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -