单片机控制交通灯V1.0

单片机控制交通灯V1.0

要求:

1)、十字路口南北、东西方向各装有绿、黄、红指示灯,南北通行时间为15s,东西通行时间为10s,当由绿灯变为红灯前的3s内,绿灯灭而黄灯亮。

2)、通行时间各由两位7LED显示器指示,且为倒时间指示(即从1510逐次减一,禁止通行时显示器灭)。

3)、当某方向显示红灯时,容许人工干预强行改变两个方向的指示灯颜色,以让救护车之类的车辆通行,但发出改变通行命令后有3s时间使显示绿灯的方向改变显示黄灯,并以每秒10次的频率闪烁,以示警告。

 

小结:本实验用ISIS 7 ProfessionalKeil uVision2仿真,由于是学单片机以来做的第一个实验,在南北、东西两个方向四个数码管倒计时那里控制得不好,用动态描扫实现,闪烁比较明显,希望在后面的理论学习中找到更好的实现方法。

 单片机控制交通灯V1.0_第1张图片

 

 

参考源程序:

#include"reg52.h"

 

#define uchar unsigned char

#define uint unsigned int

 

 

sbit one=P3^4;      //数码段位选控制

sbit two=P3^5;

sbit three=P3^6;

sbit four=P3^7;

 

sbit d_lu=P0^1;

sbit d_huang=P0^2;

sbit d_hong=P0^3;

 

sbit x_lu=P2^1;

sbit x_huang=P2^2;

sbit x_hong=P2^3;

 

sbit n_lu=P2^4;

sbit n_huang=P2^5;

sbit n_hong=P2^6;

 

sbit b_lu=P0^4;

sbit b_huang=P0^5;

sbit b_hong=P0^6;

 

uint fangxiang=0;

uint m,n,k;

 

code unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0XD8,0x80,0x90};//7LED对应于数字'0','1'...'9'

void delay(uint z)

{

    uint x,y;

    for(x=z;x>0;x--)

        for(y=110;y>0;y--);

}

 

 

 

void delayMs(uint timer_num, uint fxang)

{

    uchar aa,bb,j;

    TMOD=0x10;                  //设定T1为方式1

    TR1=1;                      //启动定时器工作 

    fangxiang=fxang;                          

    for(j=0;j<10;j++)           //每隔100ms中断一次,中断10次为1s

    {

        TH1=0x3c;                   //定初值                  

        TL1=0xb0;

        if(fxang==0)            //东西方向通行

        {

           do

                       {

                aa=timer_num/10;

                bb=timer_num%10;

                one=1;          //数码管动态扫描,位选

                     two=0;

                three=0;

                four=0;

                P1=tab[aa];     //数码管动态扫描,段选

                delay(50);

 

                     one=0;

                     two=1;

                three=0;

                four=0;

                P1=tab[bb];

                delay(50);

            }while(!TF1);

                     

            TF1=0;

          }

         else

            if(fxang==1)        //南北方向通行

            {

                           do

                           {

                    aa=timer_num/10;

                    bb=timer_num%10;

                    one=0;

                         two=0;

                         three=1;

                         four=0;

                    P1=tab[aa];

                    delay(50);

 

                         one=0;

                         two=0;

                         three=0;

                         four=1;

                    P1=tab[bb];

                    delay(50);

                 }while(!TF1);

                TF1=0;

             }

    }

}

 

void zhuangtai_1() 

{

  d_lu=0;

  d_hong=1;

  d_huang=1;

 

  x_lu=0;

  x_hong=1;

  x_huang=1;

 

  n_hong=0;

  n_huang=1;

  n_lu=1;

 

  b_hong=0;

  b_lu=1;

  b_huang=1;

 

 

}                                 

 

void zhuangtai_2()      

{

  d_lu=1;

  d_huang=0;

  d_hong=1;

 

  x_lu=1;

  x_huang=0;

  x_hong=1;

 

  n_hong=0;

  n_huang=1;

  n_lu=1;

 

  b_hong=0;

  b_huang=1;

  b_lu=1;

}

void zhuangtai_3() 

{

  d_lu=1;

  d_huang=1;

  d_hong=0;

 

  x_lu=1;

  x_huang=1;

  x_hong=0;

 

  n_hong=1;

  n_huang=1;

  n_lu=0;

 

  b_hong=1;

  b_huang=1;

  b_lu=0;

 

}

void zhuangtai_4()  

{

    d_lu=1;

    d_huang=1;

    d_hong=0;

 

    x_lu=1;

    x_huang=1;

    x_hong=0;

 

    n_hong=1;

    n_huang=0;

    n_lu=1;

 

    b_hong=1;

    b_huang=0;

    b_lu=1;

}

 

void exint0(void) interrupt 0   //人工干预,外部中断0实现

{

    EA=0;

   

    one=0;                      //关闭所有数码管

    two=0;

    three=0;

    four=0;

    TMOD=0x10;                  //设定T1为方式1

    TR1=1;                      //启动定时器工作                                    

    for(m=0;m<30;m++)           //改变通行命令后3s

    {

        TH1=0x3c;                   //定初值                  

        TL1=0xb0;

         do

             {

            delay(50);

            delay(50);

        }while(!TF1);

        TF1=0;

    }

   

    if(fangxiang==0)            //fangxiang为产生外部中断时通行的方向,0为东西方向

    {

        n_hong=0;

        b_hong=0;

        d_lu=1;

        x_lu=1;

        for(n=0;n<10;n++)       //使黄灯每秒闪10

             {

            TR1=1;              //启动定时器工作 

            TH1=0x3c;               //定初值                  

            TL1=0xb0;

                 do

                       {

                delay(100);

            }while(!TF1);

            TF1=0;

                      

            d_huang=0;

            x_huang=0;

            delay(1000);

            d_huang=1;

            x_huang=1;

             }

    }

    else                        //产生中断时,通行方向为南北方向

    {

        d_hong=0;

        x_hong=0;

        n_lu=1;

        b_lu=1;

        for(k=0;k<10;k++)//每秒10

             {

            TR1=1;              /* 启动定时器工作 */

            TH1=0x3c;           //定初值                  

            TL1=0xb0;

                 do

                       {

                delay(100);

            }while(!TF1);

            TF1=0;

                      

                               

            n_huang=0;

            b_huang=0;

            delay(1000);

            n_huang=1;

            b_huang=1;

             }

    }

 

    EA=1;

}

    

 

main()

{

    int i;

    TCON=0x55;

    IE=0x81;

    while(1)

    {

            for(i=10;i>=4;i--)      //东西通行10s

                 {

                zhuangtai_1();

                delayMs(i,0);

            }

            for(i=3;i>=0;i--)              

            {

                zhuangtai_2();

                delayMs(i,0);         

                  

             }

                      

            for(i=15;i>=4;i--)      //南北通行15s              

            {           

                zhuangtai_3();

                delayMs(i,1);

                       }

                   

            for(i=3;i>=0;i--)

            {

                zhuangtai_4();           

                delayMs(i,1);                                      

            }                            

     } 

}                       

                    

               

                            

  

 

 

你可能感兴趣的:(单片机控制交通灯V1.0)