单片机交通灯C语言设计程序

更新时间:2023-05-29 04:57:02 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

用单片机实现的交通灯设计

#include<reg51.h>

#define uchar unsigned char

#define uint unsigned int

uchar

,0x80,0x90};

uchar cs,second_counts;

uchar k;

uchar Flash_count=0,Operation_type=1;

//east and west leds

sbit RED_A=P2^0;

sbit YELLOW_A=P2^1;

sbit GREEN_A=P2^2;

//south and north leds

sbit RED_B=P2^3;

sbit YELLOW_B=P2^4;

sbit GREEN_B=P2^5;

//7segments control bits code seg7[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8

用单片机实现的交通灯设计

sbit S1=P3^0;

sbit S2=P3^1;

sbit S3=P3^2;

sbit S4=P3^3;

//Delay function

void Delay(uchar ms)

{

uchar j;

while(ms--)

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

}

//east and west leds control funtion

void East_West_Leds()

{

//south green leds on //让东、西的灯和南、北的灯亮时不冲突,即东、西灯亮绿灯时南、北灯亮红灯//

RED_A=1;

YELLOW_A=1;

GREEN_A=0;

用单片机实现的交通灯设计

//east red leds on // 符合实际交通 // RED_B=0;

YELLOW_B=1;

GREEN_B=1;

}

//east and west 7segments control function void East_West_7seg()

{

S1=0;

S2=0;

S3=0;

S4=0;

if(second_counts>0)

{

S1=1;

S2=0;

P1=seg7[second_counts/10];

Delay(2);

S1=0;

S2=1;

P1=seg7[second_counts%10];

用单片机实现的交通灯设计

Delay(2);

}

}

//east and west flashing

void East_West_Flash()

{

GREEN_A=1;

YELLOW_A=!YELLOW_A;

S1=0;

S2=0;

Delay(500);

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

East_West_7seg();

}

//south and north leds control funtion void South_North_Leds()

{

//south green leds on

RED_A=0;

YELLOW_A=1;

用单片机实现的交通灯设计

GREEN_A=1;

//east red leds on

RED_B=1;

YELLOW_B=1;

GREEN_B=0;

}

//south and north 7segments control function void South_North_7seg()

{

S1=0;

S2=0;

S3=0;

S4=0;

if(second_counts>0)

{

S3=1;

S4=0;

P1=seg7[second_counts/10];

Delay(2);

S3=0;

S4=1;

用单片机实现的交通灯设计

P1=seg7[second_counts%10];

Delay(2);

}

}

//south led flashing

void South_North_Flash()

{

GREEN_B=1;

YELLOW_B=!YELLOW_B;

S3=0;

S4=0;

Delay(500);

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

South_North_7seg();

}

//traffic function

void Traffic()

{

switch (Operation_type)

{

用单片机实现的交通灯设计

//east go,south stop

case 0:

{

East_West_Leds();

while(second_counts>3)

East_West_7seg();

while(second_counts<=3)

East_West_Flash();

break;

}

//south go, east stop

case 1:

{

South_North_Leds();

while(second_counts>3)

South_North_7seg();

while(second_counts<=3)

South_North_Flash();

break; }

用单片机实现的交通灯设计

}

}

void main()

{

cs=0;

second_counts=10;

Operation_type=0;

TMOD=0x01;

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

EA=1;

ET0=1;

TR0=1;

while(1)

{

Traffic();

}

}

//TO interruput Services

void time_interupt() interrupt 1

{

用单片机实现的交通灯设计

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

if(++cs==20)

{

cs=0;

--second_counts;

if(second_counts==0)

{

second_counts=10;

Operation_type=(++Operation_type)%2;

}

}

}

本文来源:https://www.bwwdw.com/article/pqp4.html

Top