原创 51中音乐的音调和节拍的确定(网络整理)

2008-10-12 18:30 2856 10 12 分类: MCU/ 嵌入式

     一般说来,单片机演奏音乐基本都是单音频率,它不包含相应幅度的谐波频率,也就是说不能象电子琴那样能奏出多种音色的声音。因此单片机奏乐只需弄清楚两个概念即可,也就是“音调”和“节拍”。音调表示一个音符唱多高的频率,节拍表示一个音符唱多长的时间。
    1)音调的确定
       音调就是我们常说的音高。它是由频率来确定的!我们可以查出各个音符所对应的相应的频率,那么现在就需要我们来用51来发出相应频率的声音!
       我们常采用的方法就是通过单片机的定时器定时中断,将单片机上对应蜂鸣器的I/O口来回取反,或者说来回清零,置位,从而让蜂鸣器发出声音,为了让单片机发出不同频率的声音,我们只需将定时器予置不同的定时值就可实现。那么怎样确定一个频率所对应的定时器的定时值呢?
以标准音高A为例:
       
A的频率f = 440 Hz,其对应的周期为:T = 1/ f = 1/440 =2272μs
那么,单片机上对应蜂鸣器的I/O口来回取反的时间应为:
     
t = T/2 = 2272/2 = 1136μs,也就是清零、置位在一个周期内完成.
      这个时间t也就是单片机上定时器应有的中断触发时间。一般情况下,单片机奏乐时,其定时器为工作方式1,它以振荡器的十二分频信号为计数脉冲。设振荡器频率为f0,则定时器的予置初值由下式来确定:
                
t = 12 *TALL – THL/ f0
式中TALL = 216 = 65536,THL为定时器待确定的计数初值。因此定时器的高低计数器的初值为:  
TH = THL / 256 = ( TALL – t* f0/12) / 256
 TL = THL % 256 = ( TALL – t* f0/12) %256
 t=1136μs代入上面两式(注意:计算时应将时间和频率的单位换算一致),即可求出标准音高A在单片机晶振频率f0=12Mhz,定时器在工作方式1下的定时器高低计数器的予置初值为
  TH440Hz = (65536 1136 * 12/12) /256 = FBH
 TL440Hz = (65536 1136 * 12/12)%256 = 90H
就这样,我们通过延时,发出了我们所需要的频率


以单片机12MHZ晶振为例,例出高中低音符与单片机计数T0相关的计数值如下表所示


音符


