电子琴.docx
《电子琴.docx》由会员分享,可在线阅读,更多相关《电子琴.docx(18页珍藏版)》请在冰豆网上搜索。
电子琴
#include
#ifndef__led1602_h
#define__led1602_h
#defineucharunsignedchar
#defineuintunsignedint
//定义接口
#defineLCD_datP1
sbitRS=P3^3;
sbitRW=P3^2;
sbitE=P3^1;
voidLcdWriteCommand(ucharCMD,ucharAttribC);
voiddelay(ucharj)
{
ucharn;
for(j;j>0;j--)
{for(n=248;n>0;n--){}//delay500us
}
}
voiddelay5ms()
{
unsignedinti;
for(i=0;i<1000;i++)
;
}
delay1()//按键防止连续显示
{
inti,j,k;
for(i=0;i<20;i++)
for(j=0;j<20;j++)
for(k=0;k<100;k++);
}
voiddelay_nms(unsignedintn)
{
unsignedinti=0,j=0;
for(i=n;i>0;i--)
for(j=0;j<1140;j++);
}
//发送命令
voidwritecommand(charcommand)
{
RS=0;//选择命令寄存器
RW=0;//选择写
E=1;//读写禁止
delay
(2);
P1=command;
delay5ms();
E=0;//读写允许
}
//发送数据
voidwritedata(chardat)
{
RS=1;//择数据寄存器
RW=0;//选择写
E=1;//允许读写操作
delay
(2);
P1=dat;
delay5ms();
E=0;//不允许读写操作
}
//设置显示的坐标成功
voidsetXY(charx,chary)//发送一个地址
{
charaddress;
if(y==0)
address=0x80+x;//选择第0行
if(y==1)
address=0xc0+x;//选择第1行
writecommand(address);
}
//显示一个数据成功
voidwritechar(charx,chary,chardat)
{
setXY(x,y);///在某一个地址写
writedata(dat);//写一个数
}
//显示一个字符串已成功
voidwritestring(charx,chary,char*string)//用指针写一个字符串
{
setXY(x,y);
while(*string)
{
P1=*string;
writedata(*string);
string++;
}
}
//初始化
voidinit(void)
{
delay(20);
writecommand(0x38);//8位数据,2行显示
writecommand(0x08);//关闭显示
writecommand(0x01);//清屏清屏和光标归位需要较长的时间
delay5ms();
writecommand(0x06);//光标移动设置既光标右移一格地址计数器AC加1
writecommand(0x0f);//设置光标闪0x0c是设置光标不闪0x0f为光标闪
}
voidLocateXY(charposx,charposy)
{
uchartemp;
temp=posx&0xf;
posy&=0x1;
if(posy)temp|=0x40;
temp|=0x80;
LcdWriteCommand(temp,0);
}
#endif
unsignedcharcodetable[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
unsignedchartemp,flagplay;
unsignedcharkey=0,clr=0;
unsignedchari,j,t=0;
unsignedcharSTH0,replay=0;
unsignedcharSTL0;
unsignedintcodetab[]={0,0,65030,64968,
64898,64820,64777,64684,
64580,64524,64400,64260,
64103,64021,63835,63628};
unsignedinttabekey[17];////键记忆
unsignedinttabetimer[17];///时间记忆
unsignedintstoptimer[17];
unsignedinttcont,toper=0,keycont=0;
////////////////////////////////////////
voidinitTimer(void)
{
TMOD=0x10;
TH0=0xfc;
TL0=0x18;
}
/////////////////////////////////////////
voidmain(void)
{
init();
delay(200);
writestring(0,0,"dianziqin");
initTimer();
ET0=1;
ET1=1;
TR0=0;
EA=1;
while
(1)
{
//////////////////////////////////////////////////////////
P2=0xff;
P2_4=0;
temp=P2;
temp=temp&0x0f;
if(temp!
=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P2;
temp=temp&0x0f;
if(temp!
=0x0f)
{
temp=P2;
temp=temp&0x0f;
switch(temp)
{
case0x0e:
key=0;
writestring(5,1,"clr");
break;
case0x0d:
key=1;
break;
case0x0b:
key=2;
writestring(1,1,"sizhong");
break;
case0x07:
key=3;
writestring(1,1,"lazhong");
break;
}
temp=P2;
if(key!
=1)
{
P0=table[key];
STH0=tab[key]/256;
STL0=tab[key]%256;
TR1=1;
tabetimer[keycont]=tcont;///开始按键时间
tabekey[keycont]=tab[key];
}
temp=temp&0x0f;
while(temp!
=0x0f)
{
temp=P2;
temp=temp&0x0f;
}
TR1=0;//////////按键放开音乐停止
if(key==0)
{
TR1=0;
tcont=0;
flagplay=0;
writestring(5,1,"");
for(clr=0;clr<15;clr++)
{
tabetimer[clr]=0;tabekey[clr]=0;///清除所存的曲子
keycont=0;
}
}
////////////////////////////
if(flagplay==0&&key!
=1&&key!
=0)
{
stoptimer[keycont]=tcont;//结束按键时间
writestring(1,1,"");
keycont++;
}
if(key==1&&flagplay==1)////////////开始播放
{
writestring(1,1,"play");
TR0=1;
//replay=~replay;
}
if(key==1&&flagplay!
=1)///////////////////停止记录按键
{
TR0=0;
TR1=0;
t=0;
//keycont=0;
tcont=0;
flagplay=1;///用定时器播放音乐
writestring(1,1,"stop");
}
}
}
/////////////////////////////////////////////////////////
P2=0xff;
P2_5=0;
temp=P2;
temp=temp&0x0f;
if(temp!
=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P2;
temp=temp&0x0f;
if(temp!
=0x0f)
{
temp=P2;
temp=temp&0x0f;
switch(temp)
{
case0x0e:
key=4;
writestring(1,1,"sozhong");
break;
case0x0d:
key=5;
writestring(1,1,"fazhong");
break;
case0x0b:
key=6;
writestring(1,1,"mezhong");
break;
case0x07:
key=7;
writestring(1,1,"rezhong");
break;
}
temp=P2;
P0=table[key];
STH0=tab[key]/256;
STL0=tab[key]%256;
tabetimer[keycont]=tcont;///开始按键时间
TR1=1;
temp=temp&0x0f;
while(temp!
=0x0f)
{
temp=P2;
temp=temp&0x0f;
}
TR1=0;writestring(1,1,"");
tabekey[keycont]=tab[key];
stoptimer[keycont]=tcont;//结束按键时间
keycont++;
}
}
///////////////////////////////////////////////////////
P2=0xff;
P2_6=0;
temp=P2;
temp=temp&0x0f;
if(temp!
=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P2;
temp=temp&0x0f;
if(temp!
=0x0f)
{
temp=P2;
temp=temp&0x0f;
switch(temp)
{
case0x0e:
key=8;
writestring(1,1,"dozhong");
break;
case0x0d:
key=9;
writestring(1,1,"sidi");
break;
case0x0b:
key=10;
writestring(1,1,"ladi");
break;
case0x07:
key=11;
writestring(1,1,"sodi");
break;
}
temp=P2;
P0=table[key];
tabekey[keycont]=tab[key];
STH0=tab[key]/256;
STL0=tab[key]%256;
TR1=1;
tabetimer[keycont]=tcont;///开始按键时间
temp=temp&0x0f;
while(temp!
=0x0f)
{
temp=P2;
temp=temp&0x0f;
}
TR1=0;writestring(1,1,"");
stoptimer[keycont]=tcont;//结束按键时间
keycont++;
}
}
P2=0xff;
P2_7=0;
temp=P2;
temp=temp&0x0f;
if(temp!
=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P2;
temp=temp&0x0f;
if(temp!
=0x0f)
{
temp=P2;
temp=temp&0x0f;
switch(temp)
{
case0x0e:
key=12;
writestring(1,1,"fadi");
break;
case0x0d:
key=13;
writestring(1,1,"medi");
break;
case0x0b:
key=14;
writestring(1,1,"redi");
break;
case0x07:
key=15;
writestring(1,1,"dodi");
break;
}
temp=P2;
P1_0=~P1_0;
P0=table[key];
tabekey[keycont]=tab[key];
STH0=tab[key]/256;
STL0=tab[key]%256;
tabetimer[keycont]=tcont;///开始按键时间
TR1=1;
temp=temp&0x0f;
while(temp!
=0x0f)
{
temp=P2;
temp=temp&0x0f;
}
TR1=0;writestring(1,1,"");
stoptimer[keycont]=tcont;//结束按键时间
keycont++;
}
}
}
}
//////////////////////////////////////////////////
voidtimer0(void)interrupt1
{
TH0=0xfc;
TL0=0x18;
toper++;
if(toper==5)
{
toper=0;P3_7=~P3_7;
tcont++;
if(tcont==65535)
{
writestring(1,1,"");
}
}
if(flagplay==1)
{
if(tcont==tabetimer[t])//用来判断播放时间是否到了前面的按键时间
//for(t=0;t<=keycont;t++)
{
STH0=tabekey[t]/256;
STL0=tabekey[t]%256;
TR1=1;
//delay(255);
///TR1=0;
}
if(tcont==stoptimer[t])
{
t++;TR1=0;///第t个按键时间到
if(t==keycont)
{
t=0;////////播放完毕等待下次按键播放
tcont=0;TR0=0;
}
}
}
}
/////////////////////////////////////////////////
voidtimer1(void)interrupt3
{
TH1=STH0;
TL1=STL0;
P3_4=~P3_4;
TR0=1;///当有键按下的时候定时器一开始记按键时间
}