找回密码
 立即注册

QQ登录

只需一步,快速开始

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

多彩循环灯单片机程序

[复制链接]
跳转到指定楼层
楼主
ID:143648 发表于 2016-10-20 20:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include <REG52.H>


  2. unsigned char RunMode;

  3. void Delay1ms(unsigned int count)
  4. {
  5.         unsigned int i,j;
  6.         for(i=0;i<count;i++)
  7.         for(j=0;j<120;j++);
  8. }

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

  11. void Display(unsigned char Value)
  12. {
  13.         P2 = LEDDisplayCode[Value];
  14. }

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

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

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

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

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

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

  70. void InitialCPU(void)
  71. {
  72.         RunMode = 0x00;
  73.         Timer0Count = 0;
  74.         SystemSpeedIndex = 20;

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

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

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

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

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

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

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

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

  224. void Timer2(void) interrupt 5 using 3
  225. {
  226.         TF2 = 0;         //中断标志清除( Timer2 必须软件清标志!)
  227.         if(++Timer0Count>=SystemSpeed)
  228.         {
  229.                 Timer0Count = 0;
  230.                 Timer0EventRun();
  231.            }
  232. }
  233. void KeyDispose(unsigned char Key)
  234. {
  235.         if(Key&0x01)
  236.         {
  237.                 LEDDirection = 1;
  238.                 LEDIndex = 0;
  239.                 LEDFlag = 1;
  240.                 RunMode = (RunMode+1)%10;
  241.                 Display(RunMode);
  242.                 if(RunMode==0x09)
  243.                         TR2 = 0;
  244.                 else
  245.                         TR2 = 1;
  246.         }
  247.         if(Key&0x02)
  248.         {
  249.                 if(RunMode==0x09)
  250.                
  251.                 ;
  252.                
  253.                 else
  254.                 {
  255.                         if(SystemSpeedIndex>0)
  256.                         {
  257.                                 --SystemSpeedIndex;
  258.                                 SetSpeed(SystemSpeedIndex);
  259.                         }
  260.                         else
  261.                         {
  262.                                 LEDFlash(6);
  263.                         }
  264.                  }
  265.         }
  266.         if(Key&0x04)
  267.         {
  268.                 if(RunMode==0x09)
  269.       
  270.                 ;
  271.       
  272.                 else
  273.                 {
  274.                         if(SystemSpeedIndex<28)
  275.                         {
  276.                                 ++SystemSpeedIndex;
  277.                                 SetSpeed(SystemSpeedIndex);
  278.                         }
  279.                         else
  280.                         {
  281.                                 LEDFlash(6);
  282.                         }
  283.                    }
  284.         }      
  285. }


  286. main()
  287. {
  288.         unsigned char Key;
  289.         InitialCPU();

  290.         InitialTimer2();

  291.         while(1)
  292.         {
  293.                 Key = GetKey();
  294.                
  295.                 if(Key!=0x00)
  296.                 {
  297.                         KeyDispose(Key);
  298.                 }
  299.         }
  300. }
复制代码


评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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