找回密码
 立即注册

QQ登录

只需一步,快速开始

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

利用单片机定时器产生乐曲+源程序+仿真图

[复制链接]
跳转到指定楼层
楼主
利用定时器产生乐曲仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)


单片机源码:

  1. #include<reg52.h>                 //库文件
  2. #define uchar unsigned char//宏定义无符号字符型
  3. #define uint unsigned int  //宏定义无符号整型
  4. /********************************************************************
  5.                             初始定义
  6. *********************************************************************/
  7. uchar Count;
  8. sbit _Speak =P3^7 ; //讯响器控制脚
  9. /********************************************************************
  10.                             歌曲字根码
  11. *********************************************************************/
  12. unsigned char code SONG[] ={ //祝你平安
  13. 0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20,
  14. 0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,
  15. 0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,
  16. 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,
  17. 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,
  18. 0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,
  19. 0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,
  20. 0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,
  21. 0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,
  22. 0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,
  23. 0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20,
  24. 0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,
  25. 0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,
  26. 0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00,
  27. //路边的野华不要采
  28. 0x30,0x1C,0x10,0x20,0x40,0x1C,0x10,0x18,0x10,0x20,0x10,0x1C,0x10,0x18,0x40,0x1C,
  29. 0x20,0x20,0x20,0x1C,0x20,0x18,0x20,0x20,0x80,0xFF,0x20,0x30,0x1C,0x10,0x18,0x20,
  30. 0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,
  31. 0x30,0x80,0xFF,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
  32. 0x20,0x2B,0x40,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
  33. 0x20,0x2B,0x40,0x20,0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,
  34. 0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,0x30,0x80,0x20,0x30,0x1C,0x10,0x20,
  35. 0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,0x15,0x1F,
  36. 0x05,0x20,0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,
  37. 0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,
  38. 0x20,0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30,0x1C,0x10,0x18,0x40,0x1C,0x20,0x20,
  39. 0x20,0x26,0x40,0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,0x80,0x00,};
  40. /********************************************************************
  41.                             定时中断服务函数
  42. *********************************************************************/
  43. void Time0_Init()
  44. {
  45.         TMOD = 0x01;
  46.         IE = 0x82;
  47.         TH0 = 0xD8;
  48.         TL0 = 0xEF; //12MZ晶振,10ms
  49. }
  50. /*********************************************************************/
  51. void Time0_Int() interrupt 1
  52. {
  53.         TH0 = 0xD8;
  54.         TL0 = 0xEF;                     
  55.         Count++; //长度加1
  56. }
  57. /*********************************************************************/
  58. void Delay_xMs(unsigned int x) //1MS 延时子程序
  59. {
  60.         unsigned int i,j;
  61.         for( i =0;i < x;i++ )
  62.         {
  63.                 for( j =0;j<3;j++ );
  64.         }
  65. }
  66. /*********************************************************************/
  67. void Play_Song(unsigned char i) //:歌曲播放子程序i 为播放哪一段曲目
  68. {
  69.         unsigned char Temp1,Temp2;
  70.         unsigned int Addr;
  71.         Count = 0; //中断计数器清0
  72.         Addr = i * 217;
  73. while(1)
  74.         {
  75.                 Temp1 = SONG[Addr++];
  76.                 if ( Temp1 == 0xFF ) //休止符
  77.                         {
  78.                                 TR0 = 0; Delay_xMs(100);
  79.                         }
  80.         else if ( Temp1 == 0x00 ) //歌曲结束符
  81.                 {
  82.                         return;
  83.                 }
  84. else
  85. {
  86.         Temp2 = SONG[Addr++];
  87.         TR0 = 1;
  88.         while(1)
  89.         {
  90.         _Speak = ~_Speak;
  91.         Delay_xMs(Temp1);
  92.         if ( Temp2 == Count )
  93.                 {
  94.                         Count = 0;
  95.                         break;
  96.                 }
  97.         }
  98. }
  99. }
  100. }
  101. /********************************************************************
  102.                             主函数
  103. *********************************************************************/
  104. void main() //主程序
  105. {
  106.         Time0_Init(); //定时器0 中断初始化
  107.         while(1)
  108.         {
  109.                 Play_Song(0);
  110.         } //播放
  111. }

  112. /********************************************************************
  113.                               结束
  114. *********************************************************************/
复制代码

全部资料51hei下载地址:
利用定时器产生乐曲.zip (61.3 KB, 下载次数: 10)



评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

沙发
ID:138707 发表于 2022-3-6 20:17 | 只看该作者
怎么把歌改成电子谱
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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