众所周知电子琴是一种电子键盘乐器,属于电子音乐合成器。
它采用大规模集成电路,大多配置声音记忆存储器(波表)。用于存放各类乐器的真实声音波形并在演奏的时候输出。常用的电子琴有编曲键盘(带自动伴奏)和合成器(无自动伴奏)两大类,广义上的电子琴包括电钢琴(数码钢琴,区别于电声钢琴),多使用五线谱,多为高低音双行记谱。有时也用中音谱和简谱、吉他谱。一般用于摇滚乐。
现在小编来介绍用51单片机与仿真中实现电子琴的播放。
电子琴又称作电子键盘,属于电子乐器(区别于电声乐器),发音音量可以自由调节。音域较宽,和声丰富,甚至可以演奏出一个管弦乐队的效果,表现力极其丰富。它还可模仿多种音色,甚至可以奏出常规乐器所无法发出的声音。另外,电子琴在独奏时,还可随意配上类似打击乐音响的节拍伴奏,适合于演奏节奏性较强的现代音乐。另外,电子琴还安装有效果器,如混响、回声、延音,震音轮和调制轮等多项功能装置,表达各种情绪时运用自如。
所以电子琴的键盘就如小编讲的抢答器键盘一样,用4*4的矩阵键盘。每一个按键赋予一个声调。
现在小编在仿真中实现。
检测程序成功后,我们可以根据自己打的板子,将程序考入自制一个电子琴。
接下来小编展示代码供读者参考。如果想了解矩阵键盘去看小编的抢答器。 文章来源:https://uudwc.com/A/kAj5J
#include <reg52.h>
#include <INTRINS.H>
typedef unsigned int uint ;
typedef unsigned long ulong ;
typedef unsigned char uchar ;
sbit BEEP = P1^0;
sbit KEY = P1^1;
bit a;
uchar KTH0,KTL0,key,b;
uint code tab[]={
64021,64103,64260,64400,
64524,64580,64684,64777,
64820,64898,64968,65030,
65058,65110,65157,65178
};
unsigned int code NoteFrequ[] = {
523,587,659,698,784,880,988,
1047,1175,1319,1397,1568,1760,1976
} ;
bit enable = 1;
bit tmrflag = 0;
void KeyDriver();
void PlayTwoTiger();
void delay(uint a)
{
while(a --);
}
void KeyDriver1()
{
if (a == 0 && KEY == 0)
{
delay(1000);
if ( a == 0 && KEY == 0)
{
a = 1;
}
}
else if (a == 1 && KEY == 1)
{
a = 0;
b ++ ;
b &=0x01;
}
}
void main()
{
unsigned int C;
TMOD = 1;
EA = 1;
ET0 = 1;
while(1)
{
if (b == 0)
{
KeyDriver();
}
else if (b == 1)
{
ET0 = 1;
PlayTwoTiger();
for (C = 0; C<40000; C++);
}
KeyDriver1();
}
}
void KeyDriver()
{
P2 = 0x7f ;
if (P2 != 0x7f)
{
delay(1000);
while(P2 != 0x7f)
{
switch (P2)
{
case 0x77: key = 7; break;
case 0x7b: key = 4; break;
case 0x7d: key = 1; break;
case 0x7e: key = 0; break;
}
P0 = ~key;
KTH0 = tab[key] / 256;
KTL0 = tab[key] % 256;
TR0 = 1;
ET0 = 1;
delay(6000);
ET0 = 0;
}
}
P2 = 0xbf;
if (P2 != 0xbf)
{
delay(1000);
while(P2 != 0xbf)
{
switch (P2)
{
case 0xb7: key = 8; break;
case 0xbb: key = 5; break;
case 0xbd: key = 2; break;
case 0xbe: key = 13; break;
}
P0 = ~key;
KTH0 = tab[key] / 256;
KTL0 = tab[key] % 256;
TR0 = 1;
ET0 = 1;
delay(6000);
ET0 = 0;
}
}
P2 = 0xdf ;
if (P2 != 0xdf)
{
delay(1000);
while(P2 != 0xdf)
{
switch (P2)
{
case 0xd7: key = 9; break;
case 0xdb: key = 6; break;
case 0xdd: key = 3; break;
case 0xde: key = 14; break;
}
P0 = ~key;
KTH0 = tab[key] / 256;
KTL0 = tab[key] % 256;
TR0 = 1;
ET0 = 1;
delay(6000);
ET0 = 0;
}
}
P2 = 0xef ;
if (P2 != 0xef)
{
delay(1000);
while(P2 != 0xef)
{
switch (P2)
{
case 0xe7: key = 10; break;
case 0xeb: key = 11; break;
case 0xed: key = 12; break;
case 0xee: key = 15; break;
}
P0 = ~key;
KTH0 = tab[key] / 256;
KTL0 = tab[key] % 256;
TR0 = 1;
ET0 = 1;
delay(6000);
ET0 = 0;
}
}
}
void InterruptTimer0() interrupt 1
{
TH0 = KTH0;
TL0 = KTL0;
if(b == 0)
{
BEEP = ~BEEP;
}
else if (b == 1)
{
tmrflag = 1;
if (enable)
BEEP = ~BEEP;
else
BEEP = 1;
}
}
void PlayTwoTiger()
{
unsigned char beat;
unsigned char note;
unsigned int time = 0;
unsigned int beatTime = 0;
unsigned int soundTime = 0;
unsigned char code TwoTigerNote[] = {
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, 1, 5, 1, 1, 5, 1,
};
unsigned char code TwoTigerBeat[] = {
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 8,
3, 1, 3, 1, 4, 4, 3, 1, 3, 1, 4, 4, 4, 4, 8, 4, 4, 8,
};
for (beat = 0; beat < sizeof(TwoTigerNote);)
{
while (!tmrflag);
tmrflag = 0;
if (time == 0)
{
note = TwoTigerNote[beat] - 1;
KTH0 = tab[note] >> 8;
KTL0 = tab[note];
beatTime = (TwoTigerBeat[beat] * NoteFrequ[note]) >> 2;
soundTime = beatTime - (beatTime >> 2);
enable = 1;
time++;
}
else
{
if (time >= beatTime)
{
time = 0;
beat++;
}
else
{
time++;
if (time == soundTime)
{
enable = 0;
}
}
}
}
}
文章来源地址https://uudwc.com/A/kAj5J