|
/* 播放兩首音樂(C語言) ********************************* 測試通過*/ #include 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); //生日快樂 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_3=0; for(m=0;m ; P3_3=1; for(m=0;m ; } } delay50us(6); }while((SOUNDLONG[i] !=0) || (SOUNDTONE[j] !=0)); } //延時程序 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, //生日快樂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, //生日快樂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 }; |