找回密码
 立即注册

QQ登录

只需一步,快速开始

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

一个单片机多变循环彩灯流水灯的Proteus仿真

[复制链接]
跳转到指定楼层
楼主
多变循环彩灯的一个仿真,有程序代码


单片机源程序如下:
  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.                                  
  294.                                   0x1F,0x02, 0x1B,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
  295.                                   0x17,0x02, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
  296.                                   0x17,0x16, 0x1A,0x03, 0x1A,0x03, 0x1A,0x0E, 0x1B,0x04,
  297.                                   0x1A,0x03, 0x19,0x03, 0x17,0x03, 0x16,0x03, 0x0F,0x02,
  298.                                   0x10,0x03, 0x15,0x00, 0x00,0x00 };
  299. //***********************************************************************************
  300. unsigned char * SelectMusic(unsigned char SoundIndex)
  301. {
  302.         unsigned char *MusicAddress = 0;
  303.         switch (SoundIndex)
  304.         {        
  305.                 case 0x00:
  306.                         MusicAddress = &Music_Girl[0];        //挥着翅膀的女孩               
  307.                         break;
  308.                 case 0x01:
  309.                         MusicAddress = &Music_Same[0];        //同一首歌               
  310. ……………………

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

所有资料51hei提供下载:
多变循环彩灯.zip (311.22 KB, 下载次数: 68)


评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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