找回密码
 立即注册

QQ登录

只需一步,快速开始

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

基于51单片机的跑马灯程序

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

  1. #include <REG52.H>

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



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

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

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

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

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

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

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

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

  71. void InitialCPU(void)
  72. {
  73.         RunMode = 0x00;
  74.         TimerCount = 0;
  75.         SystemSpeedIndex = 10;

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

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

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

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

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

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

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

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

  225. void Timer2(void) interrupt 5 using 3
  226. {
  227.         TF2 = 0;         //中断标志清除( Timer2 必须软件清标志!)
  228.         if(++TimerCount>=SystemSpeed)
  229.         {
  230.                 TimerCount = 0;
  231.                 TimerEventRun();
  232.            }
  233. }
  234. unsigned char MusicIndex = 0;
  235. void KeyDispose(unsigned char Key)
  236. {
  237.         if(Key&0x01)
  238.         {
  239.                 LEDDirection = 1;
  240.                 LEDIndex = 0;
  241.                 LEDFlag = 1;
  242.                 RunMode = (RunMode+1)%9;
  243.                 Display(RunMode);
  244.         }
  245.         if(Key&0x02)
  246.         {
  247.                 if(SystemSpeedIndex>0)
  248.                 {
  249.                         --SystemSpeedIndex;
  250.                         SetSpeed(SystemSpeedIndex);
  251.                 }
  252.                 else
  253.                 {
  254.                         LEDFlash(6);
  255.                 }
  256.         }
  257.         if(Key&0x04)
  258.         {
  259.                 if(SystemSpeedIndex<28)
  260.                 {
  261.                         ++SystemSpeedIndex;
  262.                         SetSpeed(SystemSpeedIndex);
  263.                 }
  264.                 else
  265.                 {
  266.                         LEDFlash(6);
  267.                 }
  268.         }        
  269. }

  270. //***********************************************************************************
  271. main()
  272. {
  273.         unsigned char Key;
  274.         InitialCPU();
  275.         InitialTimer2();

  276.         while(1)
  277.         {
  278.                 Key = GetKey();
  279.                 if(Key!=0x00)
  280.                 {
  281.                         KeyDispose(Key);
  282.                 }
  283.         }
  284. }
复制代码

全部资料51hei下载地址:
跑马灯.zip (246.95 KB, 下载次数: 8)
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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