请教一下51单片机矩阵键盘+蜂鸣器做电子琴的问题
为什么我的代码不行,运行之后只能按一个键响,按其他键完全没反应啊,是不是那个中断的问题,我想了好久了。。。程序代码:
#include<stc15.h> #include<intrins.h> #include<gpio.h> #define uchar unsigned char #define uint unsigned int #define KEY P2 //矩阵键盘接口 sbit beep=P1^0; //蜂鸣器 uchar key_volume=0; //键值 uint code tone[14]={1276,1136,1012,956,852,759,716,638,568,506,57,51,45,43}; //音调分别为So(低)、La(低)、Si(低)、Do(中)、Re(中)、Mi(中)、Fa(中)、So(中)、La(中)、Si(中)、Do(高)、Re(高)、Mi(高)、Fa(高) void Delay1ms() //@11.0592MHz { unsigned char i, j; _nop_(); _nop_(); _nop_(); i = 11; j = 190; do { while (--j); } while (--i); } void Delayxms(uint t) { uint x; for(x=0;x<t;x++) { Delay1ms(); } } void Time0_init() //定时器初始化 { TMOD=0x01; //定时器0,方式1 EA=1; ET0=1; TR0=1; } uchar keyscan() //键盘扫描 { uchar row,colume; //行、列变量 KEY=0x0f; if(KEY!=0x0f) { Delayxms(10); if(KEY!=0x0f) { KEY=0xef; if(KEY!=0xef) { row=0; goto colume_scan; } KEY=0xdf; if(KEY!=0xdf) { row=1; goto colume_scan; } KEY=0xbf; if(KEY!=0xbf) { row=2; goto colume_scan; } KEY=0x7f; if(KEY!=0x7f) { row=3; goto colume_scan; } return(16); colume_scan: if((KEY&0x01)==0) colume=0; else if((KEY&0x02)==0) colume=1; else if((KEY&0x04)==0) colume=2; else colume=3; key_volume=row*4+colume; while((KEY&0x0f)!=0x0f); } } else KEY=0xff; return(16); } void value_ISR() interrupt 1 { TF0=0; //keyscan(); if(KEY==0xed) { TH0=(65536-tone[0])/256; //填充初值 TL0=(65536-tone[0])%256; TR0=1; beep=~beep; //发出不同音调 while(KEY!=0xed); //判断键释放 } else if(KEY==0xeb) { TH0=(65536-tone[1])/256; TL0=(65536-tone[1])%256; TR0=1; beep=~beep; while(KEY!=0xeb); } else if(KEY==0xe7) { TH0=(65536-tone[2])/256; TL0=(65536-tone[2])%256; TR0=1; beep=~beep; while(KEY!=0xe7);//判断键释放 } else if(KEY==0xde) { TH0=(65536-tone[3])/256; TL0=(65536-tone[3])%256; TR0=1; beep=~beep; while(KEY!=0xde); } else if(KEY==0xdd) { TH0=(65536-tone[4])/256; TL0=(65536-tone[4])%256; TR0=1; beep=~beep; while(KEY!=0xdd); //判断键释放 } else if(KEY==0xdb) { TH0=(65536-tone[5])/256; TL0=(65536-tone[5])%256; TR0=1; beep=~beep; while(KEY!=0xdb); } else if(KEY==0xd7) { TH0=(65536-tone[6])/256; TL0=(65536-tone[6])%256; TR0=1; beep=~beep; while(KEY!=0xd7); //判断键释放 } else if(KEY==0xbe) { TH0=(65536-tone[7])/256; TL0=(65536-tone[7])%256; TR0=1; beep=~beep; while(KEY!=0xbe); } else if(KEY==0xbd) { TH0=(65536-tone[8])/256; TL0=(65536-tone[8])%256; TR0=1; beep=~beep; while(KEY!=0xbd); //判断键释放 } else if(KEY==0xbb) { TH0=(65536-tone[9])/256; TL0=(65536-tone[9])%256; TR0=1; beep=~beep; while(KEY!=0xbb); } else if(KEY==0xb7) { TH0=(65536-tone[10])/256; TL0=(65536-tone[10])%256; TR0=1; beep=~beep; while(KEY!=0xb7); //判断键释放 } else if(KEY==0x7e) { TH0=(65536-tone[11])/256; TL0=(65536-tone[11])%256; TR0=1; beep=~beep; while(KEY!=0x7e); } else if(KEY==0x7d) { TH0=(65536-tone[12])/256; TL0=(65536-tone[12])%256; TR0=1; beep=~beep; while(KEY!=0x7d); //判断键释放 } else if(KEY==0x7b) { TH0=(65536-tone[13])/256; TL0=(65536-tone[13])%256; TR0=1; beep=~beep; while(KEY!=0x7b); } } void main() { gpio(); //端口初始化 Time0_init(); while(1) { key_volume=0; //键值清零 keyscan(); if(key_volume!=0 && key_volume!=15) { IE1=1; //进入外部中断1 外部中断1可以这样触发吗 } } }