找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2739|回復(fù): 3
收起左側(cè)

新人共享一個(gè)交通燈設(shè)計(jì) 仿真+程序

[復(fù)制鏈接]
ID:93536 發(fā)表于 2015-10-25 17:55 | 顯示全部樓層 |閱讀模式
新人,多多指教
0.png

  1. #include<stdio.h>
  2. #include<REG51.h>
  3. #include<intrins.h>
  4. sfr p0= 0x80;
  5. sfr p1= 0x90;
  6. sfr p2= 0xA0;
  7. sfr p3= 0xb0;      //這部分內(nèi)容其實(shí)在“#include<reg51.h>”里已經(jīng)有,但里面定義的必須區(qū)分大小寫(xiě),在這里,因?yàn)槲页绦虿捎玫氖切?xiě),reg51.h里對(duì)各個(gè)端口與寄存器的定義都是大寫(xiě),所以在編譯連接時(shí),會(huì)報(bào)錯(cuò),所以,在本設(shè)計(jì)程序里,我只用到了端口,在這里也就只定義了四個(gè),而沒(méi)有去改reg51.h里面的內(nèi)容。其實(shí)兩者是一樣的。
  8. sbit  sw      = p0^0;
  9. sbit   OE    =P0^6;
  10. sbit   LE    =P0^7;  //74LS373鎖存器控制端定義
  11. char  display[]={ 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99 }; //p1口的數(shù)碼管時(shí)間顯示調(diào)用,利用74L74BCD碼,8位驅(qū)動(dòng)輸出;
  12. //函數(shù)聲明 begin
  13. void  delay1(int count);
  14. void  delay_long(int  number1,int number2);
  15. void    people_car_drive();
  16. //函數(shù)聲明end
  17. //***********************//延時(shí)子程序
  18. void  delay1(int  count)
  19. { int  i;
  20.    for(i=count;i>0;i--)
  21.        { ;}
  22. }
  23. void  delay_long(int  number1,int number2)
  24.   {
  25.    int a,b;
  26. for(a=number1;a>0;a--)
  27.    {
  28.     for(b=number2;b>0;b--)
  29.      { _nop_();     }
  30.    }
  31.    
  32.   }
  33. //**********************//延時(shí)子程序
  34. void   people_car_drive()
  35. {
  36.    int   p_1=2,i,j=9,p_2=6;    //****************//行人通行時(shí),延時(shí)20秒
  37.           p2=0x09;                     //南北紅燈亮
  38.        p3=0x24;                     //東西綠燈亮
  39.    
  40.   while(p_1-->0)
  41.      { LE=1;
  42.     OE=0;
  43.     if(p_1==0){OE=1;}            //當(dāng)十位數(shù)減到0時(shí),只顯示個(gè)位數(shù)
  44.     p1=display[p_1];
  45.     delay1(1000);
  46.     LE=0;  
  47.     j=9;
  48.      for(i=10;i>0;i--)
  49.    {
  50.      if(p_1==0&&j==3)break;     //減到3時(shí)退出循環(huán),讓其黃燈閃爍顯示
  51.      p1=display[j--];
  52.      delay_long(16000,2);
  53.   if(sw==1)return;   
  54.    }
  55.    
  56.      }
  57.    
  58. //*******************************************************************************//
  59.                   
  60.      p2=0x12;                  //南北黃燈閃爍三秒,以提醒行人注意
  61.      p3=0x12;
  62.   p1=display[3];
  63.   delay_long(8000,1);
  64.   p2=0x00;     
  65.      p3=0x00;     
  66.   delay_long(14000,1);
  67.    
  68.   p2=0x12;
  69.   p3=0x12;
  70.   p1=display[2];
  71.   delay_long(8000,1);
  72.   p2=0x00;     
  73.      p3=0x00;
  74.   delay_long(14000,1);
  75.   
  76.   p2=0x12;
  77.   p3=0x12;
  78.   p1=display[1];
  79.   delay_long(8000,1);
  80.   p2=0x00;     
  81.      p3=0x00;
  82.   delay_long(14000,1);
  83. //*****************以下是車(chē)輛通行時(shí)延時(shí)60秒//

  84.    p2=0x24;                 //南北綠燈亮
  85.    p3=0x09;                 //東西紅燈亮
  86.   
  87.      while(p_2-->0)
  88.      { LE=1;
  89.     OE=0;
  90.     if(p_2==0){OE=1;}       //當(dāng)十位數(shù)減到0時(shí),只顯示個(gè)位數(shù)
  91.     p1=display[p_2];
  92.     delay1(1000);
  93.     LE=0;
  94.     j=9;
  95.      for(i=10;i>0;i--)
  96.    {
  97.   if(p_2==0&&j==3)break;  //減到2時(shí)退出循環(huán)
  98.      p1=display[j--];
  99.      delay_long(16000,2);
  100.   if(sw==1)return;     
  101.    }
  102.      }
  103.    
  104.      p2=0x12;                //南北黃燈閃爍三秒,以提醒行人注意
  105.      p3=0x12;
  106.   p1=display[3];
  107.   delay_long(8000,1);
  108.   p2=0x00;     
  109.      p3=0x00;     
  110.   delay_long(14000,1);
  111.    
  112.   p2=0x12;
  113.   p3=0x12;
  114.   p1=display[2];
  115.   delay_long(8000,1);
  116.   p2=0x00;     
  117.      p3=0x00;
  118.   delay_long(14000,1);
  119.   
  120.   p2=0x12;
  121.   p3=0x12;
  122.   p1=display[1];
  123.   delay_long(8000,1);
  124.   p2=0x00;     
  125.      p3=0x00;
  126.   delay_long(14000,1);    //南北黃燈閃爍三秒完畢
  127. }

  128. void  main()               //主函數(shù)入口處
  129. {
  130.        p0=0x01;
  131.        p1=0x00;
  132.        p2=0x00;
  133.        p3=0x00;               //初始化各端口
  134. { while(1)
  135.   {
  136.      if(sw==0)
  137.     {  people_car_drive();}
  138. else
  139.   {
  140.    p2=0x00;
  141.    p3=0x00;                    //關(guān)閉所有交通燈
  142.   }
  143.   }
  144.   
  145. }
  146. }
復(fù)制代碼


交通燈設(shè)計(jì).rar

62.51 KB, 下載次數(shù): 36, 下載積分: 黑幣 -5

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的積分獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

ID:100158 發(fā)表于 2015-12-23 15:55 | 顯示全部樓層
沒(méi)黑幣啊怎么辦啊
回復(fù)

使用道具 舉報(bào)

ID:116210 發(fā)表于 2016-4-29 23:04 | 顯示全部樓層
參考參考,謝謝
回復(fù)

使用道具 舉報(bào)

ID:235609 發(fā)表于 2017-9-25 21:40 | 顯示全部樓層
沒(méi)有黑幣咋辦
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表