C51单片机电子琴程序

发布时间:2021-10-16 18:19:46

#include<reg51.h> sbit beer=P3^3; int num,count; int code lab[]={61719,62435,62506,62679,62985,63263,63512, 63628,63835,64021,64103,64260,64400,64524, 64580,64684,64777,64820,64898,64968,65030}; int code Song[]={ 0x12,50,0x12,50,0x12,100, 0x12,50,0x12,50,0x12,100, 0x10,50,0x12,50,0x13,15,0x12,25,0x10,25, 0x0f,75,0x10,75,0x12,50, 0x12,75,0x10,75,0x12,75,0x10,37,0x0f,37, 0x0e,75,0x10,75,0x0f,150, 0x10,100,0x10,100,0x0f,200, 0x0c,100,0x0e,100,0x0f,300,0x00,0x00 }; void init() //计数器终端初始化 { num=0; TMOD=0x11; TH0=0xff; TL0=0xff; TH1=0xD8; //装初值 TL1=0xEF; EA=1; ET0=1; ET1=1; } void delay(int z) //延时子程序 { int x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void inter() interrupt 1 //计数器 1 { TH0=lab[num]/256; TL0=lab[num]%256; beer=!beer; } void timer1() interrupt 3 //计数器 2 { TH1=0xD8; //装初值

TL1=0xEF; count++; } void Play_Song() //放歌子程序 { unsigned char Temp2; unsigned int Addr; count = 0; while(1) { num = Song[Addr]; Addr++; TH0=lab[num]/256; TL0=lab[num]%256; if ( num == 0xFF ) //休止符 { Temp2 = Song[Addr++]; TR1 = 0; delay(Temp2); } else if ( num == 0x00 ) //歌曲结束符 { return; } else { Temp2 = Song[Addr++]; TR1 = 1; delay(1.1*Temp2); } } } void sound(unsigned char a) //按键发声子程序 { switch(a) { case 0xfe: num=14; break; case 0xfd: num=15; break; case 0xfb: num=16; break; case 0xf7: num=17; break; case 0xef: num=18; break; case 0xdf: num=19; break; case 0xbf: num=20; break; case 0x7f: num=7; break;

} if(num==7) Play_Song(); TH0=lab[num]/256; TL0=lab[num]%256; while(P2!=0xff) {} } void main() //主程序 { init(); P2=0xff; while(1) { if(P2!=0xff) //第一次判断是否有按键按下 { delay(5); //消抖 if(P2!=0xff)//确认是否有按键按下 { TR0=1; sound(P2); } TR0=0; } } }


相关文档

  • 简易电子琴单片机c51程序
  • c51单片机课程设计——电子琴
  • 电子琴单片机程序
  • 单片机电子琴程序
  • 单片机的电子琴汇编程序
  • 单片机 C51电子琴
  • 基于c51单片机的电子琴设计
  • 基于51单片机的电子琴程序
  • 基于单片机电子琴程序设计
  • 单片机做的电子琴音乐程序中
  • 猜你喜欢

    电脑版