Các bác ạ, em thì làm được cái đồng hồ rồi, nhưng chuông thì khó quá, bác nào biết làm chuông nhạc melody thì giúp em với ( sơ đồ + cách làm ).
Cảm ơn các bác nhé
Cảm ơn các bác nhé
// #include<reg51.h> unsigned char preloadTH=0xFF,preloadTL=0xFF; unsigned int pointer=0; //octa: 0 //key : C C# D D# E F F# G G# A A# B code float key_octa0[]={16.35,17.32,18.35,19.45,20.6,21.83,23.12,24.5,25.96,27.5,29.14,30.87}; code unsigned int chuan=100; code float crystal=11.0592; //music code unsigned char cau_ho_ben_bo_hien_luong[]={"4d~,4d~,3g~,3a~,4d.,4d~,4g~,4a~,4b~,4a=,4g=,4d.,4e~,3a~,4c~,3g~,3a~,4d~,4d ,4d=,4c=,3a~,4c~,3g~,3a~,4do,4g~,4g~,4d~,4a~,4g.,4g~,4a~,4g~,4b=,4a=,4g~,4d.,4g~,4e ,4e~,4d~,4C ,4d~,4d~,3a~,4d~,3b~,3a~,3go,4d ,4go,4go,4a ,4b~,4a~,4f~,4g.,4d~,4g~,4a~,4a=,4A~,4a~,4g.,4d~,3a ,3a ,3a~,4d ,4d~,4d ,3a~,3b~,3a~,3g.,4b=,4a=,4g~,4d~,4g~,4d~,3a.,4g~,4g ,4a~,4f~,4a~,5d,4a~,4b~,4a~,4d ,3a ,4go,4d~,4d~,3g~,3a~,4do,4g~,4g~,4d~,3a~,4go,4g~,4d~,4g~,4a~,4A ,4a~,5d~,4b~,4a~,4g~,4f~,4g,s"}; void delayms(unsigned int t) { unsigned int i,j; for(i=0;i<t;i++) for(j=0;j<455;j++); } unsigned char ngattimer_0(void) interrupt 1 { p20=!p20; TL0=preloadTL; TH0=preloadTH; } void doitanso(float tanso) { unsigned int giatrinap; giatrinap=65530-(unsigned int)((1000000*crystal)/(24*tanso))+6; preloadTL=(unsigned char)giatrinap; preloadTH=(unsigned char)(giatrinap>>8); } void key(unsigned char octa,unsigned char k) { float tanso; switch (k) { case 'c' : tanso=key_octa0[0]; break; case 'C' : tanso=key_octa0[1]; break; case 'd' : tanso=key_octa0[2]; break; case 'D' : tanso=key_octa0[3]; break; case 'e' : tanso=key_octa0[4]; break; case 'f' : tanso=key_octa0[5]; break; case 'F' : tanso=key_octa0[6]; break; case 'g' : tanso=key_octa0[7]; break; case 'G' : tanso=key_octa0[8]; break; case 'a' : tanso=key_octa0[9]; break; case 'A' : tanso=key_octa0[10]; break; case 'b' : tanso=key_octa0[11]; break; } switch (octa) { case '1' : tanso*=2; break; case '2' : tanso*=4; break; case '3' : tanso*=8; break; case '4' : tanso*=16; break; case '5' : tanso*=32; break; case '6' : tanso*=64; break; case '7' : tanso*=128; break; case '8' : tanso*=256; break; } doitanso(tanso); } void tempo(unsigned char tem) { switch (tem) { case '=' : delayms(chuan); break; case '~' : delayms(chuan*2); break; case ' ' : delayms(chuan*4); break; case '.' : delayms(chuan*6); break; case 'o' : delayms(chuan*16); break; } } void play(unsigned char *music) { TR0=1; while(1) { if(music[pointer]=='s') break; else { key(music[pointer],music[pointer+1]); tempo(music[pointer+2]); TR0=0; delayms(10); TR0=1; } pointer+=4; } pointer=0; TR0=0; } void main(void) { TMOD=17; TL0=0xff; TH0=0xff; ET0=1; EA=1; TR0=1; play(cau_ho_ben_bo_hien_luong); while(1) { } }
Comment