找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机电子音乐盒 程序+仿真

[复制链接]
跳转到指定楼层
楼主


程序和protrues仿真文件下载: yinyuehe.rar (66.54 KB, 下载次数: 95)

内置两首歌曲可切换,led会随音乐变化,可以切换led闪烁样式

源码预览:
  1. #include <reg52.h>
  2. #define uchar unsigned char
  3. #define uint  unsigned int
  4. sbit key1=P3^2;//?key1?????
  5. sbit key2=P3^3;//?key2?????
  6. sbit fm=P3^7;//??????IO?
  7. sbit key3=P3^5;//??/???
  8. //***************???????
  9. uchar  code  huayang1[]=
  10. {0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf};
  11. uchar  code  huayang2[]=
  12. {0x7f,0xfe,0xbf,0xfd,0xdf,0xfb,0xef,0xf7,0xef,0xfb,0xdf,0xfd,0xbf,0xfe};
  13. uchar  code  huayang3[]=
  14. {0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x0,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};
  15. uchar  code  huayang4[]={ 0x55,0xaa,0xcc,0x33,0x99,0x66,0x0f,0xf0};
  16. uchar count1;//????       
  17. uchar count2;//????       
  18. uchar timeh,timel,i,count3=0;
  19. //---------------------------??---------------------------------------
  20. //????:???????,???????,
  21. //??????????
  22. //1-7????C?,8-E?????,0????
  23. //???0?????
  24. uchar code qnzl[]={         //????
  25. 0x12,0x22,0x34,0x84,0x74,0x54,0x38,0x42,0x32,0x22,0x42,0x34,0x84,0x72,0x82,0x94,0xA8,0x08,//??
  26. 0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,//????? ?????
  27. 0x32,0x31,0x21,0x32,0x82,0x71,0x81,0x71,0x51,0x32,0x22,//????? ??????
  28. 0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,//????? ?????
  29. 0x32,0x31,0x21,0x32,0x83,0x82,0x71,0x72,0x02,//????????
  30. 0x63,0xA1,0xA2,0x62,0x92,0x82,0x52,//??????
  31. 0x31,0x51,0x63,0x51,0x63,0x51,0x63,0x51,0x62,0x82,0x7C,0x02,//???????????
  32. 0x61,0x71,0x82,0x71,0x62,0xA2,0x71,0x76,//???????
  33. 0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,//???????
  34. 0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,//?????? ???
  35. 0x42,0x63,0x83,0x83,0x91,0x91,//??????
  36. 0x61,0x71,0x82,0x71,0x62,0x0A2,0x71,0x76,//???????
  37. 0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,//???????
  38. 0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,//?????? ???
  39. 0x42,0x82,0x88,0x02,0x74,0x93,0x89,0xff//????//?????
  40. };
  41. uchar code jmszl[]={ //?????
  42. 0x12,0x12,0x22,0x32,0x31,0x22,0x21,0x22,//???????
  43. 0x21,0x31,0x51,0x52,0x31,0x52,0x61,0x15,0x14,//????????
  44. 0x51,0x52,0x31,0x52,0x62,0x13,0x11,0x13,0x32,0x28,0x08,0x28,//??????????
  45. 0x31,0x32,0x31,0x32,0x11,0x21,0x51,0x52,0x51,0x52,//??????????
  46. 0x51,0x51,0x31,0x32,0x31,0x32,0x81,0x72,0x63,//?????????
  47. 0x62,0x71,0x81,0x72,0x61,0x61,0x52,0x31,0x21,0x32,0x51,0x54,//???????????
  48. 0x22,0x12,0x11,0x12,0x11,0x12,0x12,0x14,0x26,0x32,0x26,//??????
  49. 0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,//?????????????
  50. 0x02,0x32,0x81,0x81,0x81,0x81,0x62,0x52,0x34,//????????
  51. 0x31,0x81,0x81,0x81,0x61,0x91,0x82,//???????
  52. 0x51,0x51,0x51,0x51,0x31,0x61,0x53,//??????       
  53. 0x21,0x11,0x21,0x11,0x22,0x11,0x21,0x26,//???????
  54. 0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,0x52,//?????????????
  55. 0x31,0x31,0x81,0x81,0x81,0x61,0x91,0x81,0x61,0x31,0x56,//???????????
  56. 0x32,0x32,0x81,0x81,0x81,0x81,0x91,0x81,0x61,0x81,0x61,0x51,0x31,0x51,0x34,//???????????????
  57. 0x21,0x31,0x51,0x31,0x21,0x11,0x61,0x21,0x16,//?????????
  58. 0xff};
  59. uchar code hdqb[]={//????
  60. 0x68,0x68,0x31,0x21,0x31,0x21,0x31,0x21,0x31,0x21,0x31,0x21,0x31,0x21,0x31,0x21,0x31,0x21,0x64,0x82,0x92,0x82,0x62,0x54,0x64,0x02,0x12,0x22,0x32,0x52,0x82,0x32,0x34,0x62,0x68,0x68,0x08,0x64,0x82,0x92,0x82,0x62,0x54,0x58,0x58,0x58,0x58,//??
  61. 0x32,0x52,0x52,0x61,0x51,0x32,0x21,0x11,0x61,0x13,//??????????
  62. 0x32,0x12,0x61,0x51,0x12,0x22,0x31,0x23,//????????
  63. 0x32,0x52,0x52,0x61,0x51,0x32,0x21,0x11,0x61,0x21,0x12,//??????????
  64. 0x32,0x12,0x12,0x61,0x51,0x12,0x22,0x12,0x12,        //????????
  65. 0x08,0x04,0x62,0x82,0x82,0x91,0x81,0x62,0x51,0x31,0x52,0x82,//??????????
  66. 0x62,0x51,0x31,0x52,0x81,0x81,0x62,0x51,0x31,0x51,0x23,//?????????
  67. 0x08,0x32,0x52,0x52,0x61,0x51,0x32,0x21,0x61,0x13,//??????????
  68. 0x32,0x12,0x11,0x22,0x11,0x18,0x18,//?????
  69. 0x32,0x52,0x52,0x61,0x51,0x32,0x21,0x11,0x61,0x13,//??????????
  70. 0x32,0x11,0x11,0x12,0x61,0x51,0x12,0x22,0x31,0x23,//????????       
  71. 0x32,0x52,0x51,0x62,0x51,0x32,0x21,0x11,0x61,0x21,0x12,//??????????
  72. 0x32,0x11,0x11,0x12,0x61,0x51,0x12,0x22,0x12,0x12,//?????????
  73. 0x08,0x04,0x62,0x82,0x82,0x91,0x81,0x62,0x51,0x31,0x52,0x82,//??????????
  74. 0x62,0x51,0x31,0x52,0x82,0x62,0x51,0x31,0x61,0x53,//??????????
  75. 0x62,0x82,0x82,0x91,0x81,0x62,0x51,0x31,0x51,0xA2,0x91,//??????????
  76. 0x91,0x81,0x82,0x81,0x82,0x61,0x61,0x53,0x54,//?????
  77. 0xD1,0xC1,0xA1,0xC1,0xA1,0x91,0x81,0x91,//??
  78. 0x32,0x52,0x52,0x61,0x51,0x32,0x21,0x11,0x61,0x13,//??????????
  79. 0x32,0x12,0x12,0x61,0x51,0x11,0x22,0x31,0x24,//????????
  80. 0x32,0x52,0x52,0x61,0x51,0x32,0x21,0x11,0x51,0x61,0x11,0x31,//????????????
  81. 0x34,0x11,0x32,0x11,0x18,0x18,//???
  82. 0x08,0x08,0x38,0x58,0x36,0x22,0x18, 0xFF//?!?!
  83. };
  84. uchar code tzdn[]={//????
  85. 0xC6,0xC6,0xC6,0xA2,0xB2,0xC5,0xE6,0xD6,0xD6,0x02,0x92,0xA2,0xB6,0xB6,0xB6,0xB2,0xA2,0x82,0x86,0x86,//??
  86. 0x52,0x52,0x52,0x32,0x42,0x52,0x56,0x76,//????????
  87. 0x62,0x62,0x62,0x62,0x42,0x62,0x56,0x56,//???????
  88. 0x52,0x52,0x52,0x52,0x72,0x62,0x52,0x44,//????????
  89. 0x46,0x42,0x42,0x42,0x42,0x33,0x21,0x18,0x18,//???????
  90. 0x82,0x82,0x82,0x82,0x52,0x62,0x84,0x82,0xA6,//?????????
  91. 0x92,0x92,0x92,0x93,0x81,0x72,0x66,0x66,//???????
  92. 0x72, 0x72, 0x72, 0x72, 0x72,0x82,0x96,0x56,//????????
  93. 0x72,0x72,0x82,0x92,0x82,0x72,0x86,0x86,0xFF//???????
  94. };
  95. uchar code lnh[]={ //???
  96. 0x52,0x51,0x51,0x52,0x52,0x52,0x61,0x51,0x52,0x42,0x32,0x61,0x71,0x72,0x81,0x72,
  97. 0x52,//??????????????
  98. 0x02,0x51,0x51,0x62,0x11,0x11,0x12,0x22,0x32,0x52,//????????
  99. 0x02,0x01,0x51,0x42,0x51,0x21,0x22,0x12,0x24,0x04,//??????
  100. 0x52,0x51,0x51,0x52,0x52,0x52,0x61,0x51,0x52,0x42,0x32,0x61,0x71,0x72,0x82,0x72,0x52,//??????????????
  101. 0x02,0x51,0x51,0x62,0x11,0x11,0x12,0x22,0x32,0x52,//????????
  102. 0x02,0x01,0x51,0x42,0x51,0x21,0x22,0x12,0x14,//??????
  103. 0x82,0x72,0x62,0x71,0x81,0x82,0x92,0xA2,0x72,//????????
  104. 0x02,0x51,0x51,0x62,0xA1,0x91,0x92,0x82,0x84,//???????
  105. 0x82,0x72,0x62,0x71,0x81,0x82,0x92,0xA2,0x72,//????????
  106. 0x2,0x51,0x51,0x62,0xA1,0x91,0x92,0x82,0x88,0x08,//???????
  107. 0x08,0x04,0x02,0x81,0x81,0xC2,0xC1,0xC1,0xC2,0xC2,0xC2,0x71,0x71,//?????????
  108. 0x72,0x71,0x71,0xC2,0xC1,0xC1,0xC2,0xD2,0xC2,0x81,0x81,//?????????
  109. 0x82,0x81,0x91,0xA2,0x91,0xB2,0xA2,0xA2,0x91,0xB1,0xB2,0xA2,0xA2,0x91,0xB1,0xB2,0xA2,0xC4,//???????????????
  110. 0x02,0x81,0x81,0xC2,0xC1,0xC1,0xC2,0xC2,0xC2,0x71,0x71,//?????????
  111. 0x72,0x71,0x71,0xC2,0xC1,0xC1,0xC2,0xD2,0xC2,0x81,0x81,//?????????
  112. 0x82,0x81,0x91,0xA2,0x91,0xB1,0xB2,0xA2,0xA2,0x91,0x81,0x82,0x72,0x88, 0xFF//???????????
  113. };
  114. //----------------------------????????????---------------------------
  115. //??11.0592M???
  116. uchar code chuzhi[]={0xff,0xff,//??
  117. 0xFC,0x8E,0xFC,0xED,0xFD,0x43,0xFD,0x6A,0xFD,0xB3,0xFD,0xF3,0xFE,0x2D,//??C?1-7
  118. 0xFE,0x47,0xFE,0x76,0xFE,0xA1,0xFE,0xC7,0xFE,0xD9,0xFE,0xF9,0xFF,0x16//???1-7
  119. };
  120. uchar yinyue[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x3f,0x9f,0xcf,0xe7,0xf3,0xf9,0xfc};
  121. //?????????LED??
  122. void delay1(uint z);//??1MS
  123. void delay(uint z);//??165MS,??????
  124. void song();
  125. void beep();//??????
  126. main()
  127. {        uchar x;
  128.         count1=0;//??????
  129.         count2=1;//?????
  130.         EA=1;//????
  131.         EX0=1;//?????0
  132.         IT0=1;//????0???????
  133.         EX1=1;//?????1
  134.         IT1=1;//????1???????
  135.         TMOD=0x61;//???0?????1,???1?????2
  136.         TH0=0;//T0???
  137.         TL0=0;
  138.         ET0=1;
  139.         TH1= 0XFF;//T1???
  140.     TL1=0xff;
  141.         TR1= 1;//?T1??
  142.     ET1=1;
  143.     PT1=1;//?T1????
  144.         while(1)
  145.         {
  146.                 if(count1!=0)
  147.                 {
  148.                         switch(count1)//?????
  149.                         {
  150.                                 case 1:
  151.                                         for(x=0;x<14;x++)
  152.                                         {
  153.                                                 P1=huayang1[x];
  154.                                                 beep();
  155.                                                 delay1(300);
  156.                                                 if(count1!=1)
  157.                                                         break;       
  158.                                         }
  159.                                 break;
  160.                                 case 2:
  161.                                         for(x=0;x<16;x++)
  162.                                         {
  163.                                                 P1=huayang2[x];
  164.                                                 beep();
  165.                                                 delay1(300);
  166.                                                 if(count1!=2)
  167.                                                         break;       
  168.                                         }
  169.                                 break;
  170.                                 case 3:
  171.                                         for(x=0;x<8;x++)
  172.                                         {
  173.                                                 P1=huayang3[x];
  174.                                                 beep();
  175.                                                 delay1(300);
  176.                                                 if(count1!=3)
  177.                                                         break;       
  178.                                         }
  179.                                 break;
  180.                             case 4:
  181.                                         for(x=0;x<14;x++)
  182.                                         {
  183.                                                 P1=huayang4[x];
  184.                                                 beep();
  185.                                                 delay1(300);
  186.                                                 if(count1!=4)
  187.                                                         break;       
  188.                                         }
  189.                                 break;        
  190.                         }
  191.                 }
  192.                 else      //??????
  193.                 {
  194.                         song();
  195.                         delay1(1000);
  196.                 }
  197.         }
  198. }
  199. void int0() interrupt 0
  200. {
  201. count2=0;
  202.         i=0;
  203.     EA=0;//????
  204.         delay1(1);//??
  205.         if(key1==0)
  206.         {
  207.                 EA = 0;
  208.                 count2=0;//???????
  209.                 count3=0;
  210.                 TR0=0;
  211.                 count1++;
  212.                 if(count1==5)
  213.                 count1=1;
  214.         }
  215.         EA=1;//????
  216. }
  217. void int1() interrupt 2
  218. {
  219.         EA=0;//????
  220.         delay1(1);//??
  221.         if(key2==0)
  222.         {
  223.                 count1=0;//??????
  224.                 TR0=1;
  225.                 i=0;//?????
  226.                 count2++;
  227.                 if(count2==6)
  228.                         count2=1;
  229.         }
  230.         EA=1;//????
  231. }
  232. void timer0() interrupt 1         //????????
  233. {
  234.         TH0=timeh;
  235.         TL0=timel;
  236.         fm=~fm;
  237. }
  238. void timer1()interrupt 3   //??count3??
  239. {
  240.   delay1(5);
  241.   if(key3==0)
  242.    {
  243.     count3++;
  244.         if(count3==3)
  245.         count3=1;
  246.    }
  247. }
  248. void song()
  249. {
  250.         uint temp;
  251.         uchar jp;//jp???
  252.          i=0;
  253.         while(1)
  254.         {
  255.                 if(count3 == 1)//??
  256.                 {
  257.                         TR0 = 0;
  258.                 }
  259.                 if(count3 == 0 || count3 == 2)//??
  260.                 {
  261.                         EA = 1;
  262.                         TR0 = 1;
  263.                         if(count2==0)       
  264.                                 break;
  265.                         if(count2==1)        //??
  266.                                  temp=qnzl[i];
  267.                         if(count2==2)
  268.                                 temp=jmszl[i];
  269.                 if(count2==3)
  270.                                 temp=hdqb[i];
  271.                         if(count2==4)
  272.                                 temp=tzdn[i];
  273.                         if(count2==5)
  274.                                 temp=lnh[i];
  275.                         if(temp==0xff)
  276.                              break;
  277.                         jp=temp/16; //????4?
  278.                         P1=yinyue[jp];
  279.                         if(jp!=0)
  280.                         {
  281.                         timeh=chuzhi[jp*2];
  282.                         timel=chuzhi[jp*2+1];
  283.                         }
  284.                         else
  285.                         {
  286.                         TR0=0;
  287.                         fm=1;//????
  288.                         }
  289.                         delay(temp%16); //????4?
  290.                         TR0=0;         //??????10MS
  291.                         fm=1;
  292.                         delay1(10);
  293.                         TR0=1;
  294.                         i++;
  295.                 }
  296.         }
  297.         i=0;
  298.         TR0=0;
  299.         fm=1;
  300. }
  301. void delay(uint z)        //??165MS,??????
  302. {        uint x,y;
  303.         for(x=z;x>0;x--)
  304.                  for(y=19000;y>0;y--);
  305. }
  306. void delay1(uint z)        //??1MS
  307. {        uint x,y;
  308.         for(x=z;x>0;x--)
  309.                  for(y=112;y>0;y--);
  310. }
  311. void beep()        //??????
  312. {        uchar i;
  313.         for(i=0;i<50;i++)
  314.         {        fm=~fm;
  315.                 delay1(1);
  316.         }
  317.         fm=1;
  318. }
复制代码




评分

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

查看全部评分

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

使用道具 举报

沙发
ID:156135 发表于 2016-12-20 11:31 | 只看该作者
请问这个可以实现三首歌切换跟重播吗
回复

使用道具 举报

板凳
ID:254549 发表于 2017-11-28 10:22
这个图为什么不可以下载啊?

地板
ID:425182 发表于 2018-11-20 00:01 | 只看该作者
请问代码中count3和key3是干什么用的?
回复

使用道具 举报

5#
ID:434268 发表于 2018-11-27 20:20 来自手机 | 只看该作者
感谢分享
回复

使用道具 举报

6#
ID:436667 发表于 2018-12-1 10:21 | 只看该作者
请问有仿真吗,没看到呀
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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