我用51单片机做的一个电子琴程序,本人用的是蜂鸣器,你们可以换用喇叭效果会更加好。
-
- #define ulong unsigned long
- //#include <reg52.h> //包括一个52标准内核的头文件
- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- #include <absacc.h>
- uchar key;
- uchar kc;
- uchar sec=0;
- uchar min=0;
- uchar hour=0;
- uchar day=1;
- uchar mon=1;
- uint year=2007;
- uchar mod=0;
- uchar check=0;
- //////////////////////////////////////////////////////////////
- sbit BEEP=P1^1; //喇叭输出脚
- //sbit K1= P3^2;
- //sbit K2= P3^5;
- //sbit K3= P2^4;
- //sbit K4= P2^5;
- uchar th0_f; //在中断中装载的T0的值高8位
- uchar tl0_f; //在中断中装载的T0的值低8位
- //T0的值,及输出频率对照表
- uchar code freq[36*2]={
- 0xA9,0xEF,//00220HZ ,1 //0
- 0x93,0xF0,//00233HZ ,1#
- 0x73,0xF1,//00247HZ ,2
- 0x49,0xF2,//00262HZ ,2#
- 0x07,0xF3,//00277HZ ,3
- 0xC8,0xF3,//00294HZ ,4
- 0x73,0xF4,//00311HZ ,4#
- 0x1E,0xF5,//00330HZ ,5
- 0xB6,0xF5,//00349HZ ,5#
- 0x4C,0xF6,//00370HZ ,6
- 0xD7,0xF6,//00392HZ ,6#
- 0x5A,0xF7,//00415HZ ,7
- 0xD8,0xF7,//00440HZ 1 //12
- 0x4D,0xF8,//00466HZ 1# //13
- 0xBD,0xF8,//00494HZ 2 //14
- 0x24,0xF9,//00523HZ 2# //15
- 0x87,0xF9,//00554HZ 3 //16
- 0xE4,0xF9,//00587HZ 4 //17
- 0x3D,0xFA,//00622HZ 4# //18
- 0x90,0xFA,//00659HZ 5 //19
- 0xDE,0xFA,//00698HZ 5# //20
- 0x29,0xFB,//00740HZ 6 //21
- 0x6F,0xFB,//00784HZ 6# //22
- 0xB1,0xFB,//00831HZ 7 //23
- 0xEF,0xFB,//00880HZ `1
- 0x2A,0xFC,//00932HZ `1#
- 0x62,0xFC,//00988HZ `2
- 0x95,0xFC,//01046HZ `2#
- 0xC7,0xFC,//01109HZ `3
- 0xF6,0xFC,//01175HZ `4
- 0x22,0xFD,//01244HZ `4#
- 0x4B,0xFD,//01318HZ `5
- 0x73,0xFD,//01397HZ `5#
- 0x98,0xFD,//01480HZ `6
- 0xBB,0xFD,//01568HZ `6#
- 0xDC,0xFD,//01661HZ `7 //35
- };
- //-----------------------------------------------------------------
- void delay(uint t) /*延时子程序*/
- {while(t--);
- }
- /////////////////////////////////////////////////////
- void keyin(void) //键盘扫描
- {unsigned char i,j,w;
- delay(500);
- if(~XBYTE[0xf800]&0xf)
- {w=XBYTE[0xf8fe]&0x0f;
- if(~w&0x0f)
- {i=4;
- j=w;
- w=0xff;
- }
- w=XBYTE[0xf8fd]&0x0f;
- if(~w&0x0f)
- {i=3;
- j=w;
- w=0xff;
- }
- w=XBYTE[0xf8fb]&0x0f;
- if(~w&0x0f)
- {i=2;
- j=w;
- w=0xff;
- }
- w=XBYTE[0xf8f7]&0x0f;
- if(~w&0x0f)
- {i=1;
- j=w;
- w=0xff;
- }
- w=XBYTE[0xf8ef]&0x0f;
- if(~w&0x0f)
- {i=0;
- j=w;
- w=0xff;
- }
- w=~j;
- key=(w<<4)|i;
- }
- while(~XBYTE[0xf800]&0xf);
- switch(key)
- {case 0x80:kc=20;break;
- case 0x40:kc=15;break;
- case 0x20:kc=0;break;
- case 0x10:kc=5;break;
- case 0x81:kc=19;break;
- case 0x41:kc=14;break;
- case 0x21:kc=9;break;
- case 0x11:kc=4;break;
- case 0x82:kc=18;break;
- case 0x42:kc=13;break;
- case 0x22:kc=8;break;
- case 0x12:kc=3;break;
- case 0x83:kc=17;break;
- case 0x43:kc=12;break;
- case 0x23:kc=7;break;
- case 0x13:kc=2;break;
- case 0x84:kc=16;break;
- case 0x44:kc=11;break;
- case 0x24:kc=6;break;
- case 0x14:kc=1;break;}
- EX1=1;
- }
- ////////////////////////////////////////////////////////////////////////////
- void keybreak(void) interrupt 2 using 0 //键盘中断
- {
- EX1=0;
- key=0;
- keyin();}
- ///////////////////////////////////////////
-
- //定时中断0,用于产生唱歌频率
- timer0() interrupt 1
- {
- TL0=tl0_f;TH0=th0_f; //调入预定时值
- BEEP=~BEEP; //取反音乐输出IO
- }
- //按键控制音阶声音输出(电子琴)
- void main(void) // 主程序
- {
- ulong n;
- uchar code jie8[8]={12,14,16,17,19,21,23,24};//1234567`1八个音符在频率表中的位置
- TMOD = 0x01; //使用定时器0的16位工作模式
- TR0 = 0;
- ET0 = 1;
- EA = 1;
- while(1)
- {
- switch(kc)
- {
- case 0:
- {
- tl0_f=freq[jie8[0]*2]; //置一个音符的值
- th0_f=freq[jie8[0]*2+1];
- TR0 = 1;
- for(n=0;n<10000;n++); //延时
- break;
- }
- case 1:
- {
- tl0_f=freq[jie8[1]*2]; //置一个音符的值
- th0_f=freq[jie8[1]*2+1];
- TR0 = 1;
- for(n=0;n<10000;n++); //延时
- break;
- }
- case 2:
- {
- tl0_f=freq[jie8[2]*2]; //置一个音符的值
- th0_f=freq[jie8[2]*2+1];
- TR0 = 1;
- for(n=0;n<10000;n++); //延时
- break;
- }
- case 3:
- {
- tl0_f=freq[jie8[3]*2]; //置一个音符的值
- th0_f=freq[jie8[3]*2+1];
- TR0 = 1;
- for(n=0;n<10000;n++); //延时
- break;
- }
- default :TR0 = 0;BEEP=0;
- }
- }
- }
- //------------------------------------------------------
-
复制代码