📄 wyinyue.lst
字号:
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 此程序将某段音乐用特定的频率播放出来
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; /*产生方波的定时器的初值*/
26 uchar timer1cnt; /*定时器延时计数 */
27 uchar timer1cntflg; /*定时器定时完成标志 */
28 void timer0int () interrupt 1//定时器0中断用来产生方波
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)// 延时
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)// 发声
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)// 停止发声
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)// 依据音名取对应的频率
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 /*无效看门狗*/
76 1 WDTCN = 0xde;
77 1 WDTCN = 0xad;
78 1 OSCICN=0x14; /*使用内部晶振(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;} /*乐曲结束则播放完毕*/
86 3 if(ch=='#') {halfflg=1; continue;} /*半音标志*/
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]; /*从下一个符号获取额外音长符号*/
97 2 while(1)
98 2 {
99 3 if(!ch) break;
100 3 if(isdigit(ch)||isalpha(ch)) break; /*非音长符号则下次处理*/
101 3 if(ch=='-') lasttime+=8; /*额外延时一拍*/
102 3 if(ch=='.') lasttime+=4; /*额外延时半拍*/
103 3 if(ch=='_') lasttime/=2; /*下划线相当于简谱中音名下面的下划线,延时减半*/
104 3 if(ch=='=') lasttime/=4; /*双下划线相当于简谱中音名下面的双下划线,延时减为1/4*/
105 3 i++;
106 3 ch=str[i];
107 3 }
108 2 if(freq!=0) Sound(freq); /*发声*/
109 2 else SoundOff();
110 2 delay(lasttime); /*延时*/
111 2 SoundOff();
112 2 delay(1); /*两个引之间的间歇*/
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; //设定T0为方式1(16位计数)
119 1 ET0=1; //允许TO中断
120 1 EA=1; //开中断
121 1 delay(10); //延时
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"); /*学习雷锋好榜样*/
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-"); /*团结就是力量*/
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");/*许巍-星
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -