新闻  |   论坛  |   博客  |   在线研讨会
用8515做的音乐演奏程序
zhchxgh | 2009-07-04 01:16:47    阅读:883   发布文章

用8515做的音乐演奏程序

/**********************************************
硬件电路使用AT90S8515的PC1口通过一个100UF/12V的电容接一个8欧姆的喇叭
单片机的晶振频率为4MHZ

音乐数据中的x:11-17表示低音区;21-27表示中音区;31-37表示高音区
音乐数据中的y:表示每个音阶延时的时间
**********************************************/
#include <io8515.h>
#include <macros.h>
#include <math.h>
#pragma interrupt_handler timer0:8
#pragma interrupt_handler timer1:5
#pragma data:code

flash unsigned char music_data[][2]=
{
/*******************************************
       世上只有妈妈好音乐数据{x,y}
       x:对应音符音阶,0表示休止符
       y:对应音符节拍
*******************************************/

{26,6} ,{25,2} ,{23,4} ,{25,2} ,{26,2} ,
{31,4} ,{26,2} ,{25,2} ,{26,4} ,{ 0,4} ,

{23,4} ,{25,2} ,{26,2}, {25,4} ,{23,2} ,
{22,2} ,{21,2} ,{16,2} ,{25,2} ,{23,2} ,{22,6} ,{ 0,4} ,

{22,4} ,{22,2} ,{23,2} ,{25,4}, {25,2} ,
{26,2} ,{23,4} ,{22,4}, {21,4} ,{ 0,4} ,

{25,6} ,{23,2} ,{22,2}, {21,2} ,{16,2} ,
{21,2} ,{15,4} ,{ 0,4} ,

{ 0,8},
//{ 0,0}
//};

/*******************************************
       梁山伯与祝英台音乐数据{x,y}
       x:对应音符音阶,0表示休止符
       y:对应音符节拍
*******************************************/

{13,8} ,{15,6} ,{16,2} ,{21,6},
{22,2} ,{16,2} ,{21,2} ,{15,4},
{25,6} ,{31,2} ,{26,2} ,{25,2},
{23,2} ,{25,2} ,{22,8} ,{ 0,4},

{22,6}, {23,2} ,{17,4} ,{16,4},
{15,6}, {16,2} ,{21,4} ,{22,4},
{13,4}, {21,4} ,{16,2} ,{15,2},
{16,2}, {21,2} ,{15,8} ,{ 0,4},

{23,6} ,{25,2}, {17,4} ,{22,4},
{16,2} ,{21,2}, {15,4} ,{15,4},
{13,3} ,{15,1}, {13,4} ,{15,3},
{16,1} ,{17,2}, {22,2} ,{16,4},{ 0,2} ,

{15,2} ,{16,2} ,{21,6} ,{22,2}, 
{25,4} ,{23,4} ,{22,4} ,{23,2}, 
{22,2} ,{21,4} ,{16,2} ,{15,2}, 
{13,8} ,{21,8} ,{16,3} ,{21,1},
{16,2} ,{15,2} ,{13,2} ,{15,2},
{16,2} ,{21,2} ,{15,8}, { 0,4},

{ 0,8},
//{ 0,0}
//};

/*******************************************
       卡秋莎音乐数据{x,y}
       x:对应音符音阶,0表示休止符
       y:对应音符节拍
*******************************************/

{16,6} ,{17,2} ,{21,6} ,{16,2},
{21,4} ,{17,2} ,{16,2} ,{17,4},
{13,4} ,{17,6} ,{21,2} ,{22,6},
{17,2} ,{22,4} ,{21,2} ,{17,2},
{16,8} ,{23,4} ,{26,4} ,{25,4},
{26,2} ,{25,2} ,{24,4} ,{23,2},
{22,2} ,{23,4} ,{16,4} ,{ 0,2},//休止符
{24,4} ,{22,2} ,{23,6} ,{21,2},
{17,2} ,{13,2} ,{21,2} ,{17,2},
{16,8} ,{23,4} ,{26,4} ,{25,4},
{26,2} ,{25,2} ,{24,4} ,{23,2},
{22,2} ,{23,4} ,{16,4} ,{ 0,2},//休止符
{24,4} ,{22,2}, {23,6} ,{21,2},
{17,2} ,{13,2}, {21,2} ,{17,2},
{16,8} ,
{ 0,8} ,
{ 0,0}
};


#pragma data:data
unsigned int delay=0;
/*******************************
       MCU初始化
*******************************/
void music_init(void)
{
  MCUCR=0x00;
  DDRC=0x01;
  TCCR1A=0x00;
  TCCR1B=0x09;
  TCCR0=0x03;
  TCNT0=0x19;
  TIMSK=0x42;
  }
/**************************************************
      T0中断程序,产生音乐节拍
**************************************************/  
void timer0(void) 
    {

     delay++;
     TCNT0=0xb0;
    }
/**************************************************
      T1中断程序,根据SOUND函数输出一定频率的方波
**************************************************/  
void timer1(void) 
    {
     PORTC^=0x01;
    }
    
/**************************************************

**************************************************/  
sel(unsigned char a)
    {
    unsigned int x;

        switch (a)
        {
         case 11:x=262;break;
         case 12:x=294;break;
         case 13:x=330;break;
         case 14:x=349;break;
         case 15:x=392;break;
         case 16:x=440;break;
         case 17:x=494;break;

         case 21:x=523;break;
         case 22:x=578;break;
         case 23:x=659;break;
         case 24:x=698;break;
         case 25:x=784;break;
         case 26:x=880;break;
         case 27:x=988;break;
         
         case 31:x=1045;break;
         case 32:x=1175;break;
         case 33:x=1318;break;
         case 34:x=1397;break;
         case 35:x=1568;break;
         case 36:x=1760;break;
         case 37:x=1976;break;
     
         default:x=0;break;

         }
    return(x);
    }
    
/*****************************************************
      SOUND程序,输出频率为 x HZ的方波,延时 y MS
       x:100~20000 HZ, 0表示不发声
       y:0~65536 MS
*****************************************************/     
void sound(unsigned int x,unsigned int y)
    {
       SEI(); 
       delay=0; 
       if (x!=0)
        {         
         x=2000000/x; 
         OCR1A=x;   
         TCNT1=0x00;
         TIMSK|=0x40;
          while(delay<y)
          ;
          TIMSK&=0xbf;      
         }
      else
         {
          TIMSK&=0xbf;
          while(delay<y)
          ;
          }
      CLI(); 
             
    }
    

    
    
/*******************************
          主程序
*******************************/  
void main(void)
  {
   unsigned char i=0;
   music_init();  
   while(1)
      {
       while(music_data[i][1]!=0)
           {
            sound(sel(music_data[i][0]),music_data[i][1]*100);
            i++;
            }
       i=0;
       }
   }


该程序参照双龙的光盘内附带程序,并稍加改动

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
推荐文章
最近访客