#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;
}

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;
    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;
            i = 0;
        }
        else if (key == KEY2_PRESS) //K2按下放第二首歌
        {
            beep_flag2 = !beep_flag2;
            beep_flag1 = 0;
            beep_flag3 = 0;
            i = 0;
        }
        else if (key == KEY3_PRESS) //K3按下放第三首歌
        {
            beep_flag3 = !beep_flag3;
            beep_flag1 = 0;
            beep_flag2 = 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) 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) 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) break; // 如果有按键按下，停止播放
            }
        }
        else {
            song_playing = 0; // 没有歌曲在播放，设置歌曲播放标志为假
        }
    }
}
代码能更精简一下吗