#单片机#矩阵键盘-电子琴
sbit sound=P0^4; //将sound位定义为P0.4
#define uchar unsigned char
#define uint unsigned int
uchar k,num,temp;
uint code yinfu[]= {64580,64684,64777,64820,64898,64968,65030 }; //中1,中2,中3,中4,中5,中6,中7
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
uchar key_scan()
{
uchar i=0;
for(i=0;i<4;i )//循环四次
{
P2=~(0x01< temp=P2; //读取P1的值 temp=0xf0&temp; //获取高四位的值 if(temp!=0xf0) //若有键按下,条件成立 { { { case 0xe0:num=0 i*4; //根据i的值,决定返回num的值是多少 break; //i=0,代表第一行;i=1,代表第二行…… case 0xd0:num=1 i*4; break; case 0xb0:num=2 i*4; break; case 0x70:num=3 i*4; break; while(temp!=0xf0) //松手检测 { temp=P2; } return num; TH0=(yinfu[key_scan()])/256;
delay(5);//延时
temp=P2;//再次读取P1的值
temp=0xf0&temp;//获取高四位值
if(temp!=0xf0)//再次判断
switch(temp) //判断高四位的值
}
temp=0xf0&temp;
}
}
}
}
void main()
{
TMOD=0x01;//设置定时器0工作方式1
TH0=(yinfu[0])/256;
TL0=(yinfu[0])%6;//赋初值
EA=1;//开总中断
ET0=1;//开定时器中断0
TR0=1;//启动定时器0
while(1)
{
}
}
void timer0() interrupt 1
{
TL0=(yinfu[key_scan()])%6;//赋初值
sound=!sound;
}
关键词:
单片机矩阵键盘电子
评论