资源描述:
《51单片机电子琴设计C语言设计实例》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#defineucharunsignedchar//定义一下方便使用#defineuintunsignedint#defineulongunsignedlong#include//包括一个52标准内核的头文件//charcodedx516[3]_at_0x003b;//这是为了仿真设置的sbitBEEP=P1^7;//喇叭输出脚//sbitK1=P3^2;sbitK2=P3^5;sbitK3=P2^4;sbitK4=P2^5;ucharth0_f;//在中断中装载的T0的值高8位uchartl0_f;//在中断中装载的T0的值低8位//T0的值,及输出频率对照表ucharco
2、defreq[36*2]={0xA9,0xEF,//00220HZ,1//00x93,0xF0,//00233HZ,1#0x73,0xF1,//00247HZ,20x49,0xF2,//00262HZ,2#0x07,0xF3,//00277HZ,30xC8,0xF3,//00294HZ,40x73,0xF4,//00311HZ,4#0x1E,0xF5,//00330HZ,50xB6,0xF5,//00349HZ,5#0x4C,0xF6,//00370HZ,60xD7,0xF6,//00392HZ,6#0x5A,0xF7,//00415HZ,70xD8,0xF7,//00440HZ1//120x
3、4D,0xF8,//00466HZ1#//130xBD,0xF8,//00494HZ2//140x24,0xF9,//00523HZ2#//150x87,0xF9,//00554HZ3//160xE4,0xF9,//00587HZ4//170x3D,0xFA,//00622HZ4#//180x90,0xFA,//00659HZ5//190xDE,0xFA,//00698HZ5#//200x29,0xFB,//00740HZ6//210x6F,0xFB,//00784HZ6#//220xB1,0xFB,//00831HZ7//230xEF,0xFB,//00880HZ`10x2A,0xFC,//
4、00932HZ`1#0x62,0xFC,//00988HZ`20x95,0xFC,//01046HZ`2#0xC7,0xFC,//01109HZ`30xF6,0xFC,//01175HZ`40x22,0xFD,//01244HZ`4#0x4B,0xFD,//01318HZ`50x73,0xFD,//01397HZ`5#0x98,0xFD,//01480HZ`60xBB,0xFD,//01568HZ`6#0xDC,0xFD,//01661HZ`7//35};//定时中断0,用于产生唱歌频率timer0()interrupt1{TL0=tl0_f;TH0=th0_f;//调入预定时值BEEP=~B
5、EEP;//取反音乐输出IO}//按键控制音阶声音输出(电子琴)voidmain(void)//主程序{ulongn;ucharcodejie8[8]={12,14,16,17,19,21,23,24};//1234567`1八个音符在频率表中的位置TMOD=0x01;//使用定时器0的16位工作模式TR0=0;ET0=1;EA=1;while(1){if(!K1){tl0_f=freq[jie8[0]*2];//置一个音符的值th0_f=freq[jie8[0]*2+1];TR0=1;for(n=0;n<10000;n++);//延时}if(!K2){tl0_f=freq[jie8[1]*2
6、];//置一个音符的值th0_f=freq[jie8[1]*2+1];TR0=1;for(n=0;n<10000;n++);//延时}if(!K3){tl0_f=freq[jie8[2]*2];//置一个音符的值th0_f=freq[jie8[2]*2+1];TR0=1;for(n=0;n<10000;n++);//延时}if(!K4){tl0_f=freq[jie8[3]*2];//置一个音符的值th0_f=freq[jie8[3]*2+1];TR0=1;for(n=0;n<10000;n++);//延时}TR0=0;}}/*******************************新的好
7、用的电子琴*************************/#include#include#include#defineucharunsignedchar//#defineucharunsignedchar//定义一下方便使用#defineuintunsignedint#defineulongunsignedlongsbitBEEP=