标题: 单片机按键控制定时器选播多段音乐程序+Proteus仿真 [打印本页]

作者: 1236222    时间: 2020-12-6 21:08
标题: 单片机按键控制定时器选播多段音乐程序+Proteus仿真
按键控制定时器选播多段音乐,包含程序和电路仿真图


单片机源程序如下:
  1. #include <reg52.h>
  2. #include <intrins.h>
  3. #define uchar unsigned char
  4. #define uint unsigned int

  5. uchar Song_Index = 0,Tone_Index = 0;
  6. sbit SPK = P3^7;
  7. sbit K1 = P1^0;
  8. uchar code DSY_CODE[]=
  9. {
  10.         0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90
  11. };

  12. uchar code HI_LIST[]=
  13. {
  14.         0,266,229,232,233,236,238,240,241,242,244,245,246,247,248
  15. };
  16. uchar code LO_LIST[]=
  17. {
  18.         0,4,13,10,20,3,8,6,2,23,5,26,1,4,3
  19. };

  20. uchar code Song[][50]=
  21. {
  22.         {1,2,3,1,1,2,3,1,3,4,5,3,4,5,3,4,5,5,6,1,2,3,6,2,6,6,2,3,4,5,2,5,2,3,-1},
  23.         {3,3,3,5,6,2,3,4,6,2,6,4,2,1,2,3,6,2,5,6,3,4,5,6,2,3,1,2,3,5,1,2,3,-1},
  24.         {3,2,1,3,2,1,1,2,3,1,3,5,1,5,4,6,2,6,3,4,2,3,6,2,3,4,2,1,1,1,2,2,2,3,3,2,6,3,5,3,5,1,2,6,4,5,-1}
  25. };

  26. uchar code Len[][50]=
  27. {
  28.         {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,2,2,2,2,2,2,2,2,2,2,2,1,1,-1},
  29.         {1,1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,2,2,2,2,1,1,1,1,1,1,1,1,-1},
  30.         {1,1,2,1,1,2,1,1,1,1,1,1,1,1,2,1,1,1,2,1,1,1,1,1,1,1,2,1,2,1,2,1,2,1,1,1,1,1,2,2,-1}
  31. };

  32. void EX0_INT() interrupt 0
  33. {
  34.         TR0 = 0;
  35.         Song_Index = (Song_Index+1)%3;
  36.         Tone_Index = 0;
  37.         P2 = DSY_CODE[Song_Index];
  38. }

  39. void T0_INT() interrupt 1
  40. {
  41.         SPK = !SPK;
  42.         TH0 = HI_LIST[Song[Song_Index][Tone_Index]];
  43.         TL0 = LO_LIST[Song[Song_Index][Tone_Index]];
  44. }

  45. void delay(uint ms)
  46. {
  47.         uchar i;
  48.         while(ms--)
  49.         {
  50.                 for(i=0;i<120;i++);
  51.         }
  52. }

  53. void main()
  54. {
  55.         P2 = 0xc0;
  56.         IE = 0x83;
  57.         TMOD = 0x00;
  58.         IT0 = 1;
  59.         IP = 0x02;
  60.         while(1)
  61.         {
  62.                  while(K1==1);
  63.                 while(K1==0);
  64. ……………………

  65. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
按键控制定时器选播多段音乐.zip (48.71 KB, 下载次数: 27)







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1