閱讀271 返回首頁    go 阿裏雲 go 技術社區[雲棲]


交通燈(二)

要求同交通燈(一),https://blog.csdn.net/cometwo/article/details/8866784

警告:不同單片機的動態掃描顯示不一樣,即下麵黃底紅字是讓數碼管顯示函數,如果單片機不一樣,隻需改顯示      函數即可,其他的不要動。

2: 程序設計(僅供參考的 C語言源程序)

#include<reg52.h>     //包含頭文件,一般情況不需要改動,頭文件包含特殊功能寄存器的定義

#include<math.h>  

#define uchar unsigned char

#define uint  unsigned int

#define ulang unsigned lang

static unsigned char count;

code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; 

         //共陰數碼管 0-9

uchar smg[8];                 //定義緩衝區

uint  we,ns,h,j;   //ns代表南北,we代表東西

 

   

int aaa();   //東西紅燈亮,南北綠燈,黃燈亮

int bbb();   //南北紅燈亮,東西綠燈,黃燈亮

int eee();   //第一次完成顯示,繼續第二次初始化

void delay(unsigned int cnt)

{

 while(--cnt);

}

 

void display( )

{

                                 //取每一位的數字

            smg[0]=tab[we/10];     

       smg[1]=tab[we%10];

  smg[2]=0x00;             

            smg[3]=0x00;   

              smg[4]=0x00;

       smg[5]=0x00;            

                 smg[6]=tab[ns/10];  

  smg[7]=tab[ns%10];

}

void main()

{

     uchar i;

     TMOD |=0x01;  //定時器0 10ms in 12M crystal 用於計時

TH0=0xd8;     //初值

TL0=0xf0;

ET0=1;

TR0=1;

     EA =1;     

  display();

  while(1)

  {

    

for(i=0;i<8;i++)    //顯示函數,因單片機而異

   {

      P0=smg[i];

 P2=i;

 delay(100);

   }

           ccc();     //進入交通燈控製程序

display( );    //掃描數碼管

       }  

 

}

void timer() interrupt 1             //中斷函數

{

   

   TH0=0xd8;                  //重新賦值

   TL0=0xf0;

   count++;

}

int aaa()       

{

          if(j<25)                 //東西紅燈計數30秒,南北25秒綠燈亮

      {

      if(j==1) { we=30,ns=25; }

 ns--;

      we--;

      P1=0xde;

 return 0; } 

 

    if(25<=j<30)          //南北黃燈5

  {       

      if(j==26)  {  ns=5; } 

     P1=0xee;     

ns--; 

we--;   

return 0;    }

}

int bbb()

{

            if(h<25)   

      {                    //南北紅燈30秒,東西綠燈25

      if(h==1) { we=25,ns=30; }

 ns--;

      we--;

       P1=0xf3;

 return 0; } 

 

    if(25<=h<30)           //東西黃燈5

  {       

      if(h==26)  {  we=5; } 

     P1=0xf5;     

ns--; 

we--;   

return 0;    }

 

     

 }

 int eee()      //一次周期交通燈顯示完後,重新賦值,等待第二次

 {

  j=0;

 h=0;

 return 0;

 }

 int ccc()          //交通燈控製函數

 {

     

  if (count==100)       //定時一秒

    {

            count=0;

    j++;            //算法函數

 if ( (j>30)&&(j!=61))  { h++;      bbb();  return 0;   }

    if(j==61)   { eee(); return 0;}

               aaa();        return 0;   

   } 

 }

 

最後更新:2017-04-03 19:06:48

  上一篇:go 後綴樹
  下一篇:go 我在華為的半打歲月