找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1788|回复: 2
打印 上一主题 下一主题
收起左侧

单片机音乐播放控制不了

[复制链接]
跳转到指定楼层
楼主
大神门帮我看看这个程序,仿真时一运行音乐就开始播放了,我想通过一个按键来控制播放,可是我不管怎么加条件还是控制不了,急急急

#include "reg51.h"
unsigned char Count;
sbit _Speak =P1^0 ;
unsigned char 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 Time0_Init()
{
TMOD = 0x01;
IE   = 0x82;
TH0  = 0xD8;
TL0  = 0xEF;  //12MZ晶振,10ms
}

void Time0_Int() interrupt 1
{
TH0 = 0xD8;
TL0 = 0xEF;
Count++;   //长度加1
}

/*-------------------------------------------------
功能:1MS延时子程序
-------------------------------------------------*/
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()
{
Time0_Init();   //定时器0中断初始化
while(1)
{
  Play_Song(0);  //播放
}
}

新建压缩(zipped)文件夹.zip

57.61 KB, 下载次数: 4

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:160834 发表于 2018-4-6 13:07 | 只看该作者
在你 sbit  _Speak =P1^0 ; 下面加一个按钮定义。 sbit   KEY1 =P1^1;

void main()
{
    Time0_Init();   //定时器0中断初始化
   while(1)
{
    if (KEY1 == 0)
   {
     //这里可以加个延时作消抖。
      Play_Song(0);  //播放
   }
  }
}

你的程序已仿真试过。没有问题。程序加上红色部分就可以一个按钮控制播了。

回复

使用道具 举报

板凳
ID:277480 发表于 2018-4-6 20:18 来自手机 | 只看该作者
QIANLILI 发表于 2018-4-6 13:07
在你 sbit  _Speak =P1^0 ; 下面加一个按钮定义。 sbit   KEY1 =P1^1;

void main()

好的谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表