//《世上只有媽媽好》51單片機C語言程序,另加流水燈程序 //2007-5-22作者:徐學(xué)軍 //此程序在硬件上調(diào)試通過 //本程序的單片機晶振采用11.0592MHZ 本文引用地址:http://m.butianyuan.cn/article/201611/321864.htm//此程序應(yīng)某位單片機愛好者的要求而寫 #include sbitspeaker=P3^2;//此I/O口為接喇叭放大電路 unsigned char timer0h,timer0l,time,led=1,j=0; //世上只有媽媽好數(shù)據(jù)表 code unsigned char sszymmh[]={ 6,2,3,5,2,1,3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1, 6,2,4,3,2,2,5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1, 6,1,1,5,2,1,3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2, 5,2,1,6,2,1,3,2,2, 2,2,2, 1,2,4, 5,2,3, 3,2,1, 2,2,1,1,2,1,6,1,1, 1,2,1, 5,1,6, 0,0,0 }; // 音階頻率表 高八位 code unsigned char FREQH[]={ 0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8, 0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i 0xFC,0xFD,0xFD,0xFD,0xFD,0xFE, 0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF, } ; // 音階頻率表 低八位 code unsigned char FREQL[]={ 0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6, 0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, //1,2,3,4,5,6,7,8,i 0xEE,0x44, 0x6B,0xB4,0xF4,0x2D, 0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16, }; void delay(unsigned char t) { unsigned char t1; unsigned long t2; for(t1=0;t1{ if(j==8) { led=1;j=0; } else { P1=led; led=led<<1; j++; } for(t2=0;t2<8000;t2++) { ; } } TR0=0; } void t0int() interrupt 1 { TR0=0; speaker=!speaker; TH0=timer0h; TL0=timer0l; TR0=1; } void song() { TH0=timer0h; TL0=timer0l; TR0=1; delay(time); } void main(void) { unsigned char k,i; TMOD=1; //置CT0定時工作方式1 EA=1;ET0=1;//IE=0x82 //CPU開中斷,CT0開中斷 while(1) { i=0; time=1; while(time) { k=sszymmh[i]+7*sszymmh[i+1]-1; timer0h=FREQH[k]; timer0l=FREQL[k]; time=sszymmh[i+2]; i=i+3; song(); } } } |
評論