频率(HZ


简谱码(T值)


 <?XML:NAMESPACE PREFIX = O />


音符


频率(HZ


简谱码(T值)


1 DO


262


63628


# 4 FA#


740


64860


#1 DO#


277


63731


5 SO


784


64898


2 RE


294


63835


# 5 SO#


831


64934


#2 RE#


311


63928


6 LA


880


64968


3 M


330


64021


# 6


932


64994


4 FA


349


64103


7 SI


988


65030


# 4 FA#


370


64185


1 DO


1046


65058


5 SO


392


64260


# 1 DO#


1109


65085


# 5 SO#


415


64331


2 RE


1175


65110


6 LA


440


64400


 


# 2 RE#


1245


65134


# 6


466


64463


3 M


1318


65157


7 SI


494


64524


4 FA


1397


65178


1 DO


523


64580


# 4 FA#


1480


65198


# 1 DO#


554


64633


5 SO


1568


65217


2 RE


587


64684


# 5 SO#


1661


65235


# 2 RE#


622


64732


6 LA


1760


65252


3 M


659


64777


# 6


1865


65268


4 FA


698


64820


7 SI


1967


65283


下面我们要为这个音符建立一个表格,有助于单片机通过查表的方式来获得相应的数据


低音019之间,中音在2039之间,高音在4059之间


TABLE:        DW 0,63628,63835,64021,64103,64260,64400,64524,0,0


                   DW 0,63731,63928,0,64185,64331,64463,0,0,0


                   DW 0,64580,64684,64777,64820,64898,64968,65030,0,0


                   DW 0,64633,64732,0,64860,64934,64994,0,0,0


                   DW 0,65058,65110,65157,65178,65217,65252,65283,0,0


                   DW 0,65085,65134,0,65198,65235,65268,0,0,0


                   DW 0



         2)节拍的确定
             在一张乐谱中,我们经常会看到这样的表达式,如1=C (4/4) 、1=G(3/4) …… 等等,这里1=C (4/4),1=G(3/4)表示乐谱的曲调,和我们前面所谈的音调有很大的关联,4/4、3/4 就是用来表示节拍的。以3/4为例加以说明,它表示乐谱中以四分音符为节拍,每一小结有三拍。
               例如:     b97bd494-a666-4a51-8dd4-00b20a726529.jpg
其中1 2 为一拍,345为一拍,6为一拍共三拍。1 2的时长为四分音符的一半,即为八分音符长,34的时长为八分音符的一半,即为十六分音符长,5的时长为四分音符的一半,即为八分音符长,6的时长为四分音符长。
    一般说来,如果乐曲没有特殊说明,一拍的时长大约为400—500ms
    可见,在单片机上控制一个音符唱多长可采用循环延时的方法来实现。首先,我们确定一个基本时长的延时程序,比如说以十六分音符的时长为基本延时时间,那么,对于一个音符,如果它为十六分音符,则只需调用一次延时程序,如果它为八分音符,则只需调用二次延时程序,如果它为四分音符,则只需调用四次延时程序,依次类推。

 音乐的音拍,一个节拍为单位(C调)




曲调值


DELAY


 


曲调值


DELAY


4/4


125ms


 


4/4


62ms


3/4


187ms


 


3/4


94ms


2/4


250ms


 


2/4


125ms


    通过上面关于一个音符音调和节拍的确定方法,我们就可以在单片机上实现演奏音乐了。具体的实现方法为:将乐谱中的每个音符的音调及节拍变换成相应的音调参数和节拍参数,将他们做成数据表格,存放在存储器中,通过程序取出一个音符的相关参数,播放该音符,该音符唱完后,接着取出下一个音符的相关参数……,如此直到播放完毕最后一个音符,根据需要也可循环不停地播放整个乐曲。另外,对于乐曲中的休止符,一般将其音调参数设为FFHFFH,其节拍参数与其他音符的节拍参数确定方法一致,乐曲结束用节拍参数为00H来表示。


附上:
//挥着翅膀的女孩  
unsigned char code Music_Girl[]={ 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x03,
                                  0x16,0x03, 0x17,0x03, 0x17,0x03, 0x17,0x03, 0x18,0x03,
                                  0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x02, 0x18,0x03,
                                  0x17,0x03, 0x15,0x02, 0x18,0x03, 0x17,0x03, 0x18,0x02,
                                  0x10,0x03, 0x15,0x03, 0x16,0x02, 0x15,0x03, 0x16,0x03,
                                  0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x1A,0x03,
                                  0x1B,0x03, 0x1F,0x03, 0x1F,0x03, 0x17,0x03, 0x18,0x03,
                                  0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03,
                                  0x18,0x03, 0x1F,0x03, 0x1F,0x02, 0x16,0x03, 0x17,0x03,
                                  0x18,0x03, 0x17,0x03, 0x18,0x03, 0x20,0x03, 0x20,0x02,
                                  0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03,
                                  0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03,
                                  0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66,
                                  0x1A,0x03, 0x19,0x03, 0x15,0x03, 0x15,0x03, 0x17,0x03,
                                  0x16,0x66, 0x17,0x04, 0x18,0x04, 0x18,0x03, 0x19,0x03,
                                  0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03,
                                  0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03,
                                  0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66,
                                  0x1A,0x03, 0x19,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03,
                                  0x1F,0x00, 0x1A,0x03, 0x1A,0x03, 0x1A,0x03, 0x1B,0x03,
                                  0x1B,0x03, 0x1A,0x03, 0x19,0x03, 0x19,0x02, 0x17,0x03,
                                  0x15,0x17, 0x15,0x03, 0x16,0x03, 0x17,0x03, 0x18,0x03,
                                  0x17,0x04, 0x18,0x0E, 0x18,0x03, 0x17,0x04, 0x18,0x0E,
                                  0x18,0x66, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03,
                                  0x20,0x03, 0x20,0x02, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66,
                                  0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03, 0x1B,0x03,
                                  0x1F,0x66, 0x1F,0x04, 0x1B,0x0E, 0x1B,0x03, 0x19,0x03,
                                  0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03,
                                  0x15,0x03, 0x15,0x03, 0x17,0x03, 0x16,0x66, 0x17,0x04,
                                  0x18,0x04, 0x18,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03,
                                  0x1F,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03,
                                  0x1B,0x03, 0x1F,0x66, 0x1F,0x03, 0x1B,0x03, 0x19,0x03,
                                  0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03,
                                  0x19,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x00, 0x18,0x02,
                                  0x18,0x03, 0x1A,0x03, 0x19,0x0D, 0x15,0x03, 0x15,0x02,
                                  0x18,0x66, 0x16,0x02, 0x17,0x02, 0x15,0x00, 0x00,0x00};
//同一首歌
unsigned char code Music_Same[]={ 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x66, 0x18,0x03,
                                  0x17,0x02, 0x15,0x02, 0x16,0x01, 0x15,0x02, 0x10,0x02,
                                  0x15,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x02,
                                  0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x02, 0x18,0x66,
                                  0x17,0x03, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x16,0x00,
                                  0x17,0x01, 0x19,0x02, 0x1B,0x02, 0x1B,0x70, 0x1A,0x03,
                                  0x1A,0x01, 0x19,0x02, 0x19,0x03, 0x1A,0x03, 0x1B,0x02,
                                  0x1A,0x0D, 0x19,0x03, 0x17,0x00, 0x18,0x66, 0x18,0x03,
                                  0x19,0x02, 0x1A,0x02, 0x19,0x0C, 0x18,0x0D, 0x17,0x03,
                                  0x16,0x01, 0x11,0x02, 0x11,0x03, 0x10,0x03, 0x0F,0x0C,
                                  0x10,0x02, 0x15,0x00, 0x1F,0x01, 0x1A,0x01, 0x18,0x66,
                                  0x19,0x03, 0x1A,0x01, 0x1B,0x02, 0x1B,0x03, 0x1B,0x03,
                                  0x1B,0x0C, 0x1A,0x0D, 0x19,0x03, 0x17,0x00, 0x1F,0x01,
                                  0x1A,0x01, 0x18,0x66, 0x19,0x03, 0x1A,0x01, 0x10,0x02,
                                  0x10,0x03, 0x10,0x03, 0x1A,0x0C, 0x18,0x0D, 0x17,0x03,
                                  0x16,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x70,
                                  0x18,0x03, 0x17,0x02, 0x15,0x03, 0x15,0x03, 0x16,0x66,
                                  0x16,0x03, 0x16,0x02, 0x16,0x03, 0x15,0x03, 0x10,0x02,
                                  0x10,0x01, 0x11,0x01, 0x11,0x66, 0x10,0x03, 0x0F,0x0C,
                                  0x1A,0x02, 0x19,0x02, 0x16,0x03, 0x16,0x03, 0x18,0x66,
                                  0x18,0x03, 0x18,0x02, 0x17,0x03, 0x16,0x03, 0x19,0x00,
                                  0x00,0x00 };
//两只蝴蝶                                 
unsigned char code Music_Two[] ={ 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03,
                                  0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02,
                                  0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03,
                                  0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x16,0x03,
                                  0x17,0x01, 0x16,0x03, 0x17,0x03, 0x16,0x03, 0x15,0x01,
                                  0x10,0x03, 0x15,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03,
                                  0x16,0x03, 0x15,0x03, 0x10,0x03, 0x15,0x03, 0x16,0x01,
                                  0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03,
                                  0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02,
                                  0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03,
                                  0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x19,0x03,
                                  0x19,0x01, 0x19,0x03, 0x1A,0x03, 0x19,0x03, 0x17,0x01,
                                  0x16,0x03, 0x16,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03,
                                  0x16,0x03, 0x15,0x03, 0x10,0x03, 0x10,0x0D, 0x15,0x00,
                                  0x19,0x03, 0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03,
                                  0x1B,0x03, 0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03,
                                  0x16,0x0D, 0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03,
                                  0x1A,0x02, 0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03,
                                  0x16,0x01, 0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03,
                                  0x19,0x02, 0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E,
                                  0x1B,0x04, 0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E,
                                  0x1B,0x04, 0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03,
                                  0x17,0x0D, 0x16,0x03, 0x17,0x03, 0x19,0x01, 0x19,0x03,
                                  0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03, 0x1B,0x03,
                                  0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03, 0x16,0x03,
                                  0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03, 0x1A,0x02,
                                  0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x01,
                                  0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03, 0x19,0x03,
                                  0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
                                  0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
                                  0x17,0x16, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
                                  0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03, 0x0F,0x02,
                                  0x10,0x03, 0x15,0x00, 0x00,0x00 };

PARTNER CONTENT

文章评论2条评论)

