您现在的位置是:首页 >动态 > 2023-10-29 02:06:20 来源:

单片机设计交通灯程序(交通灯程序)

导读 大家好,我是小夏,我来为大家解答以上问题。单片机设计交通灯程序,交通灯程序很多人还不知道,现在让我们一起来看看吧!我实训是做了这个...

大家好,我是小夏,我来为大家解答以上问题。单片机设计交通灯程序,交通灯程序很多人还不知道,现在让我们一起来看看吧!

我实训是做了这个项目,发给你看看,功能主要和e68a84e8a2ad3231313335323631343130323136353331333330356163交通灯的差不多,还可以显示具体的时间,跳转的时候,时间是倒计时的,当到0以后,下面灯转换

#include<reg51.h>

unsigned int shi,ge,js;

void delay(unsigned char i);

sbit P2_0=P2^0;

sbit P2_1=P2^1;

unsigned int i=0;

unsigned char key_press();

unsigned char key_scan();

unsigned int m=0;m2=0;n1=6;n2=2;

void TIMERO_1();

void TIMERO_0();

unsigned char led[4]={0xee,0xdb,0xf5,0xdb};

unsigned char code tabe[13]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x00};

unsigned char num;

unsigned char key_scan()

{ unsigned char hang,lie,key;

P3=0x0f;

if(P3!=0x0f)

delay(100);

if(P3!=0x0f)

{

switch(P3&0x0f)

{

case 0x0e:hang=0;break;

case 0x0d:hang=1;break;

case 0x0b:hang=2;break;

//case 0x07:hang=3;break;

}

P3=0xf0;

switch(P3&0xf0)

{

case 0xe0:lie=0;break;

case 0xd0:lie=1;break;

case 0xb0:lie=2;break;

case 0x70:lie=3;break;

}

while(P3!=0xf0);

key=hang*4+lie;

}

// else

// key=12;

return (key);

}

unsigned char key_press()

{

unsigned char temp,key_flag;

P3=0x0f;

delay(100);

temp=P3;

if (temp!=0x0f)

{

key_flag=1;

}

else

{

key_flag=0;

}

return key_flag;

}

void TIMERO_0() interrupt 1

{

TH0=0X3C;

TL0=0XB0;

m++;

if(m==20)

{

m=0;

n1--;

shi=n1/10;

ge=n1%10;

}

if(n1==0)

{i=i+1;

n2=3;

TR1=1;

TR0=0;

}

//if(ge==0)

//{ge=2;}

}

void TIMERO_1() interrupt 3

{

TH1=0X3C;

TL1=0XB0;

m2++;

if(m2==20)

{

m2=0;

n2--;

shi=n2/10;

ge=n2%10;

}

if(n2==0)

{

i=i+1;

n1=js;

TR0=1;

TR1=0;

if(i== 4)

{

i=0;

}

//if(ge==0)

//{ge=js;}

}

}

void display(unsigned char a,b,c,d,e,f)

{

P2_1=1;

P2_0=0;

P0=0x20;

P2_0=1;

P2_1=0;

P0=tabe[a];

delay(10);

P0=0x00;

P2_1=1;

P2_0=0;

P0=0X10;

P2_0=1;

P2_1=0;

P0=tabe[b];

delay(10);

P0=0x00;

P2_1=1;

P2_0=0;

P0=0x08;

P2_0=1;

P2_1=0;

P0=tabe[c];

delay(10);

P0=0x00;

P2_1=1;

P2_0=0;

P0=0x04;

P2_0=1;

P2_1=0;

P0=tabe[d];

delay(10);

P0=0x00;

P2_1=1;

P2_0=0;

P0=0x02;

P2_0=1;

P2_1=0;

P0=tabe[e];

delay(10);

P0=0x00;

P2_1=1;

P2_0=0;

P0=0x01;

P2_0=1;

P2_1=0;

P0=tabe[f];

delay(10);

P0=0x00;

delay(10);

}

void main()

{

TMOD=0X11;

TH0=0X3C;

TL0=0XB0;

TH1=0X3C;

TL1=0XB0;

ET1=1;

ET0=1;

EA=1;

P1=0XFF;

while(1)

{

P0=0;

if(key_press())

{ num=key_scan();

if(num<=9)

{

shi=ge;

ge=num;

js=shi*10+ge;

n1=js;

}

if(num==10)

{

TR0=0;

TR1=0;

shi=ge;

ge=num;

}

if(num==11)

{

TR0=1;

i=0;

js=shi*10+ge;

}

}

P1=led[i];

display(12,12,12,12,shi,ge);

}

}

void delay(unsigned char i )

{

unsigned char j, k;

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

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

}

本文到此讲解完毕了,希望对大家有帮助。