|
/* 播放兩首音樂(lè)(C語(yǔ)言) ********************************* 測(cè)試通過(guò)*/ #include<AT89X51.h> void Music(unsigned char number); void delay10ms(unsigned char time); void delay50us(unsigned char time); unsigned char code SOUNDLONG[]; unsigned char code SOUNDTONE[]; void main() { while(1) { Music(1); //生日快樂(lè) delay10ms(250); delay10ms(250); Music(2); //三輪車 delay10ms(250); delay10ms(250); } } void Music(unsigned char number) { unsigned int k,n; unsigned int SoundLong,SoundTone; unsigned int i,j,m; for(k=0;k<number-1;k++) { while(SOUNDLONG[i] !=0){i++;} i++; if(i>=57) i=0; } for(k=0;k<number-1;k++) { while(SOUNDTONE[j] !=0){j++;} j++; if(j>=57) j=0; } do { if(i>=57) i=0; if(j>=57) j=0; SoundLong=SOUNDLONG[i]; SoundTone=SOUNDTONE[j]; i++; j++; for(n=0;n<SoundLong;n++) { for(k=0;k<12;k++) { P3_4=0; for(m=0;m<SoundTone/2;m++) ; P3_4=1; for(m=0;m<SoundTone/2;m++) ; } } delay50us(6); }while((SOUNDLONG[i] !=0) || (SOUNDTONE[j] !=0)); } //延時(shí)程序 void delay10ms(unsigned char time) { unsigned char a,b,c; for(a=0;a<time;a++) for(b=0;b<10;b++) for(c=0;c<120;c++) ; } void delay50us(unsigned char time) { unsigned char a,b; for(a=0;a<time;a++) for(b=0;b<6;b++) ; } unsigned char code SOUNDLONG[]= {9,3,12,12,12,24, 9,3,12,12,12,24, 9,3,12,12,12,12,12, 9,3,12,12,12,24, 0, //生日快樂(lè)end 6,6,9,3,6,6,12, 6,6,6,6,6,6,12, 6,6,9,3,6,6,9,3, 6,3,3,6,3,3,6,6,9, 0 //三輪車end }; unsigned char code SOUNDTONE[]= { 212,212,190,212,159,169, 212,212,190,212,142,159, 212,212,106,126,159,169,190, 119,119,126,159,142,159, 0, //生日快樂(lè)end 239,239,212,189,159,159,189, 159,159,142,126,120,120,159, 120,120,142,159,189,142,159,189, 239,212,189,159,142,159,189,212,239, 0 //三輪車end }; |