找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机多变循环彩灯 带音乐 16led proteus仿真原理图及源程序

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



仿真工程文件及所有完整程序等资料下载地址:http://www.51hei.com/bbs/dpj-56304-1.html

单片机源程序:

  1. #include <REG52.H>
  2. #include "SoundPlay.h"

  3. unsigned char RunMode;

  4. //**********************************System Fuction*************************************************
  5. void Delay1ms(unsigned int count)
  6. {
  7.         unsigned int i,j;
  8.         for(i=0;i<count;i++)
  9.         for(j=0;j<120;j++);
  10. }

  11. unsigned char code LEDDisplayCode[] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,        //0~7
  12.                                                           0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};

  13. void Display(unsigned char Value)
  14. {
  15.         P3 = LEDDisplayCode[Value];
  16. }

  17. void LEDFlash(unsigned char Count)
  18. {
  19.         unsigned char i;
  20.         bit Flag;
  21.         for(i = 0; i<Count;i++)
  22.         {
  23.                 Flag = !Flag;
  24.                 if(Flag)
  25.                         Display(RunMode);
  26.                 else
  27.                         Display(0x10);
  28.                 Delay1ms(100);
  29.         }
  30.         Display(RunMode);
  31. }

  32. unsigned char GetKey(void)
  33. {
  34.         unsigned char KeyTemp,CheckValue,Key = 0x00;
  35.         CheckValue = P2&0x32;
  36.         if(CheckValue==0x32)
  37.                 return 0x00;
  38.         
  39.         Delay1ms(10);
  40.         KeyTemp = P2&0x32;
  41.         if(KeyTemp==CheckValue)
  42.                 return 0x00;

  43.         if(!(CheckValue&0x02))
  44.                 Key|=0x01;
  45.         if(!(CheckValue&0x10))
  46.                 Key|=0x02;
  47.         if(!(CheckValue&0x20))
  48.                 Key|=0x04;
  49.         return Key;
  50. }

  51. unsigned int Timer0Count,SystemSpeed,SystemSpeedIndex;
  52. void InitialTimer2(void)
  53. {
  54.         T2CON  = 0x00;                        //16 Bit Auto-Reload Mode
  55.          TH2 = RCAP2H = 0xFC;          //重装值,初始值        TL2 = RCAP2L = 0x18;
  56.         ET2=1;                                        //定时器 2 中断允许
  57.         TR2 = 1;                                //定时器 2 启动
  58.         EA=1;
  59. }

  60. unsigned int code SpeedCode[]={   1,   2,   3,   5,   8,  10,  14,  17,  20,  30,
  61.                                                              40,  50,  60,  70,  80,  90, 100, 120, 140, 160,
  62.                                                                 180, 200, 300, 400, 500, 600, 700, 800, 900,1000};//30
  63. void SetSpeed(unsigned char Speed)
  64. {
  65.         SystemSpeed =SpeedCode[Speed];
  66. }

  67. void LEDShow(unsigned int LEDStatus)
  68. {
  69.         P1 = ~(LEDStatus&0x00FF);
  70.         P0 = ~((LEDStatus>>8)&0x00FF);
  71. }

  72. void InitialCPU(void)
  73. {
  74.         RunMode = 0x00;
  75.         Timer0Count = 0;
  76.         SystemSpeedIndex = 9;

  77.         P1 = 0x00;
  78.         P0 = 0x00;
  79.         P2 = 0xFF;
  80.         P3 = 0x00;
  81.         Delay1ms(500);
  82.         P1 = 0xFF;
  83.         P0 = 0xFF;
  84.         P2 = 0xFF;
  85.         P3 = 0xFF;
  86.         SetSpeed(SystemSpeedIndex);
  87.         Display(RunMode);
  88. }

  89. //Mode 0
  90. unsigned int LEDIndex = 0;
  91. bit LEDDirection = 1,LEDFlag = 1;
  92. void Mode_0(void)
  93. {
  94.         LEDShow(0x0001<<LEDIndex);
  95.         LEDIndex = (LEDIndex+1)%16;
  96. }
  97. //Mode 1
  98. void Mode_1(void)
  99. {
  100.         LEDShow(0x8000>>LEDIndex);
  101.         LEDIndex = (LEDIndex+1)%16;
  102. }
  103. //Mode 2
  104. void Mode_2(void)
  105. {
  106.         if(LEDDirection)
  107.                 LEDShow(0x0001<<LEDIndex);
  108.         else
  109.                 LEDShow(0x8000>>LEDIndex);
  110.         if(LEDIndex==15)
  111.                 LEDDirection = !LEDDirection;
  112.    LEDIndex = (LEDIndex+1)%16;
  113. }
  114. //Mode 3
  115. void Mode_3(void)
  116. {
  117.         if(LEDDirection)
  118.                 LEDShow(~(0x0001<<LEDIndex));
  119.         else
  120.                 LEDShow(~(0x8000>>LEDIndex));
  121.         if(LEDIndex==15)
  122.                 LEDDirection = !LEDDirection;
  123.    LEDIndex = (LEDIndex+1)%16;
  124. }

  125. //Mode 4
  126. void Mode_4(void)
  127. {
  128.         if(LEDDirection)
  129.         {
  130.                 if(LEDFlag)
  131.                         LEDShow(0xFFFE<<LEDIndex);
  132.                    else
  133.                         LEDShow(~(0x7FFF>>LEDIndex));
  134.         }
  135.         else
  136.         {
  137.                 if(LEDFlag)
  138.                         LEDShow(0x7FFF>>LEDIndex);
  139.                 else
  140.                         LEDShow(~(0xFFFE<<LEDIndex));
  141.         }
  142.         if(LEDIndex==15)
  143.         {
  144.                 LEDDirection = !LEDDirection;
  145.                 if(LEDDirection)        LEDFlag = !LEDFlag;
  146.         }
  147.            LEDIndex = (LEDIndex+1)%16;
  148. }

  149. //Mode 5
  150. void Mode_5(void)
  151. {
  152.         if(LEDDirection)
  153.                 LEDShow(0x000F<<LEDIndex);
  154.         else
  155.                 LEDShow(0xF000>>LEDIndex);
  156.         if(LEDIndex==15)
  157.                 LEDDirection = !LEDDirection;
  158.     LEDIndex = (LEDIndex+1)%16;
  159. }

  160. //Mode 6
  161. void Mode_6(void)
  162. {
  163.         if(LEDDirection)
  164.                 LEDShow(~(0x000F<<LEDIndex));
  165.         else
  166.                 LEDShow(~(0xF000>>LEDIndex));
  167.         if(LEDIndex==15)
  168.                 LEDDirection = !LEDDirection;
  169.            LEDIndex = (LEDIndex+1)%16;
  170. }

  171. //Mode 7
  172. void Mode_7(void)
  173. {
  174.         if(LEDDirection)
  175.                 LEDShow(0x003F<<LEDIndex);
  176.         else
  177.                 LEDShow(0xFC00>>LEDIndex);
  178.         if(LEDIndex==9)
  179.                 LEDDirection = !LEDDirection;
  180.     LEDIndex = (LEDIndex+1)%10;
  181. }

  182. //Mode 8
  183. void Mode_8(void)
  184. {
  185.         LEDShow(++LEDIndex);
  186. }

  187. void Timer0EventRun(void)
  188. {
  189.         if(RunMode==0x00)
  190.         {
  191.                 Mode_0();        
  192.         }
  193.         else if(RunMode ==0x01)
  194.         {
  195.                 Mode_1();
  196.         }
  197.         else if(RunMode ==0x02)
  198.         {
  199.                 Mode_2();
  200.         }
  201.         else if(RunMode ==0x03)
  202.         {
  203.                 Mode_3();
  204.         }
  205.         else if(RunMode ==0x04)
  206.         {
  207.                 Mode_4();
  208.         }
  209.         else if(RunMode ==0x05)
  210.         {
  211.                 Mode_5();
  212.         }
  213.         else if(RunMode ==0x06)
  214.         {
  215.                 Mode_6();
  216.         }
  217.         else if(RunMode ==0x07)
  218.         {
  219.                 Mode_7();
  220.         }
  221.         else if(RunMode ==0x08)
  222.         {
  223.                 Mode_8();
  224.         }
  225. }

  226. void Timer2(void) interrupt 5 using 3
  227. {
  228.         TF2 = 0;         //中断标志清除( Timer2 必须软件清标志!)
  229.         if(++Timer0Count>=SystemSpeed)
  230.         {
  231.                 Timer0Count = 0;
  232.                 Timer0EventRun();
  233.            }
  234. }
  235. unsigned char MusicIndex = 0;
  236. void KeyDispose(unsigned char Key)
  237. {
  238.         if(Key&0x01)
  239.         {
  240.                 LEDDirection = 1;
  241.                 LEDIndex = 0;
  242.                 LEDFlag = 1;
  243.                 RunMode = (RunMode+1)%10;
  244.                 Display(RunMode);
  245.                 if(RunMode==0x09)
  246.                         TR2 = 0;
  247.                 else
  248.                         TR2 = 1;
  249.         }
  250.         if(Key&0x02)
  251.         {
  252.                 if(RunMode==0x09)
  253.                 {
  254.                         MusicIndex =(MusicIndex+MUSICNUMBER-1)%MUSICNUMBER;
  255.                 }
  256.                 else
  257.                 {
  258.                         if(SystemSpeedIndex>0)
  259.                         {
  260.                                 --SystemSpeedIndex;
  261.                                 SetSpeed(SystemSpeedIndex);
  262.                         }
  263.                         else
  264.                         {
  265.                                 LEDFlash(6);
  266.                         }
  267.                  }
  268.         }
  269.         if(Key&0x04)
  270.         {
  271.                 if(RunMode==0x09)
  272.                 {
  273.                         MusicIndex =(MusicIndex+1)%MUSICNUMBER;
  274.                 }
  275.                 else
  276.                 {
  277.                         if(SystemSpeedIndex<28)
  278.                         {
  279.                                 ++SystemSpeedIndex;
  280.                                 SetSpeed(SystemSpeedIndex);
  281.                         }
  282.                         else
  283.                         {
  284.                                 LEDFlash(6);
  285.                         }
  286.                    }
  287.         }        
  288. }
  289. //*****************************Music******************************************************
  290. //挥着翅膀的女孩
  291. unsigned char code Music_Girl[]={ 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x03,
  292.                                   0x16,0x03, 0x17,0x03, 0x17,0x03, 0x17,0x03, 0x18,0x03,
  293.                                   0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x02, 0x18,0x03,
  294.                                   0x17,0x03, 0x15,0x02, 0x18,0x03, 0x17,0x03, 0x18,0x02,
  295.                                   0x10,0x03, 0x15,0x03, 0x16,0x02, 0x15,0x03, 0x16,0x03,
  296.                                   0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x1A,0x03,
  297.                                   0x1B,0x03, 0x1F,0x03, 0x1F,0x03, 0x17,0x03, 0x18,0x03,
  298.                                   0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03,
  299.                                   0x18,0x03, 0x1F,0x03, 0x1F,0x02, 0x16,0x03, 0x17,0x03,
  300.                                   0x18,0x03, 0x17,0x03, 0x18,0x03, 0x20,0x03, 0x20,0x02,
  301.                                   0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03,
  302.                                   0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03,
  303.                                   0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66,
  304.                                   0x1A,0x03, 0x19,0x03, 0x15,0x03, 0x15,0x03, 0x17,0x03,
  305.                                   0x16,0x66, 0x17,0x04, 0x18,0x04, 0x18,0x03, 0x19,0x03,
  306.                                   0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03,
  307.                                   0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03,
  308.                                   0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66,
  309.                                   0x1A,0x03, 0x19,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03,
  310.                                   0x1F,0x00, 0x1A,0x03, 0x1A,0x03, 0x1A,0x03, 0x1B,0x03,
  311.                                   0x1B,0x03, 0x1A,0x03, 0x19,0x03, 0x19,0x02, 0x17,0x03,
  312.                                   0x15,0x17, 0x15,0x03, 0x16,0x03, 0x17,0x03, 0x18,0x03,
  313.                                   0x17,0x04, 0x18,0x0E, 0x18,0x03, 0x17,0x04, 0x18,0x0E,
  314.                                   0x18,0x66, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03,
  315.                                   0x20,0x03, 0x20,0x02, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66,
  316.                                   0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03, 0x1B,0x03,
  317.                                   0x1F,0x66, 0x1F,0x04, 0x1B,0x0E, 0x1B,0x03, 0x19,0x03,
  318.                                   0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03,
  319.                                   0x15,0x03, 0x15,0x03, 0x17,0x03, 0x16,0x66, 0x17,0x04,
  320.                                   0x18,0x04, 0x18,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03,
  321.                                   0x1F,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03,
  322.                                   0x1B,0x03, 0x1F,0x66, 0x1F,0x03, 0x1B,0x03, 0x19,0x03,
  323.                                   0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03,
  324.                                   0x19,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x00, 0x18,0x02,
  325.                                   0x18,0x03, 0x1A,0x03, 0x19,0x0D, 0x15,0x03, 0x15,0x02,
  326.                                   0x18,0x66, 0x16,0x02, 0x17,0x02, 0x15,0x00, 0x00,0x00};
  327. //同一首歌
  328. unsigned char code Music_Same[]={ 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x66, 0x18,0x03,
  329.                                   0x17,0x02, 0x15,0x02, 0x16,0x01, 0x15,0x02, 0x10,0x02,
  330.                                   0x15,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x02,
  331.                                   0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x02, 0x18,0x66,
  332.                                   0x17,0x03, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x16,0x00,
  333.                                   0x17,0x01, 0x19,0x02, 0x1B,0x02, 0x1B,0x70, 0x1A,0x03,
  334.                                   0x1A,0x01, 0x19,0x02, 0x19,0x03, 0x1A,0x03, 0x1B,0x02,
  335.                                   0x1A,0x0D, 0x19,0x03, 0x17,0x00, 0x18,0x66, 0x18,0x03,
  336.                                   0x19,0x02, 0x1A,0x02, 0x19,0x0C, 0x18,0x0D, 0x17,0x03,
  337.                                   0x16,0x01, 0x11,0x02, 0x11,0x03, 0x10,0x03, 0x0F,0x0C,
  338.                                   0x10,0x02, 0x15,0x00, 0x1F,0x01, 0x1A,0x01, 0x18,0x66,
  339.                                   0x19,0x03, 0x1A,0x01, 0x1B,0x02, 0x1B,0x03, 0x1B,0x03,
  340.                                   0x1B,0x0C, 0x1A,0x0D, 0x19,0x03, 0x17,0x00, 0x1F,0x01,
  341.                                   0x1A,0x01, 0x18,0x66, 0x19,0x03, 0x1A,0x01, 0x10,0x02,
  342.                                   0x10,0x03, 0x10,0x03, 0x1A,0x0C, 0x18,0x0D, 0x17,0x03,
  343.                                   0x16,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x70,
  344.                                   0x18,0x03, 0x17,0x02, 0x15,0x03, 0x15,0x03, 0x16,0x66,
  345.                                   0x16,0x03, 0x16,0x02, 0x16,0x03, 0x15,0x03, 0x10,0x02,
  346.                                   0x10,0x01, 0x11,0x01, 0x11,0x66, 0x10,0x03, 0x0F,0x0C,
  347.                                   0x1A,0x02, 0x19,0x02, 0x16,0x03, 0x16,0x03, 0x18,0x66,
  348.                                   0x18,0x03, 0x18,0x02, 0x17,0x03, 0x16,0x03, 0x19,0x00,
  349.                                   0x00,0x00 };
  350. //两只蝴蝶                                 
  351. unsigned char code Music_Two[] ={ 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03,
  352.                                   0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02,
  353.                                   0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03,
  354.                                   0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x16,0x03,
  355.                                   0x17,0x01, 0x16,0x03, 0x17,0x03, 0x16,0x03, 0x15,0x01,
  356.                                   0x10,0x03, 0x15,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03,
  357.                                   0x16,0x03, 0x15,0x03, 0x10,0x03, 0x15,0x03, 0x16,0x01,
  358.                                   0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03,
  359.                                   0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02,
  360.                                   0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03,
  361.                                   0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x19,0x03,
  362.                                   0x19,0x01, 0x19,0x03, 0x1A,0x03, 0x19,0x03, 0x17,0x01,
  363.                                   0x16,0x03, 0x16,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03,
  364.                                   0x16,0x03, 0x15,0x03, 0x10,0x03, 0x10,0x0D, 0x15,0x00,
  365.                                   0x19,0x03, 0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03,
  366.                                   0x1B,0x03, 0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03,
  367.                                   0x16,0x0D, 0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03,
  368.                                   0x1A,0x02, 0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03,
  369.                                   0x16,0x01, 0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03,
  370.                                   0x19,0x02, 0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E,
  371.                                   0x1B,0x04, 0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E,
  372.                                   0x1B,0x04, 0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03,
  373.                                   0x17,0x0D, 0x16,0x03, 0x17,0x03, 0x19,0x01, 0x19,0x03,
  374.                                   0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03, 0x1B,0x03,
  375.                                   0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03, 0x16,0x03,
  376.                                   0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03, 0x1A,0x02,
  377.                                   0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x01,
  378.                                   0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03, 0x19,0x03,
  379.                                   0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
  380.                                   0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
  381.                                   0x17,0x16, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
  382.                                   0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03, 0x0F,0x02,
  383.                                   0x10,0x03, 0x15,0x00, 0x00,0x00 };
  384. //***********************************************************************************
  385. unsigned char * SelectMusic(unsigned char SoundIndex)
  386. {
  387.         unsigned char *MusicAddress = 0;
  388.         switch (SoundIndex)
  389.         {        
  390.                 case 0x00:
  391.                         MusicAddress = &Music_Girl[0];        //挥着翅膀的女孩               
  392.                         break;
  393.                 case 0x01:
  394.                         MusicAddress = &Music_Same[0];        //同一首歌               
  395.                         break;
  396.                 case 0x02:
  397.                         MusicAddress = &Music_Two[0];        //两只蝴蝶  
  398.                         break;
  399.                 case 0x03:
  400.                         break;
  401.                 case 0x04:
  402.                         break;
  403.                 case 0x05:
  404.                         break;
  405.                 case 0x06:
  406.                         break;
  407.                 case 0x07:
  408.                         break;
  409.                 case 0x08:
  410.                         break;
  411.                 case 0x09:
  412.                         break;
  413.                 default:break;
  414.         }
  415.         return MusicAddress;
  416. }

  417. void PlayMusic(void)
  418. {        
  419.         Delay1ms(200);
  420.         Play(SelectMusic(MusicIndex),0,3,360);
  421. }
  422. //***********************************************************************************
  423. main()
  424. {
  425.         unsigned char Key;
  426.         InitialCPU();
  427.         InitialSound();
  428.         InitialTimer2();

  429.         while(1)
  430.         {
  431.                 Key = GetKey();
  432.                 if(RunMode==0x09)
  433.                 {
  434.                         PlayMusic();
  435.                 }
  436.                 if(Key!=0x00)
  437.                 {
  438.                         KeyDispose(Key);
  439.                 }
  440.         }
  441. }
复制代码




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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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