新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > #單片機#矩陣鍵盤-電子琴

#單片機#矩陣鍵盤-電子琴

作者: 時間:2016-11-27 來源:網(wǎng)絡(luò) 收藏
#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 )//循環(huán)四次
{
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; //根據(jù)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;//設(shè)置定時器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;
}


關(guān)鍵詞: 單片機矩陣鍵盤電子

評論


技術(shù)專區(qū)

關(guān)閉