原创 51 音乐播放 实验(十二)

2008-5-12 10:59 3442 9 9 分类: MCU/ 嵌入式

点击下载


这个实验实现利用单片机定时器,播放音乐.详情见附件


平台:FDR51开发板


代码:


#include<AT89X52.h>
#include"key.h"  
/*************************************************************************
                    IO口宏定义区
**************************************************************************/
sbit beep="P3"^7;  
/************************************************************************
                             音乐编码存储区
**************************************************************************/
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,0x0,
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,0x6,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};


/***************************************************************
                      全局变量声明区
****************************************************************/ 
unsigned char Count="0";   
/*********************************************************************
                      定时器0中断初始化函数
**********************************************************************/
  void Time0_Init()
{
 TMOD = 0x01;
 IE   = 0x82;
 TH0  = 0xD8;
 TL0  = 0xEF;  //12MZ晶振,10ms
}  
/*************************************************************************
                      定时器0中断处理函数
**************************************************************************/
void Time0_Int() interrupt 1
{
 TH0 = 0xD8;
 TL0 = 0xEF;
 Count++;   //长度加1
}  
/*********************************************************************************
                          延时函数
**********************************************************************************/


void Delay_xMs(unsigned char x)
{
 unsigned char i,j;
 for( i =0;i < x;i++ )for( j =0;j<3;j++ );
}


/*********************************************************************************
                          放音子函数
    参数:frequence 决定音乐声调的变量
          length  决定某个音调播放的时间
*********************************************************************************/
void beeping(unsigned char frequence,unsigned char length)

 TR0=1;
 while(1)
 { 
  beep = !beep;                                              
  Delay_xMs(frequence);
  if ( length == Count )
  {
   Count = 0;
   break;
  }
 }
 TR0=0;
 beep=1;     //关蜂鸣器

/********************************************************************************
                     放音主函数


**********************************************************************************/
void Play_Song()
{
 unsigned char Temp1,
 key=0;
 unsigned int Addr="0",
 Count = 0;                     //中断计数器清0   
 while(1)
 { 
  key=getch();       //按键扫描
  if(key!=255)break;      //若按下13,停止放音
  Temp1 = SONG[Addr++];
  if ( Temp1 == 0xFF )       //休止符
  {
   TR0 = 0;
   Delay_xMs(100);
  }  
  else
  {
   if ( Temp1 == 0x00 )return; //歌曲结束符   
   else beeping(Temp1,SONG[Addr++]);
  }
 }
}  
/****************************************************
               程序主函数
*****************************************************/
void main()
{
 unsigned char k="0";
 Time0_Init();             //初始化定时器0
 while(1)
 {
  k=getch();
  if(k!=255)Play_Song(); 
 }   
}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
9
关闭 站长推荐上一条 /3 下一条