51的交通燈設計,通俗易懂

源程序:

#include <reg51.h>                     

#define uchar unsigned char

#define uint  unsigned int          

 

uchar data buf[4];

uchar data sec_dx=27;                                            

uchar data sec_nb=17;                 

uchar data set_timedx=27;           

uchar data set_timenb=17;           

int n;

uchar data countt0,countt1;

    

sbit  k1=P3^5;         

sbit  k2=P3^6;         

sbit  k3=P3^4;   

sbit  k4=P3^7;     

sbit  k5=P3^1;         

sbit  k6=P1^5;         

sbit Red_nb=P2^6;          

sbit Yellow_nb=P2^5;

sbit Green_nb=P2^4;    

 

sbit Red_dx=P2^3;          

sbit Yellow_dx=P2^2;

sbit Green_dx=P2^1;       

           

bit set=0;                    

bit dx_nb=0;              

bit shanruo=0;           

bit yejian=0;              

 

uchar code table[11]={    

      0x3f, 

      0x06, 

      0x5b, 

      0x4f,

      0x66,

      0x6d, 

      0x7d, 

      0x07, 

      0x7f, 

      0x6f, 

      0x00  

};

 

void delay(int ms);                

void key();                       

void key_to1();                      

void key_to2();

void key_to3();

void display();                 

void logo();                        

void Buzzer();

 

void main()

{

      TMOD=0X11;                          

      TH1=0X3C;         

      TL1=0XB0;

      TH0=0X3C;                          

      TL0=0XB0;

      EA=1;                              

      ET0=1;                                  

      ET1=1;                               

      TR0=1;                                  

      TR1=0;                                      

    logo();                                 

      P2=0Xc3;                        

    sec_nb=sec_dx+3;              

      while(1)                           

      {   

            key();                        

            display();                  

      }   

}

 

void key(void)

{   

      if(k1!=1)                                     

      {

            display();                        

            if(k1!=1)                                     

            {

                TR0=0;                                

                  shanruo=0;                            

                  P2=0x00;                         

                  TR1=0;                                        

                  if(set==0)                        

                       set_timedx++;         

                  else

                       set_timenb++;              

                  if(set_timenb==100)

                       set_timenb=1;

                  if(  set_timedx==100)

                       set_timedx=1;         

                  sec_nb=set_timenb;       

                  sec_dx=set_timedx;               

                do

                 {

                       display();                   

                  }

                  while(k1!=1);            

            }

      }

 

      if(k2!=1)                                     

      {         

            display();                        

            if(k2!=1)                               

            {   

                 TR0=0;                               

                  shanruo=0;                            

                  P2=0x00;                         

                  TR1=0;                                  

                  if(set==0)

                       set_timedx--;                 

                  else

                       set_timenb--;                 

                  if(set_timenb==0)

                       set_timenb=99;    

                  if(  set_timedx==0 )

                       set_timedx=99;             

                  sec_nb=set_timenb;            

                  sec_dx=set_timedx;

                do 

                {

                       display();             

                  }

                  while(k2!=1);                

            }

      }

 

      if(k3!=1)                                     

      {   

            display();                        

            if(k3!=1)                               

            {

                  TR0=1;                                

                  sec_nb=set_timenb;                     

                  sec_dx=set_timedx;                     

                  TR1=0;                                        

                  if(set==0)                              

                  {

                       P2=0X00;                        

                       Green_dx=1;                               

                       Red_nb=1;                       

                        sec_nb=sec_dx+3;                

                  }

                  else

                  {

                       P2=0x00;                         

                       Green_nb=1;

                       Red_dx=1;

                       sec_dx=sec_nb+3;

                  }

            }

      }

       

      if(k4!=1)                                     

    {

            display();                        

            if(k4!=1)                               

            { 

                 TR0=0;                                  

                  set=!set;                           

                  TR1=0;                                  

                  dx_nb=set;

                  do

                  {

                       display();                

                  }                     

                  while(k4!=1);            

            }

      }   

 

      if(k5!=1)                                     

      {

               display();                        

               if(k5!=1)                               

               { 

              TR0=0;                                  

                  P2=0x00;                         

                  Red_dx=1;

                  Red_nb=1;                       

                  TR1=0;

                  sec_dx=00;                            

                  sec_nb=00;

                  do

                  {

                       display();                  

                  }   

                  while(k5!=1);            

               }

      }

      if(k6!=1)                                     

      {

               display();                        

               if(k6!=1)                               

               { 

              TR0=0;                                  

                  P2=0x00;

                  TR1=1;

                  sec_dx=00;                            

                  sec_nb=00;

                  do

                  {

                       display();               

                  }   

                  while(k6!=1);            

               }

      }

}

