- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit key1=P3^2; /*暂停键 */
- sbit key2=P3^3; /* 切换歌曲键*/
- sbit fm=P1^1; /*蜂鸣器口*/
- sbit MOD=P1^7;
- uchar count=0; /*歌曲标志*/
- uchar timeh,timel,i;
- uchar code DSY_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
- 0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x70};
- uint code tone_delay_table[]={ 64021,64103,64260,64400,64524,64580,64684,
- 64777,64820,64898,64968,65030,65058,65110,65157,65178};/*每个音符对应的延迟*/
- uchar code huayang[]={0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x0f,0xf0,0x00};
- void delay_ms(uchar x) /*延迟函数*/
- {
- uchar i;
- while(x--)
- for(i=0;i<120;i++);
- }
- uchar code qnzl[]={ /*千年之恋*/
- 0x12,0x22,0x34,0x84,0x74,0x54,0x38,0x42,0x32,0x22,0x42,0x34,0x84,0x72,0x82,0x94,0xA8,0x08,
- 0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
- 0x32,0x31,0x21,0x32,0x82,0x71,0x81,0x71,0x51,0x32,0x22,
- 0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
- 0x32,0x31,0x21,0x32,0x83,0x82,0x71,0x72,0x02,
- 0x63,0xA1,0xA2,0x62,0x92,0x82,0x52,
- 0x31,0x51,0x63,0x51,0x63,0x51,0x63,0x51,0x62,0x82,0x7C,0x02,
- 0x61,0x71,0x82,0x71,0x62,0xA2,0x71,0x76,
- 0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
- 0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
- 0x42,0x63,0x83,0x83,0x91,0x91,
- 0x61,0x71,0x82,0x71,0x62,0x0A2,0x71,0x76,
- 0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
- 0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
- 0x42,0x82,0x88,0x02,0x74,0x93,0x89,0xff /*结束标志*/
- };
- uchar code jmszl[]={ /*寂寞沙洲冷*/
- 0x12,0x12,0x22,0x32,0x31,0x22,0x21,0x22,
- 0x21,0x31,0x51,0x52,0x31,0x52,0x61,0x15,0x14,
- 0x51,0x52,0x31,0x52,0x62,0x13,0x11,0x13,0x32,0x28,0x08,0x28,
- 0x31,0x32,0x31,0x32,0x11,0x21,0x51,0x52,0x51,0x52,
- 0x51,0x51,0x31,0x32,0x31,0x32,0x81,0x72,0x63,
- 0x62,0x71,0x81,0x72,0x61,0x61,0x52,0x31,0x21,0x32,0x51,0x54,
- 0x22,0x12,0x11,0x12,0x11,0x12,0x12,0x14,0x26,0x32,0x26,
- 0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,
- 0x02,0x32,0x81,0x81,0x81,0x81,0x62,0x52,0x34,
- 0x31,0x81,0x81,0x81,0x61,0x91,0x82,
- 0x51,0x51,0x51,0x51,0x31,0x61,0x53,
- 0x21,0x11,0x21,0x11,0x22,0x11,0x21,0x26,
- 0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,0x52,
- 0x31,0x31,0x81,0x81,0x81,0x61,0x91,0x81,0x61,0x31,0x56,
- 0x32,0x32,0x81,0x81,0x81,0x81,0x91,0x81,0x61,0x81,0x61,0x51,0x31,0x51,0x34,
- 0x21,0x31,0x51,0x31,0x21,0x11,0x61,0x21,0x16,0xff /*结束标志*/
- };
- uchar code qizige[]={ /*七子之歌*/
- 0x54,0x32,0x52,0x32,0x54,0x62,0x52,0x32,0x62,0x54,
- 0x14,0x12,0x22,0x34,0x52,0x32,0x02,0x32,0x58,
- 0x52,0x52,0x62,0x52,0x32,0x54,0x52,0x62,0x52,0x82,0x62,0x58,
- 0x14,0x52,0x32,0x22,0x12,0x24,0x32,0x54,0x2,0x22,0x34,0x1f,0x18,
- 0x04,0x54,0xa4,0x92,0x81,0x62,0x52,0x54,0x62,0x64,0x52,0x62,0x81,0xa2,0x82,0x9c,
- 0x02,0x52,0xa4,0x92,0x81,0x62,0x52,0x54,0x62,0x64,0x52,0x64,0xa2,0x92,0x9f,
- 0xa4,0x92,0x81,0x62,0x52,0x54,0x84,0x62,0x52,0x32,0x22,0x14,
- 0x04,0x22,0x32,0x58,0x58,0x04,0x52,0x62,0x8f,
- 0xff /*结束标志*/
- };
- uchar code ganen[]={ /*感恩的心*/
- 0x12,0x14,0x22,0x32,0x54,0x32,0x84,0x72,0x62,0x54,
- 0x02,0x62,0x62,0x52,0x54,0x12,0x22,0x32,0x28,
- 0x12,0x14,0x22,0x32,0x54,0x32,0x84,0x92,0x82,0x54,
- 0x32,0x24,0x21,0x61,0x54,0x22,0x32,0x12,0x18,
- 0x22,0x24,0x12,0x24,0x11,0x21,0x34,0x32,0x21,0x31,0x34,
- 0x12,0x22,0x22,0x22,0x11,0x21,0x22,0x64,0x52,0x52,0x32,0x38,
- 0x31,0x52,0x51,0x52,0x31,0x51,0x58,0x31,0x82,0x81,0x82,0x31,0x61,0x68,
- 0x62,0x62,0x62,0x51,0x61,0x64,0x61,0x82,0x61,0x9c,
- 0x52,0xa4,0x92,0xa4,0x32,0x84,0x72,0x64,0x62,0x94,0x82,0x94,
- 0x61,0x81,0x92,0x91,0x91,0x92,0x81,0xa1,0xa2,0x92,0x94,
- 0x52,0xa4,0x92,0xa4,0x32,0x84,0x72,0x64,
- 0x52,0x62,0x81,0x81,0x82,0x92,0xa2,0x94,0x82,0x72,0x82,0x88,
- 0xff /*结束标志*/
- };
- uchar code cuzhi[]={
- 0xff,0xff, 0xFC,0x8E, 0xFC,0xED,
- 0xFD,0x43,
- 0xFD,0x6A,
- 0xFD,0xB3,
- 0xFD,0xF3,
- 0xFE,0x2D,
- 0xFE,0x47, 0xFE,0x76,
- 0xFE,0xA1,
- 0xFE,0xC7,
- 0xFE,0xD9,
- 0xFE,0xF9,
- 0xFF,0x16
- };
- void delay1(uint z); /* 延时1MS*/
- void delay(uint z); /*延时165ms,既16分音符*/
- void song();
- void main()
- {
- P0=0xbf;
- EA=1;
- EX0=1;
- IT0=1;
- EX1=1;
- IT1=1;
- TMOD=0x01;
- TH0=0;
- TL0=0;
- ET0=1;
- while(1)
- {
- if(MOD==0)
- {
- count++;
- if(count==5)
- count=1;
- }
- if(count!=0)
- {
- song();
- delay1(1000);
- }
-
- }
- }
- void int0() interrupt 0
- {
- EA=0;
- delay1(1);
- if(key1==0)
- {
- count=0;/*暂停音乐*/
- TR0=0;
- }
- EA=1;
- }
- void int1() interrupt 2
- {
- TR0 = 0;
- delay1(1); /*去抖*/
- if(key2==0)
- {
- i=0;/*从头开始唱*/
- count++;
- TR0=~TR0;
- if(count==5)
- count=0;
- }
- }
- void timer0() interrupt 1/*用于产生各种音调*/
- {
- if(count!=0) //放音乐
- {
- TH0=timeh;
- TL0=timel;
- fm=~fm;
- }
- }
- void song()
- {
- uint temp;
- uchar jp;
- i=0;
- while(1)
- {
- if(count==0)
- {
- break;
- }
- if(count==1) //选曲
- temp=jmszl[i];
- if(count==2)
- temp=qnzl[i];
- if(count==3)
- temp=qizige[i];
- if(count==4)
- temp=ganen[i];
- if(temp==0xff)
- break;
- jp=temp/16;
- P2 = huayang[jp];
- P0=DSY_table[count];
- if(jp!=0)
- {
- timeh=cuzhi[jp*2];
- timel=cuzhi[jp*2+1];
- }
- else
- {
- TR0=0;
- fm=1;
- }
- delay(temp%16);
- TR0=0;
-
- fm=1;
- delay1(10);
- TR0=1;
- i++;
- }
- TR0=0;
- fm=1;
- }
- void delay(uint z)/*延时165ms,既16分音符*/
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=19000;y>0;y--);
- }
- void delay1(uint z)/*延时1ms*/
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=112;y>0;y--);
- }
复制代码
|