用C语言使用89C51 做一个电子琴实验,求程序和图!谢谢了

如题所述

#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char

unsigned int code tab[]=
{63628,63835,64021,64103,
64260,64400,64524,64580,
64684,64777,64820,64898,
64968,65030,65058,65110};//频率表
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0};//数码管段码显示值

sbit P1_0=P1^0;

uchar num,temp,num1,key,i,j,STH0,STL0;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}

uchar keyscan();
void display(uchar aa);
void main()
{

TMOD=0x01;//定时器0工作方式1
ET0=1;//开定时器0中断
EA=1;//开总中断
P2=0;

P0=0;

while(1)
{
keyscan();//键盘扫描子程序
}
}

uchar keyscan()
{
P3=0xfe;//第一行判断
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{

switch(temp)
{
case 0xe0:num=1;
break;
case 0xd0:num=2;
break;
case 0xb0:num=3;
break;
case 0x70:num=4;
break;
}
P1_0=~P1_0;
P0=table[num];
STH0=tab[num-1]/256;
STL0=tab[num-1]%256;
TR0=1;

while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
TR0=0;
}
}

P3=0xfd;//第二行判断
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{

switch(temp)
{
case 0xe0:num=5;
break;
case 0xd0:num=6;
break;
case 0xb0:num=7;
break;
case 0x70:num=8;
break;
}
P1_0=~P1_0;
if(num<=7)
P0=table[num];
else
P0=table[num-7];
STH0=tab[num-1]/256;
STL0=tab[num-1]%256;
TR0=1;
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
TR0=0;
}
}

P3=0xfb;//第三行判断
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{

switch(temp)
{
case 0xe0:num=9;
break;
case 0xd0:num=10;
break;
case 0xb0:num=11;
break;
case 0x70:num=12;
break;
}
P1_0=~P1_0;
P0=table[num-7];
STH0=tab[num-1]/256;
STL0=tab[num-1]%256;
TR0=1;
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
TR0=0;
}
}

P3=0xf7;//第四行判断
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{

switch(temp)
{
case 0xe0:num=13;
break;
case 0xd0:num=14;
break;
case 0xb0:num=15;
break;
case 0x70:num=16;
break;
}
P1_0=~P1_0;
if(num<=14)
P0=table[num-7];
else
P0=table[num-14];
STH0=tab[num-1]/256;
STL0=tab[num-1]%256;
TR0=1;
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
TR0=0;
}
}
return num;

}

void time0() interrupt 1
{
TH0=STH0;
TL0=STL0;
P1_0=~P1_0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-05-21
这个我之前做过,留个邮箱,我发给你
第3个回答  2011-05-21
scanf (
相似回答