您的位置 首页 解答

#单片机#矩阵键盘-电子琴

includereg52h>包含51单片机寄存器定义的头文件sbitsound=P0^4;将sound位定义为P04defineucharunsignedchardefine

#include //包括51单片机寄存器界说的头文件

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) //若有键按下,条件建立
{
delay(5);//延时
temp=P2;//再次读取P1的值
temp=0xf0&temp;//获取高四位值
if(temp!=0xf0)//再次判别
{
switch(temp) //判别高四位的值
{
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;
temp=0xf0&temp;
}
}
}
}
return num;
}
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
{
TH0=(yinfu[key_scan()])/256;
TL0=(yinfu[key_scan()])%6;//赋初值
sound=!sound;
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/changshang/jieda/256867.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部