#includeunsigned char tone;unsigned char op;sbit led=P1^1;sbit spk=P1^0;sbit key1=P3^2; //两只老虎演奏按键sbit key2=P3^3; //1~i演奏按键unsigned char code shuma[]={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};//用于使定时器初值变化以产生相应频率的定时unsigned char code yinfu_H[]={0xfb,0xfc,0xfc,0xfc,0xfd,0xfd,0xfd,0xfd,0x00}; unsigned char code yinfu_L[]={0xe9,0x5c,0xc1,0xef,0x45,0x92,0xd0,0xee,0x00}; void delay(unsigned int N){ unsigned int j=N; while(j--) { unsigned char i; for(i=0;i<250;i++); }}unsigned char code laohu[]={1,2,3,1,1,2,3,1,3,4,5,3,4,5,5,6,5,4,3,1,5,6,5,4,3,1,3,1,1,3,1,1};//两只老虎乐谱unsigned char code laohuys[]={220,220,220,220,220,220,220,220,220,220,235,220,220,210,150,150, 170,170,200,200,150,150,170,170,200,200,244,240,250,244,220,250};//定时器初始化void initTimer(void){ TMOD=0x01; TH0=yinfu_H[tone]; TL0=yinfu_L[tone];}//定时器T0中断服务程序void timer0(void) interrupt 1{ TH0=yinfu_H[tone]; TL0=yinfu_L[tone]; spk=~spk; //}void laohu_yz()//两只老虎 演奏程序{ unsigned char i; for(i=0;i<32;i++) { led=1; tone=laohu[i]-1; delay(laohuys[i]/1.2); led=0; //每个音符闪烁一次 delay(laohuys[i]/1.5); }}void yuep_yz()//1~i音符演奏程序{ unsigned char i; for(i=0;i<16;i++) { led=1; if(i<8) { tone=i; } else { tone=15-i; } delay(220); led=0; delay(180); }}void jianpan(){ op=P2; switch(op) { case 0xfe:led=0;tone=0;P0=shuma[1];TR0=1;break; case 0xfd:led=0;tone=1;P0=shuma[2];TR0=1;break; case 0xfb:led=0;tone=2;P0=shuma[3];TR0=1;break; case 0xf7:led=0;tone=3;P0=shuma[4];TR0=1;break; case 0xef:led=0;tone=4;P0=shuma[5];TR0=1;break; case 0xdf:led=0;tone=5;P0=shuma[6];TR0=1;break; case 0xbf:led=0;tone=6;P0=shuma[7];TR0=1;break; case 0x7f:led=0;tone=7;P0=shuma[8];TR0=1;break; default:break; } delay(4); TR0=0; led=1; P0=0xff;}//主程序void main(void){ initTimer(); ET0=1; EA=1; spk=0;while(1) { if(key1==0)//判断K1键是否按下 { delay(10); if(key1==0) { TR0=1; laohu_yz(); spk=0; } }else if(key2==0)//判断K2键是否按下 { delay(10); if(key2==0) { TR0=1; yuep_yz(); spk=0; } } jianpan();//键盘检测 }}
导读:目前正在解读《51单片机制作电子琴》的相关信息,《51单片机制作电子琴》是由用户自行发布的知识型内容!下面请观看由(电工技术网 - www.9ddd.net)用户发布《51单片机制作电子琴》的详细说明。
简介:本文给大家分享了一个51单片机制作电子琴代码。
提醒:《51单片机制作电子琴》最后刷新时间 2024-03-14 00:58:01,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《51单片机制作电子琴》该内容的真实性请自行鉴别。