登录后参与讨论

用户194501 2009-9-18 13:12

你好!我想问一下,对于不同调的音乐(比如C调,G调……)它所对应的简码的频率一不一样呢? 比如: A调音乐中的1的频率与G调音乐中的1的频率一不一样?谢谢!

用户116683 2008-10-13 08:51

写得好.我也正打算整理下这方面的资料.
相关推荐阅读
用户61911 2009-01-25 13:46
给大家拜年!
一拜全家好;二拜困难少;三拜烦恼消;四拜不变老;五拜儿女孝;六拜幸福绕;七拜忧愁抛;八拜收入高;九拜平安罩;十拜乐逍遥!"新" 歌一曲送祝福"年" 年有余丰五谷"快" 马加鞭奔小康"乐" 乐呵呵享福禄...
用户61911 2008-11-11 22:41
ARM-AN2410SSB之在Keil中和H-Jtag调试
今天进行了Keil+H-Jtag的仿真调试!按照下图的设置即可!进入选项(怎么进入就不说了)把Linker中设置如下,道理和设置AXD的一样!在Keil中配置使用RDI接口点击“Settings”,进...
用户61911 2008-11-11 20:52
ARM-AN2410SSB之'中文路径导致的错误'
今天用AXD进行调试的时候,出现了一个错误!‘c:\documents and settings\“你的用户名”\default-1-2-0-0.ses' could not be loaded 后来...
用户61911 2008-11-06 17:28
ARM-AN2410SSB之LED~GPIO/INT
一、GPIO试验书中的程序有几处问题,正确为:#include "2410addr.h"#define  KEY1 (1<<3)#define  KEY2 (1<<6)#def...
用户61911 2008-11-06 00:24
AND电路与OR电路(二极管)
开关二极管的AND电路与OR电路的应用AND电路,实现数字电路中的‘与’OR电路,实现数字电路中的‘或’...
用户61911 2008-11-05 23:09
ARM-AN2410SSB之LED
https://static.assets-stash.eet-china.com/album/old-resources/2008/11/5/d85930fd-74c9-4799-ab71-6854...
EE直播间
更多
我要评论
2
10
关闭 站长推荐上一条 /3 下一条