at89c51单片机编程,遇到个问题,望指教
这是一个AT89C51单片机电子琴的c语言代码,要播放存入的歌曲和1-7七个音阶,同时8段数码管在按下音阶时对应的简谱数字要亮起来。但是编译成功之后,数码管和键盘都没有工作,即数码管不亮同时按下键盘也没有声音(键盘为4x4键盘)程序代码:
#include #define uint unsigned int #define uchar unsigned char void Init(void);//初始化 void Key_Scan(void);//键值扫描 void Delayms(uint z);//延时 void LED_Display(void);//数码显示 void Sound_Player(void);//音频处理 uint Keycode;//键值 uint Time = 0; uint Flag = 0; uint TH0code,TL0code; sbit Speaker = P3^0; //扬声器控制引脚 uint Num = 0; uchar code table[]={ //数码管共阳码表 0xc0,0xF9,0xA4,0xB0, 0X99,0X92,0X82,0XF8, 0x80,0x90,0x88,0x83, 0xC6,0xA1,0x86,0x8E}; uchar code Music1_Tab[]={ 0xc0,0xf9,0xa4,0xb0, 0x99,0x92,0x82,0xf8, 0x80,0x90,0x88,0x83, 0xc6,0xa1,0x86,0x8e}; //曲谱1码表 uint code Music2_Tab[]={ //曲谱2码表 64021,64103,64260,64400, 64524,64580,64684,64777, 64820,64898,64968,65030, 65058,65110,65157,65178}; uchar code Music3_Tab[]={ //曲谱3码表 0x64,0x42,0x62,0x98,0x74,0x92,0x72,0x68, 0x64,0x22,0x32,0x44,0x32,0x22,0x3c,0x64, 0x42,0x62,0x94,0x04,0x82,0x74,0x94,0x68, 0x64,0x32,0x42,0x54,0x04,0x12,0x2c,0x74, 0x94,0x98,0x84,0x72,0x82,0x98,0x72,0x82, 0x92,0x72,0x72,0x62,0x42,0x22,0x3c,0x64, 0x42,0x62,0x94,0x04,0x82,0x74,0x94,0x68, 0x64,0x32,0x42,0x54,0x04,0x12,0x2c,0x00}; /**************************************** 主函数 ****************************************/ void main() { Init(); while(1) { Sound_Player();//音频处理函数 LED_Display();//数码管显示键值 } } /**************************************** 初始化函数 ****************************************/ void Init(void) { P0 = 0xff;//先将P0口置为1 Speaker = 0; IT0 = 1;//外部中断0边沿触发 EX0 = 1;//外部中断0允许 TMOD = 0x01;//定时器方式1 ET0 = 1;//定时器0中断允许 TR0 = 0;//关定时器0 EA = 1;//开总中断 } /**************************************** 音频处理函数 ****************************************/ void Sound_Player(void) { if(Flag == 0) //Do~Si七个音调键 { Flag = 1; switch(Keycode) { case 1: //Do { TH0code = 0XFB; TL0code = 0XE9; TH0 = TH0code; //置初值 TL0 = TL0code; TR0 = 1; //开定时器0 Speaker = ~Speaker; break; } case 2: //Re { TH0code = 0XFC; TL0code = 0X5C; TH0 = TH0code; //置初值 TL0 = TL0code; TR0 = 1; //开定时器0 Speaker = ~Speaker; break; } case 3: //Mi { TH0code = 0XFC; TL0code = 0X5C; TH0 = TH0code; //置初值 TL0 = TL0code; TR0 = 1; //开定时器0 Speaker = ~Speaker; break; } case 4: //Fa { TH0code = 0XFC; TL0code = 0X5C; TH0 = TH0code; //置初值 TL0 = TL0code; TR0 = 1; //开定时器0 Speaker = ~Speaker; break; } case 5: //So { TH0code = 0XFC; TL0code = 0X5C; TH0 = TH0code; //置初值 TL0 = TL0code; TR0 = 1; //开定时器0 Speaker = ~Speaker; break; } case 6: //La 基于51单片机的电子琴设计 { TH0code = 0XFC; TL0code = 0X5C; TH0 = TH0code; //置初值 TL0 = TL0code; TR0 = 1; //开定时器0 Speaker = ~Speaker; break; } case 7: //Si { TH0code = 0XFC; TL0code = 0X5C; TH0 = TH0code; //置初值 TL0 = TL0code; TR0 = 1; //开定时器0 Speaker = ~Speaker; break; } default:break; } } else if(Keycode ==8) //特殊功能键 { switch(Time) { case 1: //播放第一首乐曲 { TH0code = Music1_Tab[Num++]; TL0code = Music1_Tab[Num++]; TH0 = TH0code; //置初值 TL0 = TL0code; TR0 = 1; //开定时器0 if(Num == 15) Num = 0; Speaker = ~Speaker; break; } case 2: //播放第二首乐曲 { TH0code = Music2_Tab[Num]/256; TL0code = Music2_Tab[Num]%256; TH0 = TH0code; //置初值 TL0 = TL0code; TR0 = 1; //开定时器0 基于51单片机的电子琴设计 Num++; if(Num == 15) Num = 0; Speaker = ~Speaker; break; } case 3: //播放第三首乐曲 { TH0code = Music3_Tab[Num++]; TL0code = Music3_Tab[Num++]; TH0 = TH0code; //置初值 TL0 = TL0code; TR0 = 1; //开定时器0 if(Num == 64) Num = 0; Speaker = ~Speaker; break; } default: break; } } } /**************************************** 数码管显示函数 ***************************************/ void LED_Display(void) { if(Keycode>0 && Keycode<8) P0 = ~table[Keycode];//Do~Si七个音调键分别显示1~7 else if(Keycode == 8) P0 = ~table[Time + 9];//特殊功能键分别显示A、b、C TR0 = 0; //关定时器0 } /**************************************** 键值扫描函数 ****************************************/ void Key_Scan(void) { uint key; key = P1; switch(key) //键值 { case 0xfe: //Do { Delayms(10); //消抖 基于51单片机的电子琴设计 if(key == 0xfe) { Keycode = 1; Flag = 0; while(P1 != 0xFF); //等待按键释放 break; } } case 0xfd: //Re { Delayms(10); if(key == 0xfd) { Keycode = 2; Flag = 0; while(P1 != 0xFF); //等待按键释放 break; } } case 0xfb: //Mi { Delayms(10); if(key == 0xfb) { Keycode = 3; Flag = 0; while(P1 != 0xFF); //等待按键释放 break; } } case 0xf7: //Fa { Delayms(10); if(key == 0xf7) { Keycode = 4; Flag = 0; while(P1 != 0xFF); //等待按键释放 break; } } case 0xef: //So { Delayms(10); if(key == 0xef) { Keycode = 5; Flag = 0; while(P1 != 0xFF); //等待按键释放 break; } } case 0xdf: //La { Delayms(10); if(key == 0xdf) { Keycode = 6; Flag = 0; while(P1 != 0xFF); //等待按键释放 break; } } case 0xbf: //Si { Delayms(10); if(key == 0xbf) { Keycode = 7; Flag = 0; while(P1 != 0xFF); //等待按键释放 break; } } case 0x7f: // 播放/下一首 { Delayms(10); if(key == 0x7f) { Keycode = 8; Flag = 1; Num = 0; if(Time == 4)Time=0; Time++; while(P1 != 0xFF); //等待按键释放 break; } } default : break; } } void Delayms(uint z)//延时ms { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } /**************************************** 键盘中断函数 ****************************************/ void Key_inter() interrupt 0 { Key_Scan();//键值扫描 } /**************************************** 定时中断函数 ****************************************/ void T0_inter() interrupt 1 { TH0 = TH0code; TL0 = TL0code; Speaker = ~Speaker; }