#include "public.h"
#include "key.h"
#include "songdata.h"
#include "reg52.h"
#define Clk 0x070000
#define SMG_A_DP_PORT	P0	//使用宏定义数码管段码口
#define RELOAD_COUNT 0xFA //宏定义波特率发生器的载入值 9600

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, 880, 988, 1047, 1175}; // 音调数组

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 UART_Init(void)
{
	SCON=0X50;			//设置为工作方式1
	TMOD=0X20;			//设置计数器工作方式2
	PCON=0X80;			//波特率加倍
	TH1=RELOAD_COUNT;	//计数器初始值设置
	TL1=TH1;
	ES=1;				//关闭接收中断
	EA=1;				//打开总中断
	TR1=1;				//打开计数器	
}

void send_data(u8 sen_data)
{
    SBUF = sen_data+0x30; // 将数据放入发送寄存器
    while (!TI); // 等待发送数据完成
    TI = 0;// 清除发送完成标志位
}


void save_music(u8 key_value)
{
    int m=0;
    if (music_index < 32)
    {
        music[music_index] = tone[key_value];
		music_index++;
		send_data(key_value);
    }
	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; 
	UART_Init();//波特率为9600
    while (1)
    {	
	    if (loop_flag) {
        loop_flag = 0;
        continue;
        }
		SMG_A_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_matrix_ranks_scan(); //检测按键
        if (key == 1) //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 == 2) //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 == 3) //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 == 4) //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)
				  {
				  //停止播放并退出循环
		  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(2);
				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(4);
             if (key_scan(0) != KEY_UNPRESS) 
			 {
               song_playing = 0;
			   loop_flag=1;
                break;
               }
			  }
             }
      	 }
	   }
   }
  }

 演奏功能中，为什么第一次按按键后，蜂鸣器会响，再按按键的时候蜂鸣器就不响了？但是串口通信函数send_data还是能正常发送？