原创 按键控制音阶输出实验

2008-9-12 20:22 2853 9 9 分类: MCU/ 嵌入式

       最近一直在看单片机控制声音输出的实验,移植了几个小程序,不知道是单片机晶振不同的原因,还是程序本身的原因,总是有点变声,程序只能看懂不能编。所以还是从简单的编起,模仿着编了这个按键控制音阶的小程序,但是它总是不按我的意思发声,或者不按我的意思止声,本人是个超级菜鸟,身边又没有高手请教,所以只能自己摸索了……


        总算是按自己的意愿发声了,虽然程序很简单,调试的时候还是学到了很多东西。


#include <at89x52.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long


uchar KEY;
uchar key_th0;
uchar key_tl0;
sbit K1=P2^4;
sbit K2=P2^5;
sbit BEEP="P1"^7;
uchar soundf[]={ 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
};
uchar chushi8[8]={12,14,16,17,19,21,23,24};


void delay (ulong CNT)
{
 ulong i;
 for(i=CNT;i>0;i--);
}


uchar getkey()
{
 
 uchar i,j;
 K1=0;
 i=P2;
 delay(1000);
 j=P2;
 K1=1;


 if(j==i)
 {
  switch (j)
  {
   case 0xee: KEY="1";break;
   case 0xed: KEY="2";break;
   case 0xeb: KEY="3";break;
   case 0xe7: KEY="4";break;
   
  }
 }


 delay(5000);
 K2=0;
 i=P2;
 delay(1000);
 j=P2;
 K2=1;


 if (j==i)
 {
  switch (j)
  {
   case 0xde: KEY="5";break;
   case 0xdd: KEY="6";break;
   case 0xdb: KEY="7";break;
   case 0xd7: KEY="8";break;
  }
 }
 
 return KEY;
}


void T0Int ()interrupt 1
{
 TH0=key_th0;
 TL0=key_tl0;
 BEEP=~BEEP;
}


void playkey ()
{
 getkey();
 if(KEY!=0)
 {
  key_tl0=soundf[chushi8[KEY-1]*2];
  key_th0=soundf[chushi8[KEY-1]*2+1];
  TR0=1;
  delay(10000);
  KEY=0;
  TR0=0;
  BEEP=1;
 }


}


void main ()
{
 TMOD=0x01;
 EA=1;
 TR0=0;
 ET0=1;
 KEY=0;
 while (1)
 {
   playkey();
 }
}


 

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
9
关闭 站长推荐上一条 /3 下一条