当前位置:   article > 正文

用C语言编写一个程序,能用蜂鸣器实现一段青花瓷音乐的一段播放_蜂鸣器唱歌代码青花瓷

蜂鸣器唱歌代码青花瓷

##青花瓷片段的乐谱如下

其实现代码如下:

  1. #include <REGX52.H>
  2. #include "Delay.h" //引用了头文件,其代码在最后面
  3. #include "Timer0.h" //引用了头文件,其代码在最后面
  4. //蜂鸣器端口定义
  5. sbit Buzzer=P2^5;
  6. //播放速度,值为四分音符的时长(ms)
  7. #define SPEED 500
  8. //音符与索引对应表,P:休止符,L:低音,M:中音,H:高音,下划线:升半音符号#
  9. #define P 0
  10. #define L1 1
  11. #define L1_ 2
  12. #define L2 3
  13. #define L2_ 4
  14. #define L3 5
  15. #define L4 6
  16. #define L4_ 7
  17. #define L5 8
  18. #define L5_ 9
  19. #define L6 10
  20. #define L6_ 11
  21. #define L7 12
  22. #define M1 13
  23. #define M1_ 14
  24. #define M2 15
  25. #define M2_ 16
  26. #define M3 17
  27. #define M4 18
  28. #define M4_ 19
  29. #define M5 20
  30. #define M5_ 21
  31. #define M6 22
  32. #define M6_ 23
  33. #define M7 24
  34. #define H1 25
  35. #define H1_ 26
  36. #define H2 27
  37. #define H2_ 28
  38. #define H3 29
  39. #define H4 30
  40. #define H4_ 31
  41. #define H5 32
  42. #define H5_ 33
  43. #define H6 34
  44. #define H6_ 35
  45. #define H7 36
  46. //索引与频率对照表
  47. unsigned int FreqTable[]={
  48. 0,
  49. 63628,63731,63835,63928,64021,64103,64185,64260,64331,64400,64463,64528,
  50. 64580,64633,64684,64732,64777,64820,64860,64898,64934,64968,65000,65030,
  51. 65058,65085,65110,65134,65157,65178,65198,65217,65235,65252,65268,65283,
  52. };
  53. //乐谱
  54. unsigned char code Music[]={
  55. //音符,时值,
  56. //1
  57. H5,4,
  58. H5,4,
  59. H3,4,
  60. H2,4,
  61. H3,4,
  62. M6,4,
  63. H2,4,
  64. H3,4,
  65. H5,4,
  66. H3,4,
  67. H2,4,
  68. H5,4,
  69. H5,4,
  70. H3,4,
  71. H2,4,
  72. H3,4,
  73. M5,4,
  74. H2,4,
  75. H3,4,
  76. H5,4,
  77. H2,4,
  78. H1,4,
  79. H1,4,
  80. H2,4,
  81. H3,4,
  82. H5,4,
  83. H6,4,
  84. H5,4,
  85. H3,4,
  86. H5,4,
  87. H3,4,
  88. H3,4,
  89. H2,4,
  90. H2,4,
  91. H1,4,
  92. H2,4,
  93. H1,4,
  94. H2,4,
  95. H1,4,
  96. H2,4,
  97. H3,4,
  98. H5,4,
  99. H3,4,
  100. H5,4,
  101. H5,4,
  102. H3,4,
  103. H2,4,
  104. H3,4,
  105. M6,4,
  106. H2,4,
  107. H3,4,
  108. H5,4,
  109. H3,4,
  110. H2,4,
  111. H5,4,
  112. H5,4,
  113. H3,4,
  114. H2,4,
  115. H3,4,
  116. M5,4,
  117. H2,4,
  118. H3,4,
  119. H5,4,
  120. H2,4,
  121. H1,4,
  122. H1,4,
  123. H2,4,
  124. H3,4,
  125. H5,4,
  126. H6,4,
  127. H5,4,
  128. H3,4,
  129. H5,4,
  130. H3,4,
  131. H3,4,
  132. H2,4,
  133. H2,4,
  134. H1,4,
  135. H2,4,
  136. H1,4,
  137. H2,4,
  138. H1,4,
  139. H2,4,
  140. H3,4,
  141. H5,4,
  142. H3,4,
  143. M5,4,
  144. H3,4,
  145. H2,4,
  146. H2,4,
  147. H1,4,
  148. 0xFF //终止标志
  149. };
  150. unsigned char FreqSelect,MusicSelect;
  151. void main()
  152. {
  153. Timer0Init();
  154. while(1)
  155. {
  156. if(Music[MusicSelect]!=0xFF) //如果不是停止标志位
  157. {
  158. FreqSelect=Music[MusicSelect]; //选择音符对应的频率
  159. MusicSelect++;
  160. Delay(SPEED/4*Music[MusicSelect]); //选择音符对应的时值
  161. MusicSelect++;
  162. TR0=0;
  163. Delay(5); //音符间短暂停顿
  164. TR0=1;
  165. }
  166. else //如果是停止标志位
  167. {
  168. TR0=0;
  169. while(1);
  170. }
  171. }
  172. }
  173. void Timer0_Routine() interrupt 1
  174. {
  175. if(FreqTable[FreqSelect]) //如果不是休止符
  176. {
  177. /*取对应频率值的重装载值到定时器*/
  178. TL0 = FreqTable[FreqSelect]%256; //设置定时初值
  179. TH0 = FreqTable[FreqSelect]/256; //设置定时初值
  180. Buzzer=!Buzzer; //翻转蜂鸣器IO口
  181. }
  182. }
  183. //
  184. void Delay(unsigned int xms)
  185. {
  186. unsigned char i, j;
  187. while(xms--)
  188. {
  189. i = 2;
  190. j = 239;
  191. do
  192. {
  193. while (--j);
  194. } while (--i);
  195. }
  196. }
  197. //
  198. #include <REGX52.H>
  199. /**
  200. * @brief 定时器0初始化,1毫秒@12.000MHz
  201. * @param 无
  202. * @retval 无
  203. */
  204. void Timer0Init(void)
  205. {
  206. TMOD &= 0xF0; //设置定时器模式
  207. TMOD |= 0x01; //设置定时器模式
  208. TL0 = 0x18; //设置定时初值
  209. TH0 = 0xFC; //设置定时初值
  210. TF0 = 0; //清除TF0标志
  211. TR0 = 1; //定时器0开始计时
  212. ET0=1;
  213. EA=1;
  214. PT0=0;
  215. }
  216. /*定时器中断函数模板
  217. void Timer0_Routine() interrupt 1
  218. {
  219. static unsigned int T0Count;
  220. TL0 = 0x18; //设置定时初值
  221. TH0 = 0xFC; //设置定时初值
  222. T0Count++;
  223. if(T0Count>=1000)
  224. {
  225. T0Count=0;
  226. }
  227. }
  228. */

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/1007007
推荐阅读
相关标签
  

闽ICP备14008679号