#include "public.h"
#include "key.h"
#include "beep.h"
#include "songdata.h"
#define Clk 0x070000
unsigned char data val_H; //计数器高字节
unsigned char data val_L; //计数器低字节
sbit P00 = P2^5; //扬声器控制引脚
bit song_playing = 0; // 添加歌曲播放标志

void t0_isr() interrupt 1 //定时器 0 中断处理程序
{
    if (song_playing) { // 当歌曲播放标志为真时，产生方波
        P00 = ~P00;
    } else {
        P00 = 1; // 不产生方波，关闭蜂鸣器
    }
    TH0 = val_H; //重新装入计数值
    TL0 = val_L;
}
void Delay(unsigned char cnt) //单位延时
{
  unsigned char i;
   unsigned int j;
   for(i=0; i<cnt; i++)
{
    for(j=0; j<0x3600; j++);
}
}
u8 key_scan(u8 mode)
{
	static u8 key=1;

	if(mode)key=1;//连续扫描按键
	if(key==1&&(KEY1==0||KEY2==0||KEY3==0||KEY4==0))//任意按键按下
	{
		delay_10us(1000);//消抖
		key=0;
		if(KEY1==0)
			return KEY1_PRESS;
		else if(KEY2==0)
			return KEY2_PRESS;
		else if(KEY3==0)
			return KEY3_PRESS;
		else if(KEY4==0)
			return KEY4_PRESS;	
	}
	else if(KEY1==1&&KEY2==1&&KEY3==1&&KEY4==1)	//无按键按下
	{
		key=1;			
	}
	return KEY_UNPRESS;		
}
void main()
{
    u8 key = 0;
    u8 beep_flag1 = 0; //蜂鸣器状态标志
    u8 beep_flag2 = 0;
    u8 beep_flag3 = 0;
	u8 beep_flag4 = 0;
    unsigned int val;
    unsigned char i = 0;
    TMOD = 0x01; //初始化
    IE = 0x82;
    TR0 = 1;
    while (1)
    {
        key = key_scan(0); //检测按键
      if (key == KEY1_PRESS) //K1按下，放第一首歌
        {
            beep_flag1 = !beep_flag1;
            beep_flag2 = 0;
            beep_flag3 = 0;
			beep_flag4=0;
            i = 0;
        }
        else if (key == KEY2_PRESS) //K2按下放第二首歌
        {
            beep_flag2 = !beep_flag2;
            beep_flag1 = 0;
            beep_flag3 = 0;
			beep_flag4=0;
            i = 0;
        }
        else if (key == KEY3_PRESS) //K3按下放第三首歌
        {
            beep_flag3 = !beep_flag3;
            beep_flag1 = 0;
            beep_flag2 = 0;
			beep_flag4 = 0;
            i = 0;
        }
		else if (key == KEY4_PRESS) //K4按下放第四首歌
        {
            beep_flag4 = !beep_flag4;
            beep_flag1 = 0;
            beep_flag2 = 0;
			beep_flag3 = 0;
            i = 0;
        }


        if (beep_flag1) 
		{
            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)
				  {
        beep_flag1 = beep_flag2 = beep_flag3 =beep_flag4= 0; // 清除当前正在播放的歌曲标志，停止播放并退出循环
        break;
                  }
            }
        }
        else if (beep_flag2) {
            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) {
         beep_flag1 = beep_flag2 = beep_flag3 =beep_flag4= 0; // 清除当前正在播放的歌曲标志，停止播放并退出循环
        break;
    }
            }
        }
        else if (beep_flag3)
		 {
            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) {
         beep_flag1 = beep_flag2 = beep_flag3 =beep_flag4= 0; // 清除当前正在播放的歌曲标志，停止播放并退出循环
        break;
    }
            }
        }
		 else if (beep_flag4)
		 {
            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) {
         beep_flag1 = beep_flag2 = beep_flag3 =beep_flag4= 0; // 清除当前正在播放的歌曲标志，停止播放并退出循环
        break;
    }
            }
        }
        else {
            song_playing = 0; // 没有歌曲在播放，设置歌曲播放标志为假
        }



    }
}
补充代码，使用矩阵键盘作为电子琴键，按下键演奏出相应的音符，能够保存所演奏的音乐，并重复自动播放