#includereg52.h//包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义sbitspeaker=P2^3;//定义音乐输出端口,需要连接到对应的喇叭#defineucharunsignedchar#defineuintunsignedintuchartimer0h,timer0l,time;codeucharsszymmh[]={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};//世上只有妈妈好codeucharmldsh[]={6,1,2,3,2,2,7,1,3,6,1,1,1,2,1,2,2,1,1,2,1,3,2,4,6,1,2,6,2,2,5,2,1,6,2,1,5,2,1,2,2,1,4,2,1,3,2,1,3,2,2,6,1,2,6,2,2,5,2,1,6,2,1,5,2,1,6,2,1,5,2,2,2,2,1,3,2,4,1,2,4,6,1,2,3,2,2,2,2,3,7,1,1,6,1,8,6,1,2,3,2,2,7,2,3,7,1,1,6,1,1,1,2,1,2,2,1,1,2,1,3,2,4,6,1,2,6,2,2,5,2,1,6,2,1,5,2,1,2,2,1,4,2,1,3,2,1,3,2,6,6,1,2,6,2,2,5,2,1,2,2,1,3,2,1,4,2,1,3,2,2,2,2,2,1,2,4,6,2,2,2,2,2,1,2,3,7,1,1,6,1,6,6,1,1,7,1,1,1,2,1,2,2,1,3,2,1,1,2,1,7,2,2,5,1,2,6,1,1,1,2,1,2,2,1,3,2,1};//美丽的神话codeucharlzlh[]={1,2,2,2,2,2,3,2,2,1,2,2,1,2,2,2,2,2,3,2,2,1,2,2,3,2,2,4,2,2,5,2,4,3,2,2,4,2,2,5,2,4,5,2,1,6,2,1,5,2,1,4,2,1,3,2,2,1,2,2,5,2,1,6,2,1,5,2,1,4,2,1,3,2,2,1,2,2,2,2,2,5,1,2,1,2,4,2,2,2,5,1,2,1,2,4,1,2,2,2,2,2,3,2,2,1,2,2,1,2,2,2,2,2,3,2,2,1,2,2,3,2,2,4,2,2,5,2,4,3,2,2,4,2,2,5,2,4,5,2,1,6,2,1,5,2,1,4,2,1,3,2,2,1,2,2,5,2,1,6,2,1,5,2,1,4,2,1,3,2,2,1,2,2,2,2,2,5,1,2,1,2,4,2,2,2,5,1,2,1,2,4};//两只老虎codeucharsrg[]={5,1,1,5,1,1,6,1,2,5,1,2,1,2,2,7,1,4,5,1,1,5,1,1,6,1,2,5,1,2,2,2,2,1,2,4,5,1,1,5,1,1,5,2,2,3,2,2,1,2,2,7,1,1,6,1,1,4,2,1,4,2,1,3,2,2,1,2,2,2,2,2,1,2,4};//生日歌//音阶频率表高八位codeucharFREQH[]={0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC,//1,2,3,4,5,6,7,8,i0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,};//音阶频率表低八位codeucharFREQL[]={0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F,//1,2,3,4,5,6,7,8,i0xEE,0x44,0x6B,0xB4,0xF4,0x2D,0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,};ucharconstEL[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//0-F/*******************************************************//*函数声明*//*******************************************************/ucharkeyscan(void);//键盘扫描voiddelay1(uinti);//演示程序voiddelay2(unsignedchart);voidyy1(void);//音乐播放voidyy2(void);voidyy3(void);voidyy4(void);/****************************************************//*主函数*//****************************************************/voidmain(){ucharkey;P2=0;//1数码管亮按相应的按键,会显示按键上的字符P0=0X00;while(1){key=keyscan();//调用键盘扫描,switch(key){case0xee:P0=EL[0];P1=0xfe;yy1();break;//0按下相应的键显示相对应的码值case0xde:P0=EL[1];P1=0xfd;yy2();break;//1按下相应的键显示相对应的码值case0xbe:P0=EL[2];P1=0xfb;yy3();break;//2按下相应的键显示相对应的码值case0x7e:P0=EL[3];P1=0xf7;yy4();break;//3按下相应的键显示相应的码值}}}/*------------------------------------------------键盘扫描程序------------------------------------------------*/ucharkeyscan(void)//键盘扫描函数,使用行列反转扫描法{ucharcord_h,cord_l;//行列值中间变量P3=0x0f;//行线输出全为0cord_h=P3&0x0f;//读入列线值if(cord_h!=0x0f)//先检测有无按键按下{delay1(100);//去抖if(cord_h!=0x0f){cord_h=P3&0x0f;//读入列线值P3=cord_h|0xf0;//输出当前列线值cord_l=P3&0xf0;//读入行线值return(cord_h+cord_l);//键盘最后组合码值}}return(0xff);//返回该值}/*------------------------------------------------延时程序------------------------------------------------*/voiddelay1(uinti)//延时函数{while(i--);}voiddelay2(unsignedchart){unsignedchart1;unsignedlongt2;for(t1=0;t1t;t1++){for(t2=0;t28000;t2++){;}}TR0=0;}/******************************************************************//*定时器中断函数*//******************************************************************/voidt0int()interrupt1{TR0=0;speaker=!speaker;TH0=timer0h;TL0=timer0l;TR0=1;}/******************************************************************//*音乐处理函数*//******************************************************************/voidsong(){TH0=timer0h;TL0=timer0l;TR0=1;delay2(time);}//**************音乐播放函数***************//voidyy1(void){unsignedchark,i;TMOD=1;//置CT0定时工作方式1EA=1;ET0=1;//IE=0x82//CPU开中断,CT0开中断while(1){i=0;while(i100){//音乐数组长度,唱完从头再来k=sszymmh[i]+7*sszymmh[i+1]-1;timer0h=FREQH[k];timer0l=FREQL[k];time=sszymmh[i+2];i=i+3;song();}}}voidyy2(void){unsignedchark,i;TMOD=1;//置CT0定时工作方式1EA=1;ET0=1;//IE=0x82//CPU开中断,CT0开中断while(1){i=0;while(i150){//音乐数组长度,唱完从头再来k=mldsh[i]+7*mldsh[i+1]-1;timer0h=FREQH[k];timer0l=FREQL[k];time=mldsh[i+2];i=i+3;song();}}}voidyy3(void){unsignedchark,i;TMOD=1;//置CT0定时工作方式1EA=1;ET0=1;//IE=0x82//CPU开中断,CT0开中断while(1){i=0;while(i150){//音乐数组长度,唱完从头再来k=lzlh[i]+7*lzlh[i+1]-1;timer0h=FREQH[k];timer0l=FREQL[k];time=lzlh[i+2];i=i+3;song();}}}voidyy4(void){unsignedchark,i;TMOD=1;//置CT0定时工作方式1EA=1;ET0=1;//IE=0x82//CPU开中断,CT0开中断while(1){i=0;while(i150){//音乐数组长度,唱完从头再来k=srg[i]+7*srg[i+1]-1;timer0h=FREQH[k];timer0l=FREQL[k];time=srg[i+2];i=i+3;song();}}}