久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4774|回復: 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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 偷拍自拍第一页 | 永久www成人看片 | 欧美成人aaa级毛片在线视频 | 91精品久久久 | 久久亚洲国产 | 日本久久精品视频 | 国产1区2区3区| 国产精品久久久久久婷婷天堂 | aaaa一级毛片| 久久综合av | 亚洲欧洲中文 | 欧美日韩亚洲国产 | 91久久精 | 国产午夜视频 | 日韩精品国产精品 | 国产一区二区成人 | 中文字幕日韩专区 | 密色视频| 欧美久久久久 | 黄网站涩免费蜜桃网站 | 日韩视频在线一区 | 国产欧美日韩精品一区二区三区 | 在线午夜电影 | 日韩小视频在线 | 亚洲欧美日韩国产综合 | 久久久久亚洲精品 | 国产乱码精品一品二品 | 亚州精品成人 | 日韩一区二区不卡 | 久久精品 | 干出白浆视频 | 最近中文字幕第一页 | 欧美精选一区二区 | 亚洲伊人a | 欧美日韩视频 | 国产成人精品一区二区三区四区 | 91成人在线视频 | 视频一区在线观看 | 欧美网站一区 | 国产精品国产三级国产aⅴ中文 | 精品av|