22. 電子琴
(1.由4X4組成16個按鈕矩陣,設(shè)計成16個音。
(2.可隨意彈奏想要表達的音樂。
2.
(1.把“單片機系統(tǒng)”區(qū)域中的P1.0端口用導(dǎo)線連接到“音頻放大模塊”區(qū)域中的SPKIN端口上;
(2.把“單片機系統(tǒng)“區(qū)域中的P3.0-P3.7端口用8芯排線連接到“4X4行列式鍵盤”區(qū)域中的C1-C4R1-R4端口上;
4.
(1.4X4行列式鍵盤識別;
(2.音樂產(chǎn)生的方法;
一首音樂是許多不同的音階組成的,而每個音階對應(yīng)著不同的頻率,這樣我們就可以利用不同的頻率的組合,即可構(gòu)成我們所想要的音樂了,當(dāng)然對于單片機來產(chǎn)生不同的頻率非常方便,我們可以利用單片機的定時/計數(shù)器T0來產(chǎn)生這樣方波頻率信號,因此,我們只要把一首歌曲的音階對應(yīng)頻率關(guān)系弄正確即可。現(xiàn)在以單片機12MHZ晶振為例,例出高中低音符與單片機計數(shù)T0相關(guān)的計數(shù)值如下表所示
音符
頻率(HZ)
簡譜碼(T值)
音符
頻率(HZ)
簡譜碼(T值)
低1DO
262
63628
#4FA#
740
64860
#1DO#
277
63731
中5SO
784
64898
低2RE
294
63835
#5SO#
831
64934
#2RE#
311
63928
中6LA
880
64968
低3M
330
64021
#6
932
64994
低4FA
349
64103
中7SI
988
65030
#4FA#
370
64185
高1DO
1046
65058
低5SO
392
64260
#1DO#
1109
65085
#5SO#
415
64331
高2RE
1175
65110
低6LA
440
64400
#2RE#
1245
65134
#6
466
64463
高3M
1318
65157
低7SI
494
64524
高4FA
1397
65178
中1DO
523
64580
#4FA#
1480
65198
#1DO#
554
64633
高5SO
1568
65217
中2RE
587
64684
#5SO#
1661
65235
#2RE#
622
64732
高6LA
1760
65252
中3M
659
64777
#6
1865
65268
中4FA
698
64820
高7SI
1967
65283
下面我們要為這個音符建立一個表格,有助于單片機通過查表的方式來獲得相應(yīng)的數(shù)據(jù)
低音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、音樂的音拍,一個節(jié)拍為單位(C調(diào))
曲調(diào)值
DELAY
曲調(diào)值
DELAY
調(diào)4/4
125ms
調(diào)4/4
62ms
調(diào)3/4
187ms
調(diào)3/4
94ms
調(diào)2/4
250ms
調(diào)2/4
125ms
對于不同的曲調(diào)我們也可以用單片機的另外一個定時/計數(shù)器來完成。
下面就用AT89S51單片機產(chǎn)生一首“生日快樂”歌曲來說明單片機如何產(chǎn)生的。
在這個程序中用到了兩個定時/計數(shù)器來完成的。其中T0用來產(chǎn)生音符頻率,T1用來產(chǎn)生音拍。
5.
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
MOVA,P3
ANLA,#0FH
XRLA,#0FH
JZNOKEY1
MOVA,P3
ANLA,#0FH
CJNEA,#0EH,NK1
MOVKEYBUF,#0
LJMPDK1
NK1:CJNEA,#0DH,NK2
MOVKEYBUF,#1
LJMPDK1
NK2:CJNEA,#0BH,NK3
MOVKEYBUF,#2
LJMPDK1
NK3:CJNEA,#07H,NK4
MOVKEYBUF,#3
LJMPDK1
NK4:NOP
DK1:
MOVA,KEYBUF
MOVDPTR,#TABLE
MOVCA,@A+DPTR
MOVP0,A
MOVA,KEYBUF
MOVB,#2
MULAB
MOVTEMP,A
MOVDPTR,#TABLE1
MOVCA,@A+DPTR
MOVSTH0,A
MOVTH0,A
INCTEMP
MOVA,TEMP
MOVCA,@A+DPTR
MOVSTL0,A
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H0,A
INCTEMP
MOVA,TEMP
MOVCA,@A+DPTR
MOVSTL0,A
MOVTL0,A
SETBTR0
DK3A:MOVA,P3
ANLA,#0FH
XRLA,#0FH
JNZDK3A
CLRTR0
NOKEY3:
MOVP3,#0FFH
CLRP3.7
MOVA,P3
ANLA,#0FH
XRLA,#0FH
JZNOKEY4
LCALLDELY10MS
MOVA,P3
ANLA,#0FH
XRLA,#0FH
JZNOKEY4
MOVA,P3
ANLA,#0FH
CJNEA,#0EH,NK13
MOVKEYBUF,#12
LJMPDK4
NK13:CJNEA,#0DH,NK14
MOVKEYBUF,#13
LJMPDK4
NK14:CJNEA,#0BH,NK15
MOVKEYBUF,#14
LJMPDK4
NK15:CJNEA,#07H,NK16
MOVKEYBUF,#15
LJMPDK4
NK16:NOP
DK4:
MOVA,KEYBUF
MOVDPTR,#TABLE
MOVCA,@A+DPTR
MOVP0,A
MOVA,KEYBUF
MOVB,#2
MULAB
MOVTEMP,A
MOVDPTR,#TABLE1
MOVCA,@A+DPTR
MOVSTH0,A
MOVTH0,A
INCTEMP
MOVA,TEMP
MOVCA,@A+DPTR
MOVSTL0,A
MOVTL0,A
SETBTR0
DK4A:MOVA,P3
ANLA,#0FH
XRLA,#0FH
JNZDK4A
CLRTR0
NOKEY4:
LJMPWAIT
DELY10MS:
MOVR6,#10
D1:MOVR7,#248
DJNZR7,$
DJNZR6,D1
RET
INT_T0:
MOVTH0,STH0
MOVTL0,STL0
CPLP1.0
RETI
TABLE:DB3FH,06H,5BH,4FH,66H,6DH,7DH,07H
DB7FH,6FH,77H,7CH,39H,5EH,79H,71H
TABLE1:DW64021,64103,64260,64400
DW64524,64580,64684,64777
DW64820,64898,64968,65030
DW65058,65110,65157,65178
END
7.
#include
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>0;j--);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
switch(temp)
{
case0x0e:
key=0;
break;
case0x0d:
key=1;
break;
case0x0b:
key=2;
break;
case0x07:
key=3;
break;
}
temp=P3;
P1_0=~P1_0;
P0=table[key];
STH0=tab[key]/256;
STL0=tab[key]%256;
TR0=1;
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
TR0=0;
}
}
P3=0xff;
P3_5=0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
switch(temp)
{
case0x0e:
key=4;
break;
case0x0d:
key=5;
break;
case0x0b:
key=6;
break;
case0x07:
key=7;
break;
}
temp=P3;
P1_0=~P1_0;
P0=table[key];
STH0=tab[key]/256;
STL0=tab[key]%256;
TR0=1;
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
TR0=0;
}
}
P3=0xff;
P3_6=0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
switch(temp)
{
case0x0e:
key=8;
break;
case0x0d:
key=9;
break;
case0x0b:
key=10;
break;
case0x07:
key=11;
break;
}
temp=P3;
P1_0=~P1_0;
P0=table[key];
STH0=tab[key]/256;
STL0=tab[key]%256;
TR0=1;
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
TR0=0;
}
}
P3=0xff;
P3_7=0;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
switch(temp)
{
case0x0e:
key=12;
break;
case0x0d:
key=13;
break;
case0x0b:
key=14;
break;
case0x07:
key=15;
break;
}
temp=P3;
P1_0=~P1_0;
P0=table[key];
STH0=tab[key]/256;
STL0=tab[key]%256;
TR0=1;
temp=temp&0x0f;
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
TR0=0;
}
}
}
}
voidt0(void)interrupt1using0
{
TH0=STH0;
TL0=STL0;
P1_0=~P1_0;
}
評論