0
/*******************************************************************
名称:简易电子琴
作者:郭 强
All rights reserved. 2012
*******************************************************************/
#include
#define uchar unsigned char
***it P36=P3^6;
***it P27=P2^7;
uchar key;
/*************各个数组准备********************/
uchar code dis_code[]={
0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8, // 1~7
0x79,0x24,0x30,0x19,0x12,0x02,0x78, //1. ~ 7.
0x86,0x8e,0xbf};
uchar code key_code[]={
0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,
0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77}; //矩阵键盘编码
uchar code mh_code[]={ 0xFC,0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF}; //TH0 预装值 1~7~1.~7.
uchar code ml_code[]={ 0x43,0xAB,0x08,0x33,0x81,0xC7,0x05,0x20,0x55,0x84,0x99,0xC0,0xE3,0x02}; //TL0 预装值 1~7~1.~7.
/***************延时程序 n* 2ms*****************/
void delay(uchar n)
{
uchar j;
while(n--)
{
for(j=0;j<115;j++);
}
}
/**************按键搜索程序*******************/
uchar scan()
{
uchar s1,s2,keycode,k;
P1=0xf0;
s1=P1;
if((s1&0xf0)!=0xf0)
{
delay(10);
s1=P1;
if((s1&0xf0)!=0xf0)
{
P1=0x0f;
s2=P1;
keycode=s1|s2;
}
for(k=0;k<16;k++)
{
if(keycode==key_code[k])
{
key=k;
return(key); //返回 按键值
}
}
}
else
P1=0xff;
return(16); //若没搜索到 ,返回 “-”
}
/**************主函数*******************/
void main()
{
P1=0xf0;
P27=0;
P0=dis_code[16]; //数码管显示音符
TMOD=0x01;
EA=1;
ET0=1; //中断需要的设置
while(1)
{
if((P1&0xf0)!=0xf0)
{
scan();
P0=dis_code[key];
TH0=mh_code[key];
TL0=ml_code[key];
TR0=1;
if(P1==0xf0) //
TR0=0; //这两条语句能够使电子琴 按下发声,松开停止
}
}
}
/**************中断函数*******************/
void ding() interrupt 1
{
TH0=mh_code[key];
TL0=ml_code[key];
P36=~P36;
}
个人能力有限,喜欢拿走,不喜勿喷,谢谢!
|
|