【附图和程序】简单的点亮LED和蜂鸣器放音乐不能同时实现。

2019-07-15 14:13发布

想用2个中断分别实现 LED 闪烁和蜂鸣器播放音乐。但是试了好久一直没有成功,救助大家帮忙看看哪里出了问题!

程序:
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit led=P1^0;
sbit _Speak =P1^5 ;
uchar num;
uchar Count;
uchar code SONG[] ={     
0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20,
0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,
0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,
0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,
0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,
0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,
0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,
0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,
0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20,
0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,
0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,
0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00};
void T0_time()interrupt 1
{
        TH0=(65536-45872)/256;
        TL0=(65536-45872)%256;
                        num++;
        if(num==20)
                {
                        num=0;
                        led=~led;
                }
}


void T1_Time() interrupt 3
{
TH0 = 0xD8;
TL0 = 0xEF;
Count++;   //长度加1
}
void Delay_xMs(unsigned int x)
{
    unsigned int i,j;
    for( i =0;i < x;i++ )
    {
        for( j =0;j<3;j++ );
    }
}
void Play_Song(unsigned char i)
{
unsigned char Temp1,Temp2;
unsigned int Addr;
Count = 0;      //中断计数器清0
Addr = i * 217;
while(1)
{
  Temp1 = SONG[Addr++];
     if ( Temp1 == 0xFF )          //休止符
     {
      TR0 = 0;
      Delay_xMs(100);
     }
     else if ( Temp1 == 0x00 )   //歌曲结束符
     {
      return;
     }
     else
     {
      Temp2 = SONG[Addr++];
      TR0 = 1;
     while(1)
     {
       _Speak = ~_Speak;
       Delay_xMs(Temp1);
       if ( Temp2 == Count )
       {
        Count = 0;
        break;
       }
      }
     }
}
}
void main()
{
        TMOD=0X01;
        TH0=(65536-45872)/256;
        TL0=(65536-45872)%256;
        EA=1;
        IE=0x82;
        ET0=1;
        TR0=1;
        while(1)
        {
                  Play_Song(0);  //播放
        }
}

QQ图片20160427175509.png
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。