当前位置:编程学习 > 网站相关 >>

交通灯(二)

警告:不同单片机的动态扫描显示不一样,即下面黄底红字是让数码管显示函数,如果单片机不一样,只需改显示      函数即可,其他的不要动。

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;  

   }

 }

 

补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,