找回密码
 立即注册

QQ登录

只需一步,快速开始

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

51单片机彩灯控制器仿真及程序带音乐和加速减速功能

[复制链接]
跳转到指定楼层
楼主
大学时做的设计,基于51单片机彩灯控制器。。。缺少文件
下面是仿真电路及源程序:(proteus仿真工程文件可到本帖附件中下载)


有3个按钮 模式键 加速键 减速键.可以播放音乐



单片机源程序如下:
  1. #include <REG52.H>
  2. #include "SoundPlay.h"
  3. unsigned char RunMode;
  4.         unsigned char Key;

  5. //*****************************Music******************************************************
  6. //挥着翅膀的女孩
  7. unsigned char code Music_Girl[]={ 0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x03,
  8.                                   0x16,0x03, 0x17,0x03, 0x17,0x03, 0x17,0x03, 0x18,0x03,
  9.                                   0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x02, 0x18,0x03,
  10.                                   0x17,0x03, 0x15,0x02, 0x18,0x03, 0x17,0x03, 0x18,0x02,
  11.                                   0x10,0x03, 0x15,0x03, 0x16,0x02, 0x15,0x03, 0x16,0x03,
  12.                                   0x17,0x02, 0x17,0x03, 0x18,0x03, 0x19,0x02, 0x1A,0x03,
  13.                                   0x1B,0x03, 0x1F,0x03, 0x1F,0x03, 0x17,0x03, 0x18,0x03,
  14.                                   0x19,0x02, 0x16,0x03, 0x17,0x03, 0x18,0x03, 0x17,0x03,
  15.                                   0x18,0x03, 0x1F,0x03, 0x1F,0x02, 0x16,0x03, 0x17,0x03,
  16.                                   0x18,0x03, 0x17,0x03, 0x18,0x03, 0x20,0x03, 0x20,0x02,
  17.                                   0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03,
  18.                                   0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03,
  19.                                   0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66,
  20.                                   0x1A,0x03, 0x19,0x03, 0x15,0x03, 0x15,0x03, 0x17,0x03,
  21.                                   0x16,0x66, 0x17,0x04, 0x18,0x04, 0x18,0x03, 0x19,0x03,
  22.                                   0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x20,0x03, 0x21,0x03,
  23.                                   0x20,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66, 0x1F,0x03,
  24.                                   0x1B,0x03, 0x19,0x03, 0x19,0x03, 0x15,0x03, 0x1A,0x66,
  25.                                   0x1A,0x03, 0x19,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03,
  26.                                   0x1F,0x00, 0x1A,0x03, 0x1A,0x03, 0x1A,0x03, 0x1B,0x03,
  27.                                   0x1B,0x03, 0x1A,0x03, 0x19,0x03, 0x19,0x02, 0x17,0x03,
  28.                                   0x15,0x17, 0x15,0x03, 0x16,0x03, 0x17,0x03, 0x18,0x03,
  29.                                   0x17,0x04, 0x18,0x0E, 0x18,0x03, 0x17,0x04, 0x18,0x0E,
  30.                                   0x18,0x66, 0x17,0x03, 0x18,0x03, 0x17,0x03, 0x18,0x03,
  31.                                   0x20,0x03, 0x20,0x02, 0x1F,0x03, 0x1B,0x03, 0x1F,0x66,
  32.                                   0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03, 0x1B,0x03,
  33.                                   0x1F,0x66, 0x1F,0x04, 0x1B,0x0E, 0x1B,0x03, 0x19,0x03,
  34.                                   0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03,
  35.                                   0x15,0x03, 0x15,0x03, 0x17,0x03, 0x16,0x66, 0x17,0x04,
  36.                                   0x18,0x04, 0x18,0x03, 0x19,0x03, 0x1F,0x03, 0x1B,0x03,
  37.                                   0x1F,0x66, 0x20,0x03, 0x21,0x03, 0x20,0x03, 0x1F,0x03,
  38.                                   0x1B,0x03, 0x1F,0x66, 0x1F,0x03, 0x1B,0x03, 0x19,0x03,
  39.                                   0x19,0x03, 0x15,0x03, 0x1A,0x66, 0x1A,0x03, 0x19,0x03,
  40.                                   0x19,0x03, 0x1F,0x03, 0x1B,0x03, 0x1F,0x00, 0x18,0x02,
  41.                                   0x18,0x03, 0x1A,0x03, 0x19,0x0D, 0x15,0x03, 0x15,0x02,
  42.                                   0x18,0x66, 0x16,0x02, 0x17,0x02, 0x15,0x00, 0x00,0x00};
  43. //同一首歌
  44. unsigned char code Music_Same[]={ 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x66, 0x18,0x03,
  45.                                   0x17,0x02, 0x15,0x02, 0x16,0x01, 0x15,0x02, 0x10,0x02,
  46.                                   0x15,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x02,
  47.                                   0x17,0x03, 0x18,0x03, 0x19,0x02, 0x15,0x02, 0x18,0x66,
  48.                                   0x17,0x03, 0x19,0x02, 0x16,0x03, 0x17,0x03, 0x16,0x00,
  49.                                   0x17,0x01, 0x19,0x02, 0x1B,0x02, 0x1B,0x70, 0x1A,0x03,
  50.                                   0x1A,0x01, 0x19,0x02, 0x19,0x03, 0x1A,0x03, 0x1B,0x02,
  51.                                   0x1A,0x0D, 0x19,0x03, 0x17,0x00, 0x18,0x66, 0x18,0x03,
  52.                                   0x19,0x02, 0x1A,0x02, 0x19,0x0C, 0x18,0x0D, 0x17,0x03,
  53.                                   0x16,0x01, 0x11,0x02, 0x11,0x03, 0x10,0x03, 0x0F,0x0C,
  54.                                   0x10,0x02, 0x15,0x00, 0x1F,0x01, 0x1A,0x01, 0x18,0x66,
  55.                                   0x19,0x03, 0x1A,0x01, 0x1B,0x02, 0x1B,0x03, 0x1B,0x03,
  56.                                   0x1B,0x0C, 0x1A,0x0D, 0x19,0x03, 0x17,0x00, 0x1F,0x01,
  57.                                   0x1A,0x01, 0x18,0x66, 0x19,0x03, 0x1A,0x01, 0x10,0x02,
  58.                                   0x10,0x03, 0x10,0x03, 0x1A,0x0C, 0x18,0x0D, 0x17,0x03,
  59.                                   0x16,0x00, 0x0F,0x01, 0x15,0x02, 0x16,0x02, 0x17,0x70,
  60.                                   0x18,0x03, 0x17,0x02, 0x15,0x03, 0x15,0x03, 0x16,0x66,
  61.                                   0x16,0x03, 0x16,0x02, 0x16,0x03, 0x15,0x03, 0x10,0x02,
  62.                                   0x10,0x01, 0x11,0x01, 0x11,0x66, 0x10,0x03, 0x0F,0x0C,
  63.                                   0x1A,0x02, 0x19,0x02, 0x16,0x03, 0x16,0x03, 0x18,0x66,
  64.                                   0x18,0x03, 0x18,0x02, 0x17,0x03, 0x16,0x03, 0x19,0x00,
  65.                                   0x00,0x00 };
  66. //两只蝴蝶                                 
  67. unsigned char code Music_Two[] ={ 0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03,
  68.                                   0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02,
  69.                                   0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03,
  70.                                   0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x16,0x03,
  71.                                   0x17,0x01, 0x16,0x03, 0x17,0x03, 0x16,0x03, 0x15,0x01,
  72.                                   0x10,0x03, 0x15,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03,
  73.                                   0x16,0x03, 0x15,0x03, 0x10,0x03, 0x15,0x03, 0x16,0x01,
  74.                                   0x17,0x03, 0x16,0x03, 0x17,0x01, 0x16,0x03, 0x17,0x03,
  75.                                   0x16,0x03, 0x15,0x01, 0x10,0x03, 0x15,0x03, 0x16,0x02,
  76.                                   0x16,0x0D, 0x17,0x03, 0x16,0x03, 0x15,0x03, 0x10,0x03,
  77.                                   0x10,0x0E, 0x15,0x04, 0x0F,0x01, 0x17,0x03, 0x19,0x03,
  78.                                   0x19,0x01, 0x19,0x03, 0x1A,0x03, 0x19,0x03, 0x17,0x01,
  79.                                   0x16,0x03, 0x16,0x03, 0x16,0x02, 0x16,0x0D, 0x17,0x03,
  80.                                   0x16,0x03, 0x15,0x03, 0x10,0x03, 0x10,0x0D, 0x15,0x00,
  81.                                   0x19,0x03, 0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03,
  82.                                   0x1B,0x03, 0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03,
  83.                                   0x16,0x0D, 0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03,
  84.                                   0x1A,0x02, 0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03,
  85.                                   0x16,0x01, 0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03,
  86.                                   0x19,0x02, 0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E,
  87.                                   0x1B,0x04, 0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E,
  88.                                   0x1B,0x04, 0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03,
  89.                                   0x17,0x0D, 0x16,0x03, 0x17,0x03, 0x19,0x01, 0x19,0x03,
  90.                                   0x19,0x03, 0x1A,0x03, 0x1F,0x03, 0x1B,0x03, 0x1B,0x03,
  91.                                   0x1A,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x03, 0x16,0x03,
  92.                                   0x17,0x01, 0x17,0x03, 0x17,0x03, 0x19,0x03, 0x1A,0x02,
  93.                                   0x1A,0x02, 0x10,0x03, 0x17,0x0D, 0x16,0x03, 0x16,0x01,
  94.                                   0x17,0x03, 0x19,0x03, 0x19,0x03, 0x17,0x03, 0x19,0x03,
  95.                                   0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
  96.                                   0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
  97.                                   0x17,0x16, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
  98.                                   0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03, 0x0F,0x02,
  99.                                   0x10,0x03, 0x15,0x00, 0x00,0x00 };
  100. //***********************************************************************************

  101. //**********************************System Fuction*************************************************
  102. void Delay1ms(unsigned int count)
  103. {
  104.         unsigned int i,j;
  105.         for(i=0;i<count;i++)
  106.         for(j=0;j<120;j++);
  107. }

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

  110. void Display(unsigned char Value)
  111. {
  112.         P3 = LEDDisplayCode[Value];
  113. }

  114. void LEDFlash(unsigned char Count)
  115. {
  116.         unsigned char i;
  117.         bit Flag;
  118.         for(i = 0; i<Count;i++)
  119.         {
  120.                 Flag = !Flag;
  121.                 if(Flag)
  122.                         Display(RunMode);
  123.                 else
  124.                         Display(0x10);
  125.                 Delay1ms(100);
  126.         }
  127.         Display(RunMode);
  128. }

  129. unsigned char GetKey(void)
  130. {
  131.         unsigned char KeyTemp,CheckValue,Key = 0x00;
  132.         CheckValue = P2&0x32;
  133.         if(CheckValue==0x32)
  134.                 return 0x00;
  135.         
  136.         Delay1ms(10);
  137.         KeyTemp = P2&0x32;
  138.         if(KeyTemp==CheckValue)
  139.                 return 0x00;

  140.         if(!(CheckValue&0x02))
  141.                 Key|=0x01;
  142.         if(!(CheckValue&0x10))
  143.                 Key|=0x02;
  144.         if(!(CheckValue&0x20))
  145.                 Key|=0x04;
  146.         return Key;
  147. }

  148. unsigned int TimerCount,SystemSpeed,SystemSpeedIndex;

  149. unsigned int code SpeedCode[]={   1,   2,   3,   5,   8,  10,  14,  17,  20,  30,
  150.                                                              40,  50,  60,  70,  80,  90, 100, 120, 140, 160,
  151.                                                                 180, 200, 300, 400, 500, 600, 700, 800, 900,1000};//30
  152. void SetSpeed(unsigned char Speed)
  153. {
  154.         SystemSpeed =SpeedCode[Speed];
  155. }

  156. void LEDShow(unsigned int LEDStatus)
  157. {
  158.         P1 = ~(LEDStatus&0x00FF);
  159.         P0 = ~((LEDStatus>>8)&0x00FF);
  160. }

  161. //Mode 0
  162. unsigned int LEDIndex = 0;
  163. bit LEDDirection = 1,LEDFlag = 1;
  164. void Mode_0(void)
  165. {
  166.         LEDShow(0x0001<<LEDIndex);
  167.         LEDIndex = (LEDIndex+1)%16;
  168. }
  169. //Mode 1
  170. void Mode_1(void)
  171. {
  172.         LEDShow(0x8000>>LEDIndex);
  173.         LEDIndex = (LEDIndex+1)%16;
  174. }
  175. //Mode 2
  176. void Mode_2(void)
  177. {
  178.         if(LEDDirection)
  179.                 LEDShow(0x0001<<LEDIndex);
  180.         else
  181.                 LEDShow(0x8000>>LEDIndex);
  182.         if(LEDIndex==15)
  183.                 LEDDirection = !LEDDirection;
  184.    LEDIndex = (LEDIndex+1)%16;
  185. }
  186. //Mode 3
  187. void Mode_3(void)
  188. {
  189.         if(LEDDirection)
  190.                 LEDShow(~(0x0001<<LEDIndex));
  191.         else
  192.                 LEDShow(~(0x8000>>LEDIndex));
  193.         if(LEDIndex==15)
  194.                 LEDDirection = !LEDDirection;
  195.    LEDIndex = (LEDIndex+1)%16;
  196. }

  197. //Mode 4
  198. void Mode_4(void)
  199. {
  200.         if(LEDDirection)
  201.         {
  202.                 if(LEDFlag)
  203.                         LEDShow(0xFFFE<<LEDIndex);
  204.                    else
  205.                         LEDShow(~(0x7FFF>>LEDIndex));
  206.         }
  207.         else
  208.         {
  209.                 if(LEDFlag)
  210.                         LEDShow(0x7FFF>>LEDIndex);
  211.                 else
  212.                         LEDShow(~(0xFFFE<<LEDIndex));
  213.         }
  214.         if(LEDIndex==15)
  215.         {
  216.                 LEDDirection = !LEDDirection;
  217.                 if(LEDDirection)        LEDFlag = !LEDFlag;
  218.         }
  219.            LEDIndex = (LEDIndex+1)%16;
  220. }

  221. //Mode 5
  222. void Mode_5(void)
  223. {
  224.         if(LEDDirection)
  225.                 LEDShow(0x000F<<LEDIndex);
  226.         else
  227.                 LEDShow(0xF000>>LEDIndex);
  228.         if(LEDIndex==15)
  229.                 LEDDirection = !LEDDirection;
  230.     LEDIndex = (LEDIndex+1)%16;
  231. }

  232. //Mode 6
  233. void Mode_6(void)
  234. {
  235.         if(LEDDirection)
  236.                 LEDShow(~(0x000F<<LEDIndex));
  237.         else
  238.                 LEDShow(~(0xF000>>LEDIndex));
  239.         if(LEDIndex==15)
  240.                 LEDDirection = !LEDDirection;
  241.            LEDIndex = (LEDIndex+1)%16;
  242. }

  243. //Mode 7
  244. void Mode_7(void)
  245. {
  246.         if(LEDDirection)
  247.                 LEDShow(0x003F<<LEDIndex);
  248.         else
  249.                 LEDShow(0xFC00>>LEDIndex);
  250.         if(LEDIndex==9)
  251.                 LEDDirection = !LEDDirection;
  252.     LEDIndex = (LEDIndex+1)%10;
  253. }

  254. //Mode 8
  255. void Mode_8(void)
  256. {
  257.         LEDShow(++LEDIndex);
  258. }

  259. void TimerEventRun(void)
  260. {
  261.         if(RunMode==0x00)
  262.         {
  263.                 Mode_0();        
  264.         }
  265.         else if(RunMode ==0x01)
  266.         {
  267.                 Mode_1();
  268.         }
  269.         else if(RunMode ==0x02)
  270.         {
  271.                 Mode_2();
  272.         }
  273.         else if(RunMode ==0x03)
  274.         {
  275.                 Mode_3();
  276.         }
  277.         else if(RunMode ==0x04)
  278.         {
  279.                 Mode_4();
  280.         }
  281.         else if(RunMode ==0x05)
  282.         {
  283.                 Mode_5();
  284.         }
  285.         else if(RunMode ==0x06)
  286.         {
  287.                 Mode_6();
  288.         }
  289.         else if(RunMode ==0x07)
  290.         {
  291.                 Mode_7();
  292.         }
  293.         else if(RunMode ==0x08)
  294.         {
  295.                 Mode_8();
  296.         }
  297. }

  298. unsigned char MusicIndex = 0;
  299. void KeyDispose(unsigned char Key)
  300. {
  301.         if(Key&0x01)
  302.         {
  303.                 LEDDirection = 1;
  304.                 LEDIndex = 0;
  305.                 LEDFlag = 1;
  306.                 RunMode = (RunMode+1)%9;
  307.                 Display(RunMode);
  308.         }
  309.         if(Key&0x02)
  310.         {
  311.                 if(SystemSpeedIndex>0)
  312.                 {
  313.                         --SystemSpeedIndex;
  314.                         SetSpeed(SystemSpeedIndex);
  315.                 }
  316.                 else
  317.                 {
  318.                         LEDFlash(6);
  319.                 }
  320.         }
  321.         if(Key&0x04)
  322.         {
  323.                 if(SystemSpeedIndex<28)
  324.                 {
  325.                         ++SystemSpeedIndex;
  326.                         SetSpeed(SystemSpeedIndex);
  327.                 }
  328.                 else
  329.                 {
  330.                         LEDFlash(6);
  331.                 }
  332.         }        
  333. }
  334. void InitialCPU(void)
  335. {
  336.         RunMode = 0x00;
  337.         TimerCount = 0;
  338.         SystemSpeedIndex = 10;

  339.         P1 = 0x00;
  340.         P0 = 0x00;
  341.         P2 = 0xFF;
  342.         P3 = 0x00;
  343.         Delay1ms(500);
  344.         P1 = 0xFF;
  345.         P0 = 0xFF;
  346.         P2 = 0xFF;
  347.         P3 = 0xFF;
  348.         SetSpeed(SystemSpeedIndex);
  349.         Display(RunMode);
  350. }


  351. void InitialSound(void)
  352. {
  353.         BeepIO = 0;
  354.         Sound_Temp_TH1 = (65535-(1/1200)*SYSTEM_OSC)/256;        // 计算TL1应装入的初值         (10ms的初装值)
  355.         Sound_Temp_TL1 = (65535-(1/1200)*SYSTEM_OSC)%256;        // 计算TH1应装入的初值
  356.         TH1 = Sound_Temp_TH1;
  357.         TL1 = Sound_Temp_TL1;
  358.         TMOD  |= 0x11;
  359.         ET0    = 1;
  360.         ET1           = 0;
  361.         TR0           = 0;
  362.         TR1    = 0;
  363. }

  364. void InitialTimer2(void)
  365. {
  366.         T2CON  = 0x00;                        //16 Bit Auto-Reload Mode
  367.          TH2 = RCAP2H = 0xFC;          //重装值,初始值        TL2 = RCAP2L = 0x18;
  368.         ET2=1;                                        //定时器 2 中断允许
  369. ……………………

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

所有资料51hei提供下载:
仿真及源程序.zip (31.56 KB, 下载次数: 61)


评分

参与人数 2黑币 +60 收起 理由
持勤补拙 + 10 赞一个!
admin + 50 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

沙发
ID:130345 发表于 2017-10-22 00:13 | 只看该作者
感谢分享,做个试试
回复

使用道具 举报

板凳
ID:217754 发表于 2017-10-22 15:04 来自手机 | 只看该作者
感谢分享,这个看着简单些,下来做个试试
回复

使用道具 举报

地板
ID:259588 发表于 2017-12-10 09:57 来自手机 | 只看该作者
都是糊弄人了
回复

使用道具 举报

5#
ID:367827 发表于 2018-7-9 11:33 | 只看该作者
感谢!!!
回复

使用道具 举报

6#
ID:1098157 发表于 2024-4-20 11:36 | 只看该作者
老哥,你sunplay.h没给
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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