基于单片机的智能交通灯控制系统设计

initled(); } }

//定时器中断

void time0() interrupt 1 {

TH0 = (65536 - 50000)/256; TL0 = (65536 - 50000)%6; timebreak0 ++;

if(timebreak0 == 20) //一秒定时 {

i --;

car_time ++; timebreak0 = 0; }

if(car_time == 300) //5分钟记录一次车流量 {

car_number = break_count; break_count = 0; car_time = 0; } }

//紧急中断

void Exit_0() interrupt 0 {

initcross(); num_stop ++;

if(num_stop % 2) {

flag_stop = 1; //第一次中断为禁止通行 TR0 = 0;

timebreak0 = 0; breakflag = 1; }

else //第二次中断为恢复正常通行 {

flag_stop = 0; TR0 = 1; dx_red = 1; nb_red = 1; } }

//紧急中断路口为红灯 void stop()

{

if(flag_stop) {

initled(); }

while(flag_stop) {

dx_red = 0; nb_red = 0; initnumber(); } }

//计数器

void countfunction() interrupt 2 {

break_count ++; }

//显示车流量

void displaycount() {

if(botton1 == 0) {

delay_ms(10); if(botton1 == 0) {

u8 flag = 1; while(!botton1); TR0 = 0;

break_count = break_count % 100; while(flag) {

display_2(DONG,break_count); display_2(XI,break_count); display_2(NAN,break_count); display_2(BEI,break_count); if(botton1 == 0) {

delay_ms(10); if(botton1 == 0) {

flag = 0;

while(!botton1); } }

}

initcross(); TR0 = 1;

breakflag = 1; } } }

//根据车流量自动调时 void autsettime() {

TR0 = 0;

if(car_number >= 0 && car_number <= 20) 间不同 {

dx_redtime = 20;

dx_gretime = 20;

nb_redtime = dx_gretime + yeltime; nb_gretime = dx_redtime - yeltime; }

else if(car_number > 20 && car_number <= 50) {

dx_redtime = 20;

dx_gretime = 50;

nb_redtime = dx_gretime + yeltime; nb_gretime = dx_redtime - yeltime; } else {

dx_redtime = 20;

dx_gretime = 70;

nb_redtime = dx_gretime + yeltime; nb_gretime = dx_redtime - yeltime; }

TR0 = 1; }

//初始化交通灯led void initled() {

dx_red = 1; dx_yel = 1; dx_gre = 1; nb_red = 1; nb_yel = 1; nb_gre = 1;

分钟内车流量不同,时 //5 }

//初始化数码管 void initnumber() {

display_2(DONG,0); display_2(XI,0); display_2(NAN,0); display_2(BEI,0); }

//初始化路口交通灯 void initcross() {

initled();

initnumber(); }

//异常情况处理

void breakfunction() {

oneway(); night(); stop(); settime();

displaycount(); autsettime(); if(breakflag) {

timebreak0 = 0; } }

//单向通行 void oneway() {

if(botton2 == 0) {

delay_ms(10); if(botton2 == 0) {

while(!botton2); TR0 = 0;

//timebreak0 = 0; initcross(); count_oneway ++;

while(count_oneway < 3) {

联系客服:779662525#qq.com(#替换为@)