void display(void)

{         

      buf[1]=sec_nb/10;          

      buf[2]=sec_nb%10;        

      buf[3]=sec_dx/10;          

      buf[0]=sec_dx%10;                    

      P1=0xff;                 

      P0=0x00;                   

      P1=0xfe;                     

      P0=table[buf[1]];                        

      delay(1);                    

      P1=0xff;                    

      P0=0x00;                   

                                     

      P1=0xfd;            

      P0=table[buf[2]];       

      delay(1);                    

      P1=0xff;                    

      P0=0x00;                   

      P1=0Xfb;                 

      P0=table[buf[3]];       

      delay(1);                    

      P1=0xff;                    

      P0=0x00;                   

      P1=0Xf7;                       

      P0=table[buf[0]];       

      delay(1);                    

}

void time0(void) interrupt 1 using 1     

{

      TH0=0X3C;                                      

      TL0=0XB0;                                      

      TR0=1;                                             

      countt0++;                                   

      if(countt0==10)                                

      {

            if((sec_nb<=3)&&(dx_nb==0)&&(shanruo==1))        

        {

                  Green_dx=0;

                  Yellow_dx=0;

            }                                  

          if((sec_dx<=3)&&(dx_nb==1)&&(shanruo==1))                     { 

                  Green_nb=0;

                  Yellow_nb=0;

            }   

      }

           

      if(countt0==20)                           

      {    countt0=0;                             

            sec_dx--;                               

            sec_nb--;                               

 

            if((sec_nb<=3)&&(dx_nb==0)&&(shanruo==1))                        {

                  Green_dx=0;

                  Yellow_dx=1;

            }                                  

         if((sec_dx<=3)&&(dx_nb==1)&&(shanruo==1))                      { 

                  Green_nb=0;

                  Yellow_nb=1;

            }                                          

            if(sec_dx==0&&sec_nb==3)          

              {

                  sec_dx=3;

                  shanruo=1;

            }

            if(sec_nb==0&&sec_dx==3)         

            {

                  sec_nb=3;

                  shanruo=1;

            }

            if(dx_nb==0&&sec_nb==0)             

            {

                  P2=0x00;                         

                  Green_nb=1;

                  Red_dx=1;

                  dx_nb=!dx_nb;

                  shanruo=0;

                  sec_nb=set_timenb;               

                  sec_dx=set_timenb+3;           

            }         

            if(dx_nb==1&&sec_dx==0)             

            {

                  P2=0X00;                        

                  Green_dx=1;                         

                  Red_nb=1;                       

                  dx_nb=!dx_nb;                     

                  shanruo=0;                            

                  sec_dx=set_timedx;               

                  sec_nb=set_timedx+3;           

            }

      }   

}

void time1(void) interrupt 3

{

      TH1=0X3C;                                      

      TL1=0XB0;                                      

      countt1++;                                   

      if(countt1==10)                              

      {

                  Yellow_nb=0;                  

                  Yellow_dx=0;                  

      }

      if(countt1==20)                           

      {    countt1=0;                             

                  Yellow_nb=1;                        

                  Yellow_dx=1;               

      }   

}

 

void logo()

{

      for(n=0;n<50;n++)    

         {

          P0=0x40;

          P1=0xfe;

            delay(1);   

          P1=0xfd;

            delay(1);   

            P1=0Xfb; 

            delay(1);   

            P1=0Xf7; 

            delay(1);   

          P1 = 0xff;

}

}

 

void delay(int ms)      

{

      uint j,k;

      for(j=0;j<ms;j++)            

         for(k=0;k<124;k++);       

}

没有更多推荐了,返回首页