- #include<REG51.H>
- sbit play_up=P1^0;
- sbit play_down=P1^1;
- sbit pause=P1^2;
- sbit speaker=P3^1;
- unsigned char timer0h,timer0l,time;
- unsigned char music_num;
- unsigned int num;
- signed int fre;
- unsigned char code FREQL[]={
- 0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6,
- 0x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F,
- 0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D,
- 0x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16};
- unsigned char code FREQH[]={
- 0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8,
- 0xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC,
- 0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE,
- 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF};
- unsigned code dm[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x0c};
- unsigned code sz[]={9,1,6,0,9,5,2,1};
- unsigned code wm[]={0x01,0x02,0x04,0x08,0x80,0x40,0x20,0x10};
- bit play_enable;
- void delay(unsigned char t);
- void delayms(unsigned int t);
- void song(void);
- void music_play(void);
- unsigned char code song1[]={
- 5,2,1, 3,2,1, 2,2,2, 2,2,4, 3,2,1, 1,2,1, 2,2,1, 3,2,4,
- 5,2,1, 3,2,1, 2,2,2, 2,2,2, 5,1,1, 3,2,1, 4,2,1, 3,2,4,
- 3,2,1, 3,2,1, 7,2,1, 3,2,1, 2,2,2, 1,2,1, 7,1,1, 1,2,1,
- 2,2,1, 3,2,1, 6,2,3, 6,1,1, 1,2,1, 3,2,1, 2,2,1, 6,1,1,
- 1,2,1, 7,1,1, 5,1,1, 6,1,6, 5,2,1, 3,2,1, 2,2,2, 2,2,1,
- 2,2,1, 3,2,1, 1,2,1, 2,2,1, 3,2,4, 5,2,1, 3,2,1, 2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1,1, 6,1,1, 1,1,5,
- 1,1,1, 2,1,1, 3,1,1, 2,1,1, 2,1,1, 2,1,1, 1,1,1, 2,1,1, 1,1,1, 6,1,1,
- 3,1,2, 2,1,3, 5,1,1, 6,1,1, 1,2,1, 6,1,2, 6,1,1, 5,1,1, 6,1,1, 5,1,1,
- 6,1,1, 1,1,5, 1,1,1, 2,1,1, 3,1,1, 4,1,2, 4,1,1, 5,1,1, 6,1,1, 6,1,1,
- 5,1,1, 6,1,2, 1,2,1, 6,1,3, 1,2,1, 6,1,1, 5,1,1, 5,1,4, 1,1,1, 6,1,1,
- 5,1,5, 5,1,1, 6,1,1, 1,2,1, 3,1,1, 2,1,1, 3,1,1, 1,1,6, 0,0,0};
- void KEY()
- {
- if(!pause)
- {
- delayms(5);
- if(!pause)
- {
- TR0=0;
- speaker=1;
- if(music_num==0)
- {
- music_num=1;
- num=0;
- play_enable=1;
- }
- else
- {
- play_enable=~play_enable;
- speaker=1;
- }
- while(!pause)
- {
- if(play_enable==0){}
- }
- }
- if((!play_up)&&(music_num!=0))
- {
- delayms(5);
- if((!play_up)&&(music_num!=0))
- {
- TR0=0;
- speaker=1;
- music_num-=1;
- if(music_num<=0)
- music_num=8;
- num=0;
- delayms(500);
- }
- }
- if((!play_down)&&(music_num!=0))
- {
- delayms(5);
- if((!play_down)&&(music_num!=0))
- {
- TR0=0;
- speaker=1;
- music_num+=1;
- if(music_num>=9)
- music_num=1;
- num=0;
- delayms(500);
- }
- }
- }
- }
- void main(void)
- {
- TMOD=0x11;
- ET0=1;
- EA=1;
- while(1)
- {
- music_play();
- KEY();
- }
- }
- void delayms(unsigned int t)
- {
- unsigned int i,j;
- for(i=0;i<t;i++)
- {
- for(j=0;j<123;j++)
- ;
- }
- }
- void delay(unsigned char t)
- {
- unsigned char t1;
- unsigned long t2;
- for(t1=0;t1<t;t1++)
- {
- for(t2=0;t2<6500;t2++)
- {
- KEY();
- }
- }
- TR0=0;
- }
- void timer0(void) interrupt 1
- {speaker=!speaker;
- TH0=timer0h;
- TL0=timer0l;
- }
- void song(void)
- {
- TH0=timer0h;
- TL0=timer0l;
- TR0=1;
- delay(time);
- }
- void music_play(void)
- { int i;
- i=0;
-
- for(i=0;i<8;i++)
- { P2=0x00;
- P0=0x00;
- if(play_enable==1)
- {
- P0=dm[music_num];
- P2=0x99;
- break;}
- else if(play_enable==0)
- {
- P0=dm[sz[i]];
- P2=wm[i];
- delayms(5);
- P2=0x00;
- };
- }
- if((music_num==1)&&(play_enable==1))
- {
- fre=song1[num]+7*song1[num+1]-1;
- timer0h=FREQH[fre];
- timer0l=FREQL[fre];
- time=song1[num+2];
- num+=3;
- if(fre<0)
- {
- num=0;
- music_num=2;
- }
- song();
- }
- if((music_num==2)&&(play_enable==1))
- {
- fre=song2[num]+7*song2[num+1]-1;
- timer0h=FREQH[fre];
- timer0l=FREQL[fre];
- time=song2[num+2];
- num+=3;
- if(fre<0)
- {
- num=0;
- music_num=3;
- }
- song();
- }
- if((music_num==3)&&(play_enable==1))
- {
- fre=song3[num]+7*song3[num+1]-1;
- timer0h=FREQH[fre];
- timer0l=FREQL[fre];
- time=song3[num+2];
- num+=3;
- if(fre<0)
- {
- num=0;
- music_num=4;
- }
- song();
- }
- if((music_num==4)&&(play_enable==1))
- {
- fre=song4[num]+7*song4[num+1]-1;
- timer0h=FREQH[fre];
- timer0l=FREQL[fre];
- time=song4[num+2];
- num+=3;
- if(fre<0)
- {
- num=0;
- music_num=5;
- }
- song();
- }
- if((music_num==5)&&(play_enable==1))
- {
- fre=song5[num]+7*song5[num+1]-1;
- timer0h=FREQH[fre];
- timer0l=FREQL[fre];
- time=song5[num+2];
- num+=3;
- if(fre<0)
- {
- num=0;
- music_num=6;
- }
- song();
- }
- if((music_num==6)&&(play_enable==1))
- {
- fre=song6[num]+7*song6[num+1]-1;
- timer0h=FREQH[fre];
- timer0l=FREQL[fre];
- time=song6[num+2];
- num+=3;
- if(fre<0)
- {
- num=0;
- music_num=7;
- }
- song();
- }
- if((music_num==7)&&(play_enable==1))
- {
- fre=song7[num]+7*song7[num+1]-1;
- timer0h=FREQH[fre];
- timer0l=FREQL[fre];
- time=song7[num+2];
- num+=3;
- if(fre<0)
- {
- num=0;
- music_num=8;
- }
- song();
- }
- if((music_num==8)&&(play_enable==1))
- {
- fre=song8[num]+7*song8[num+1]-1;
- timer0h=FREQH[fre];
- timer0l=FREQL[fre];
- time=song8[num+2];
- num+=3;
- if(fre<0)
- {
- num=0;
- music_num=1;
- }
- song();
- }
- }
復制代碼 |