用C语言编写的交通灯控制程序

用C语言编写的交通灯控制程序,第1张

我用的是ATMEGA16单片机编程的,也是AVR单片机

假设PA0接红灯,PA1接黄灯,PA2接绿灯。高电平亮。

我的延时函数是ms延时,比如delay(1000)表示延时1000毫秒

void main()

{

DDRA|=BIT(0)|BIT(1)|BIT(2);//设置PA0,PA1,PA2口为输出

while(1)

{

PORTA|=BIT(0);

PORTA&=~BIT(1);

PORTA&=~BIT(2);

delay(50000);

PORTA&=~BIT(0);

PORTA|=BIT(1);

PORTA&=~BIT(2);

delay(5000);

PORTA&=~BIT(0);

PORTA&=~BIT(1);

PORTA|=BIT(2);

delay(50000);

}

}

#include <reg51h>

#include <stdioh>

#define uchar     unsigned char

#define uint      unsigned int

uchar h;

uint r=0;

uint s;

uchar code  dis[]={ 0Xc0,0Xf9, 0Xa4,0Xb0,0X99,0X92,0X82,0Xf8,0X80,0X90,0Xff,};

/0//1//2//3//4//5//6//7//8//9//NULL/

void delay(uint c)

{

uint i,j;

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

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

}

void t0(void) interrupt 0 using 1   //interrupt 1

{

delay(2);

if(INT0==0)

{

r++;

if(r==2)

{

r=0;

}

r%=2;

if(r==0)

{

P0=0xeb;

P1=0x00;

P2=0xff;

}while(1)

{

if(INT0==0|INT1==0|T0==0)

{

delay(2);

if(INT0==0|INT1==0|T0==0)

break;

}

};

}

}

void t1(void) interrupt 2 using 1   //interrupt 2

{

delay(2);

if(INT1==0)

{

r++;

if(r==2)

{

r=0;

}

r%=2;

if(r==0)

{

P0=0xdd;

P1=0x00;

P2=0xff;

}while(1)

{

if(INT1==0|INT0==0|T0==0)

{

delay(2);

if(INT1==0|INT0==0|T0==0)

break;

}

};

}

}

void t2(void) interrupt 1 using 1   //interrupt 3

{

TH0=0xff;

TL0=0xff;

delay(2);

if(T0==0)

{

P0=0xdb;

P1=0x00;

P2=0xff;

}while(1)

{

if(INT0==0|INT1==0)

{

delay(2);

if(INT0==0|INT1==0)

break;

}

};

}

void main(void)

{

TMOD=0x06;

TH0=0xff;

TL0=0xff;

TR0=1;

EA=1;

EX0=1;

EX1=1;

ET0=1;

while(1)

{

P0=0xeb;

for(h=28;h>0;h--)

{

for(s=248;s>0;s--)

{

P1=0x00;

P2=0xff;

P1=0x01;

P2=dis[h/10];

delay(1);

P1=0x00;

P2=0xff;

P1=0x02;

P2=dis[h%10];

delay(1);

P1=0x00;

P2=0xff;

P1=0x04;

P2=dis[(h+2)/10];

delay(1);

P1=0x00;

P2=0xff;

P1=0x08;

P2=dis[(h+2)%10];

delay(1);

}

}

delay(248);

P0=0xf3;

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

{

delay(392);

P0=0xf3;

delay(1);

P1=0x00;

P2=0xff;

P1=0xaa;

P2=dis[(h-1)%10];

delay(600);

P0=0xfb;

}

delay(248);

P0=0xdd;

for(h=38;h>0;h--)

{

for(s=248;s>0;s--)

{

P1=0x00;

P2=0xff;

P1=0x01;

P2=dis[(h+2)/10];

delay(1);

P1=0x00;

P2=0xff;

P1=0x02;

P2=dis[(h+2)%10];

delay(1);

P1=0x00;

P2=0xff;

P1=0x04;

P2=dis[h/10];

delay(1);

P1=0x00;

P2=0xff;

P1=0x08;

P2=dis[h%10];

delay(1);

}

}

delay(248);

P0=0xde;

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

{

delay(392);

P0=0xde;

delay(1);

P1=0x00;

P2=0xff;

P1=0xaa;

P2=dis[(h-1)%10];

delay(600);

P0=0xdf;

}

delay(248);

}

}

那个数码管显示的if判断要用if(a>=x);的办法,因为定时器中断优先级别比if语句高,在if语句运行时与定时器中断在时序上重叠的时候,用if(a==x);的话会导致num3的数值没有得到有效控制,后果就会像你上面所说的那样。

#include<reg52h>

#define uchar unsigned char

#define uint unsigned int

uchar b=5,c;

unsigned char code a[]={0x6F,0x7F,0x07,0x7D,0x6D,0x66,0x4F,0x5B,0x06,0x3f};

sbit sg=P1^0;

sbit sy=P1^1;

sbit sr=P1^2;

sbit hg=P1^3;

sbit hy=P1^4;

sbit hr=P1^5;

sbit ge=P1^6;

sbit shi=P1^7;

uint num1,num2,num3;

void delay(unsigned int z1)

{

unsigned int a1,b1;

for(a1=z1;a1>0;a1--)

for(b1=12500;b1>0;b1--);

}

void main()

{

P2=0;

P1=0;

shi=1;

P2=a[8];

shi=0;

ge=1;

P2=a[4];

ge=0;

TMOD=0X11;

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

TH1=(65536-50000)/256;

TL1=(65536-50000)%256;

EA=1;

ET0=1;

ET1=1;

TR0=1;

TR1=1;

while(1)

{

switch(num2) //红绿灯

{

case(0): //10s内s绿灯亮

sr=0;

hg=0;

sg=1;

hr=1;

break;

case(1): //5S:竖黄横红

sg=0;

sy=1;

delay(5);

sy=0;

delay(5);

break;

case(2): //10s:竖红横绿

sg=0;

sr=1;

sy=0;

hg=1;

hr=0;

break;

case(3): //5S:竖红横黄

hg=0;

hy=1;

delay(5);

hy=0;

delay(5);

break;

}

}

}

void timer0() interrupt 1 //定时器0

{

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

num1++;

if(num1==200)

num2=1;

if(num1==300)

num2=2;

if(num1==500)

num2=3;

if(num1==600)

{

num1=0;

num2=0;

}

}

void timer1() interrupt 3 //定时器1

{

TH1=(65536-50000)/256;

TL1=(65536-50000)%256;

num3++;

if(num3>=20) //数码管显示

{

num3=0;

c++;

ge=1;

P2=a[b++];

ge=0;

if(b>=10)

b=0;

if(b==1)

{

P2=a[9];

shi=1;

shi=0;

}

if(c>=15)

{

c=0;

shi=1;

P2=a[8];

shi=0;

ge=1;

P2=a[4];

ge=0;

b=5;

}

}

}

以上就是关于用C语言编写的交通灯控制程序全部的内容,包括:用C语言编写的交通灯控制程序、基于51单片机控制交通灯的电路图与C语言程序、单片机C语言编交通灯,请教~~等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9299881.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-26
下一篇2023-04-26

发表评论

登录后才能评论

评论列表(0条)

    保存