#include "public.h"
#include "key.h"
#include "songdata.h"
#include "reg52.h"
#define Clk 0x070000
#define SMG_A_DP_PORT	P0	//使用宏定义数码管段码口
#define SMG_B_DP_PORT	P0	//使用宏定义数码管段码口

unsigned char data val_H; //计数器高字节
unsigned char data val_L; //计数器低字节

//管脚定义
sbit BEEP=P2^5;



//共阴极数码管显示0~F的段码数据
u8 gsmg_code[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
				0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

// 音符频率数组
u16 tone[] = {262, 294, 330, 349, 392, 440, 494, 523, 587, 659, 698, 784}; // 音调数组

u8 music[32]; // 保存音乐的数组
u8 music_index = 0; // 音乐数组的索引

bit song_playing = 0; // 添加歌曲播放标志

void t0_isr() interrupt 1 //定时器 0 中断处理程序
{
    if (song_playing) { // 当歌曲播放标志为真时，产生方波
        BEEP = ~BEEP;
    } else {
        BEEP = 1; // 不产生方波，关闭蜂鸣器
    }
    TH0 = val_H; //重新装入计数值
    TL0 = val_L;
}

void save_music(u8 key_value)
{
    int m=0;
    if (music_index < 32)
    {
        music[music_index] = tone[key_value];
		music_index++;
    }
	else if(music_index>=32)
	{
	  music_index = 0;
	 
        for( m = 0; m < 32; m++)
		 {
            music[m] = 0;
        }
	}
}

void Delay(unsigned char cnt) //单位延时
{
  unsigned char i;
   unsigned int j;
   for(i=0; i<cnt; i++)
{
    for(j=0; j<0x3600; j++);
}

}

void main()
{
    
	u8 key1 = 0;
	u8 key = 0;
	u8 key2 = 0;
	u8 loop_flag=0;
	unsigned char i = 0;
    unsigned int val;
    TMOD = 0x01; //初始化
    IE = 0x82;
    TR0 = 1;
    while (1)
    {	
	    if (loop_flag) {
        loop_flag = 0;
        continue;
        }
		SMG_B_DP_PORT=gsmg_code[0];   
		key1=key_matrix_ranks_scan();
	    if(key1==13)
		{
		SMG_A_DP_PORT=gsmg_code[1];
		while(!loop_flag)
		{
        key = key_scan(0); //检测按键
        if (key == KEY1_PRESS) //K1按下，放第一首歌
        {
            i=0;
			song_playing = 1;
            while (freq_list1[i]) // 频率为 0 重新开始
            {
                val = Clk / (freq_list1[i]);
                val = 0xFFFF - val; // 计算计数值
                val_H = (val >> 8) & 0xff;
                val_L = val & 0xff;
                TH0 = val_H;
                TL0 = val_L;
                Delay(time_list1[i]);
                i++;
                 if ((key_scan(0) != KEY_UNPRESS)||(key_matrix_ranks_scan()==16))
				  {
         // 停止播放并退出循环
		  loop_flag = 1;
		  song_playing = 0;
		  break;
                  }
            }
        }
        else if (key == KEY2_PRESS) //K2按下放第二首歌
        {
		  
			 i=0;
			song_playing = 1;
            while (freq_list2[i]) // 频率为 0 重新开始
            {
                val = Clk / (freq_list2[i]);
                val = 0xFFFF - val; // 计算计数值
                val_H = (val >> 8) & 0xff;
                val_L = val & 0xff;
                TH0 = val_H;
                TL0 = val_L;
                Delay(time_list2[i]);
                i++;
                 if ((key_scan(0) != KEY_UNPRESS)||(key_matrix_ranks_scan()==16))
				  {
        // 停止播放并退出循环
		  loop_flag = 1;
		  song_playing = 0;
		  break;
                  }
            }
        }
        else if (key == KEY3_PRESS) //K3按下放第三首歌
        {
           i=0;
			song_playing = 1;
            while (freq_list3[i]) // 频率为 0 重新开始
            {
                val = Clk / (freq_list3[i]);
                val = 0xFFFF - val; // 计算计数值
                val_H = (val >> 8) & 0xff;
                val_L = val & 0xff;
                TH0 = val_H;
                TL0 = val_L;
                Delay(time_list3[i]);
                i++;
                 if ((key_scan(0) != KEY_UNPRESS)||(key_matrix_ranks_scan()==16))
				  {
                      // 停止播放并退出循环
		           loop_flag = 1;
				   song_playing = 0;
		           break;
                  }
            }
        }
		else if (key == KEY4_PRESS) //K4按下放第四首歌
        {
             i=0;
			song_playing = 1;
            while (freq_list4[i]) // 频率为 0 重新开始
            {
                val = Clk / (freq_list4[i]);
                val = 0xFFFF - val; // 计算计数值
                val_H = (val >> 8) & 0xff;
                val_L = val & 0xff;
                TH0 = val_H;
                TL0 = val_L;
                Delay(time_list4[i]);
                i++;
                 if ((key_scan(0) != KEY_UNPRESS)||(key_matrix_ranks_scan()==16))
				  {
				  //停止播放并退出循环
		  loop_flag = 1;
		  song_playing = 0;
		  break;
                  }
            }
        }
     }
  }
	 //演奏功能
	 else if(key1==14)
	 {
	 SMG_A_DP_PORT=gsmg_code[2];
	  while (!loop_flag) 
	 {
        key2 = key_matrix_ranks_scan();
        if (key2!=0)
		 {
		        song_playing = 1;
                val = Clk / (tone[key2-1]);
                val = 0xFFFF - val; // 计算计数值
                val_H = (val >> 8) & 0xff;
                val_L = val & 0xff;
                TH0 = val_H;
                TL0 = val_L;
                Delay(3);
				song_playing = 0;
				save_music(key2-1);
		   } 
	    else if(key_scan(0) != KEY_UNPRESS) 
		{
		song_playing = 0;
		loop_flag=1;
		break;
		}
     }       
	}
	 //重复播放功能
	 else if(key1==15)
	 {  if(music_index==0) 
	    {
		  SMG_A_DP_PORT=gsmg_code[14];
		  Delay(10);
		  break;
	     }
		 else
		 {
		 SMG_A_DP_PORT=gsmg_code[3];
	     while(!loop_flag)
		 {
            
			song_playing = 1;
            for (i=0;i<music_index;i++) 
            {
                val = Clk / (music[i]);
                val = 0xFFFF - val; // 计算计数值
                val_H = (val >> 8) & 0xff;
                val_L = val & 0xff;
                TH0 = val_H;
                TL0 = val_L;
                Delay(3);
             if ((key_scan(0) != KEY_UNPRESS)||(key_matrix_ranks_scan()==16)) 
			 {
               song_playing = 0;
			   loop_flag=1;
                break;
               }
			  }
             }
      	 }
	   }
   }
  }
通过串口或USB口实现上/下位机通信，能够显示下位机当前(3采样数据(动态曲线显示)，保存数据和调阅老数据。
该怎么修改呢？