电子琴Word文件下载.docx
《电子琴Word文件下载.docx》由会员分享,可在线阅读,更多相关《电子琴Word文件下载.docx(20页珍藏版)》请在冰豆网上搜索。
低2 RE
294
63835
#5SO#
831
64934
#2RE#
311
63928
中6LA
880
64968
低3M
330
64021
#6
932
64994
低4FA
349
64103
中7SI
988
65030
370
64185
高1DO
1046
65058
低5SO
392
64260
#1DO#
1109
65085
415
64331
高2RE
1175
65110
低6LA
440
64400
#2RE#
1245
65134
466
64463
高3M
1318
65157
低7SI
494
64524
高4FA
1397
65178
中1DO
523
64580
1480
65198
554
64633
高5SO
1568
65217
中2RE
587
64684
1661
65235
622
64732
高6LA
1760
65252
中3M
659
64777
1865
65268
中4FA
698
64820
高7SI
1967
65283
下面我们要为这个音符建立一个表格,有助于单片机通过查表的方式来获得相应的数据
低音0-19之间,中音在20-39之间,高音在40-59之间
TABLE:
DW0,63628,63835,64021,64103,64260,64400,64524,0,0
DW0,63731,63928,0,64185,64331,64463,0,0,0
DW0,64580,64684,64777,64820,64898,64968,65030,0,0
DW0,64633,64732,0,64860,64934,64994,0,0,0
DW0,65058,65110,65157,65178,65217,65252,65283,0,0
DW0,65085,65134,0,65198,65235,65268,0,0,0
DW0
2、音乐的音拍,一个节拍为单位(C调)
曲调值
DELAY
调4/4
125ms
62ms
调3/4
187ms
94ms
调2/4
250ms
对于不同的曲调我们也可以用单片机的另外一个定时/计数器来完成。
下面就用AT89S51单片机产生一首“生日快乐”歌曲来说明单片机如何产生的。
在这个程序中用到了两个定时/计数器来完成的。
其中T0用来产生音符频率,T1用来产生音拍。
5.5.程序框图
图4.22.2
6.6.汇编源程序
KEYBUFEQU30H
STH0EQU31H
STL0EQU32H
TEMPEQU33H
ORG00H
LJMPSTART
ORG0BH
LJMPINT_T0
START:
MOVTMOD,#01H
SETBET0
SETBEA
WAIT:
MOVP3,#0FFH
CLRP3.4
MOVA,P3
ANLA,#0FH
XRLA,#0FH
JZNOKEY1
LCALLDELY10MS
ANLA,#0FH
CJNEA,#0E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,71H
TABLE1:
DW64021,64103,64260,64400
DW64524,64580,64684,64777
DW64820,64898,64968,65030
DW65058,65110,65157,65178
END
7.7.
C语言源程序
#include<
AT89X51.H>
unsignedcharcodetable[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
unsignedchartemp;
unsignedcharkey;
unsignedchari,j;
unsignedcharSTH0;
unsignedcharSTL0;
unsignedintcodetab[]={64021,64103,64260,64400,
64524,64580,64684,64777,
64820,64898,64968,65030,
65058,65110,65157,65178};
voidmain(void)
{
TMOD=0x01;
ET0=1;
EA=1;
while
(1)
{
P3=0xff;
P3_4=0;
temp=P3;
temp=temp&
0x0f;
if(temp!
=0x0f)
for(i=50;
i>
0;
i--)
for(j=200;
j>
j--);
switch(temp)
case0x0e:
key=0;
break;
case0x0d:
key=1;
case0x0b:
key=2;
case0x07:
key=3;
}
P1_0=~P1_0;
P0=table[key];
STH0=tab[key]/256;
STL0=tab[key]%256;
TR0=1;
while(temp!
TR0=0;
P3_5=0;
key=4;
key=5;
key=6;
key=7;
P3_6=0;
key=8;
key=9;
key=10;
key=11;
P3_7=0;
key=12;
key=13;
key=14;
key=15;
}
}
voidt0(void)interrupt1using0
TH0=STH0;
TL0=STL0;