專注電子技術學習與研究
當前位置:單片機教程網(wǎng) >> MCU設計實例 >> 瀏覽文章

交通信號燈的控制

作者:佚名   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2010年09月05日   【字體:

  此程序是在黃鶯老師的源程序上加以修改,在單片機板子上運行成功,這里是板子的硬件資料http://www.torrancerestoration.com/mcu/1019.html,數(shù)碼管是四位共陽管子,程序沒有進行仿真測試



 

 #include <reg51.h>
 #define uint unsigned int
 #define uchar unsigned char
 uint temp,temp1,temp2,num,shi,ge,i,j;
 uchar code table[]=

{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
 sbit WX1=P0^0;
 sbit WX2=P0^1;
 sbit WX3=P0^2;
 sbit WX4=P0^3;
 sbit NBL=P2^2;
 sbit NBY=P2^1;
 sbit NBR=P2^0;
 sbit DXL=P2^5;
 sbit DXY=P2^4;
 sbit DXR=P2^3;
 void init()
 {
  P0=0xff;
  P2=0xff;
  temp=0;
  temp1=0;
  temp2=0;
  shi=0;
  ge=0;
  TMOD=0x21;  
  TH0=0x3c; //50ms
  TL0=0xb0;
  EA=1;
  ET0=1;
  TF0=0;
  TR0=1;
 }
 void delay(uchar z)
 {
  uchar x,y;
  for(x=110;x>0;x--)
   for(y=z;y>0;y--);
 }

 void display()
 {
  shi=num/10;
  ge=num%10;
  P1=table[ge];
  WX3=1;
  WX2=0;WX1=0;WX4=0;
  delay(10);
  WX2=1;
  P1=table[shi];
  WX2=1;
  WX3=0;WX1=0;WX4=0;
  delay(10);
  WX3=1;  
 }
  void time0() interrupt 1 using 1
 {
  temp++;
  temp1++;
  TH0=0x3c;
  TL0=0xb0;
  TR0=1; 
 }
 void main()
 {
  init();
  while(1)
  {

if(temp==0)
{
DXR=1;
NBY=1;
DXL=0;
NBR=0;
num=21;
temp1=0;
for(i=20;i>0;i--)
{
num--;
while(temp1!=20)
{
display();
}
temp1=0;
}
}

if(temp==400)  
{
DXY=1;
NBR=0;
DXL=0;
num=3;
j=0; 
for(i=7;i>0;i--)
{
j++;
if(j%2==0)
{
num--;
}
temp2=temp;
DXL=!DXL;  //燈閃爍
while(temp-temp2!=10)
{
display();
};
}     
}

if(temp==470)
{   
    DXL=1;
 DXY=0;
 NBR=0;
 num=3;
 temp1=0;
 for(i=3;i>0;i--)
 {
 num--;
 while(temp1!=20)
 {
 display();
 }
 temp1=0;
 }


if(temp==530)
{
 DXY=1;
 DXR=0;
 NBR=1;
 NBL=0;
 num=26;
 temp1=0;
 for(i=25;i>0;i--)
 {
 num--;
 while(temp1!=20)
 {
 display();
 }
 temp1=0;
 }
 }
 

if(temp==1030)
{
DXY=1;
DXR=0;
NBL=0;
num=3;
j=0; 
for(i=7;i>0;i--)
{
j++;
if(j%2==0)
{
num--;
}
temp2=temp;
NBL=!NBL;  //燈閃爍
while(temp-temp2!=10)
{
display();
};
}     
}

if(temp==1100)
{   
    DXL=1;
 DXR=0;
 NBY=0;
 num=3;
 temp1=0;
 for(i=3;i>0;i--)
 {
 num--;
 while(temp1!=20)
 {
 display();
 }
 temp1=0;
 }

temp=0;
}
}
}
}
關閉窗口

相關文章