51单片机电子琴

时间:2025-03-07 17:36:42
  • #include <reg52.h>
  • #include <>
  • 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;
  • }
  • }
  • }
  • }
  • }