资讯详情

51课程设计—交通灯篇

/*===Chip:C12C5A32S2=================================*/ /*===Software:Keil 4 C51===================================*/ /*===Author:梁鹏=========================================*/ /*===Organization:广西民族大学物电学院07自动化=======================*/ /*===Date:2010年05月26日=================================*/ /*===Version:1.0=========================================*/ /*===声明:本程序属原创作品,仅供学习交流,不得用于商业用途,如需 转载,请注明出处,谢谢合作!==============================*/ ///*----------文件名:Traffic_light.c-------------*/    

#include<reg52.h>#include<intrins.h>#include<LCD1602.H>#define  ON  0#define  OFF  1#define  yellow_time  3//===========================sbit MODE_KEY=P3^3;     //调整时间的模式按键sbit TIME_ADD=P3^4;  //时间增加5s的按键sbit TIME_DEC=P3^5;  //时间减少5s的按键sbit LOCK_KEY=P3^6;  //封/恢复交通按键 sbit North_turn_R= P0^0; //南北左拐红灯sbit North_turn_G= P0^1; //南北左拐绿灯sbit North_Y =  P0^2; //南北黄灯sbit North_R =  P0^3; //南北直行红灯sbit North_G =  P1^0; //南北直行绿灯sbit East_turn_R = P1^1; //东西左拐红灯sbit East_turn_G = P1^2; //东西左拐绿灯sbit East_Y =  P1^3;  //东西黄灯sbit East_R =  P1^4; //东西直行红灯sbit East_G =  P1^5; //东西直行绿灯//===========================void delay_ms(uint);  //延时子函数void init();    //程序定时中断外部中断初始化void system_on();  //系统上电,两只黄灯闪烁3秒的子函数void display_lcd();  //LCD显示子函数void display_light(); //LED灯的显示void judge();   //各状态切换的判断函数void yellow_light();  //黄灯闪烁3秒的子函数void GoStraight();  //直行通路的子函数void TurnLeft();   //左拐通路的子函数//===========================bit  direction;   //0->南北方向;1->东西方向;bit  flash_flags;  //闪烁状态变量bit  change_flags;  //计时结束变量bit  Lock_flags;   //0->交通不封锁;1->交通封锁uint ovf_cnt;   //定时中断计数变量uchar mode_flags;   //按键改变变量uchar run_flags;   //运行时的状态变量uchar time_temp;   //计时时间缓存uchar north_green_time=10;//南北直行绿灯时间初始化uchar east_green_time=10; //东西直行绿灯时间初始化/*左转绿灯时间与通行绿灯时间相等*//*-红灯时间为绿灯时间加黄灯时间-*//*------黄灯时时间固定为3秒-----*///uchar north_red_time;//uchar east_red_time;//uchar north_turn_time;//uchar east_turn_time;//===========================//红灯切换到绿灯时,黄灯不用闪烁//绿灯切换到红灯时,黄灯需要闪烁//黄灯闪烁时,绿灯没有熄灭//红灯时间=绿灯时间+黄灯时间//左拐时间=直行绿灯时间//============================================//********************************************void main(){ init();   //定时中断外部中断初始化 LCD_init();  //LCD1602初始化 /*首次运行时,黄灯闪烁三秒*/ LCD_str(1,1,"System on ..."); time_temp=yellow_time; system_on();  //两只黄灯闪烁子函数 while(1) {  judge();  /*LED灯的显示*/  display_light();  /*1602显示*/  display_lcd(); }}//*********************************************//=============================================void init(){ TMOD|=0x02;  //定时器0方式2,自动重载 TH0=5;TL0=5; //250us中断一次 ET0=1;   //允许定时器0中断 TCON|=0x01;  //外部中断0下降沿触发 EX0=1;    //允许外部中断0 EA=1;   //允许全局总中断 TR0=1;    //启动定时器0}//===========================void system_on() //上电后,东西南北路口黄灯闪烁3秒{ do{  if(flash_flags) {North_Y=OFF;East_Y=OFF;}  else   {North_Y=ON; East_Y=ON; }  }while(!change_flags);//首次计时未完成不改变状态}//===========================void display_lcd(){  if((!mode_flags)&&(!Lock_flags)){//正常显示状态  switch(run_flags){  case 0:case 2:case 4:case 6:case 8: //黄灯闪烁状态   if(!direction){     if(flash_flags) {LCD_str(1,1,"                ");}    else   {LCD_str(1,1,"Status Change...");}   }else{    if(flash_flags) {LCD_str(1,1,"                ");}    else   {LCD_str(1,1,"Status Change...");}   }   break;  case 1:case 5:       //直行状态   if(!direction) {LCD_str(1,1,"North GoStraight");}   else   {LCD_str(1,1,"East  GoStraight");}   break;  case 3:case 7:         //左拐状态   if(!direction) {LCD_str(1,1,"North Turn Left ");}   else   {LCD_str(1,1,"East  Turn Left ");}   break;  }  LCD_str(2,6,"         ");   //第二行清屏 } if((mode_flags==1)&&(!Lock_flags)){//更改南北直通时间状态  LCD_str(1,1,"North Green Time");  LCD_str(2,6,"To: ");  LCD_int_(north_green_time);  LCD_str_("S  "); } if((mode_flags==2)&&(!Lock_flags)){//更改东西直通时间状态  LCD_str(1,1,"East  Green Time");  LCD_str(2,6,"To: ");  LCD_int_(east_green_time);  LCD_str_("S  "); } if(!Lock_flags){/*当前状态剩余时间的显示*/  LCD_int(2,1,time_temp);  LCD_char(' ');  LCD_str(2,4,"S  ") } if(Lock_flags){ //封锁交通时显示  if(flash_flags){   LCD_str(1,1,"                ");   LCD_str(2,1,"                ");  }else{   LCD_str(1,1," Please Wait... ");   LCD_str(2,1,"   Locking...   ");  } }}//===========================void display_light(){ if(!Lock_flags){//交通未被封锁时,交通灯按以下程序显示  /*以下由run_flags状态决定灯的亮灭*/  switch(run_flags){   case 0:case 2:case 4:case 6:case 8:    yellow_light(); break;   case 1:case 5:    GoStraight(); break;   case 3:case 7:    TurnLeft();  break;   default: run_flags=1;      }  /*红灯的亮灭*/  if(!direction){ //南北通行时,东西绿灯全灭,红灯全亮   East_R=ON; East_turn_R=ON;   East_G=OFF; East_turn_G=OFF;   East_Y=OFF;  }else{   //东西通行时,南北绿灯全灭,红灯全亮   North_R=ON; North_turn_R=ON;   North_G=OFF;North_turn_G=OFF;   North_Y=OFF;  } }else{//封锁交通时,所有红灯亮  North_R=ON; North_turn_R=ON;  North_G=OFF;North_turn_G=OFF;  North_Y=OFF;East_Y=OFF;  East_R=ON; East_turn_R=ON;  East_G=OFF; East_turn_G=OFF;  }}//===========================void yellow_light()    /*黄灯闪烁的子函数*/{ if(!direction){  //North_G=OFF;    //南北直行绿灯灭  //North_turn_G=OFF;  if(flash_flags) {North_Y=OFF;}  else   {North_Y=ON; } }else{  //East_G=OFF;  //East_turn_G=OFF;  if(flash_flags) {East_Y=OFF; }  else   {East_Y=ON;  } } }//===========================void GoStraight(){   /*直行通路的子函数*/ if(!direction){  North_Y=OFF;   //南北黄灯灭  North_turn_R=ON;  //南北左拐红灯亮  North_R=OFF;   //南北直行红灯灭  North_G=ON;    //南北直行绿灯亮      }else{  East_Y=OFF;  East_turn_R=ON;  East_R=OFF;  East_G=ON;    }}//===========================void TurnLeft()    /*左转通路的子函数*/{ if(!direction){  North_G=OFF;   //南北直行绿灯灭  North_R=ON;     //南北直行红灯亮  North_turn_R=OFF;  //南北左拐红灯灭  North_turn_G=ON;   //南北左拐绿灯亮    }else{  East_G=OFF;  East_R=ON;  East_turn_R=OFF;  East_turn_G=ON;   }}//===========================void judge()     /*状态切换判断函数*/{  if(change_flags){  //状态计时结束标志   change_flags=0;  //状态标志清零   run_flags++;  //状态切换   if((run_flags==5)||(run_flags==9))direction=~direction;   switch(run_flags){ //状态时间赋值    case 0:case 2:case 4:case 6:case 8:  //黄灯闪烁     time_temp=yellow_time;break;    case 1:time_temp=north_green_time;break;//南北直行    case 5:time_temp=east_green_time;break; //东西直行        case 3:time_temp=north_green_time;break;//南北左拐    case 7:time_temp=east_green_time;break; //东西左拐         default:run_flags=1;time_temp=north_green_time;   }  }}//===========================void keys_scan() interrupt 0 /*按键扫描子函数*/{ delay_ms(20); if(!INT0){  if(!MODE_KEY){   if(++mode_flags>=3)mode_flags=0;  }  if(!TIME_ADD){   if(mode_flags==1)north_green_time+=5;   if(mode_flags==2)east_green_time+=5;   if(north_green_time>90)north_green_time=5;   if(east_green_time>90)east_green_time=5;  }  if(!TIME_DEC){   if(mode_flags==1)north_green_time-=5;   if(mode_flags==2)east_green_time-=5;   if(north_green_time>90)north_green_time=5;   if(east_green_time>90)east_green_time=5;     }  /*还可以在此处添加状态,用以改变左拐通行时间*/  if(!LOCK_KEY)Lock_flags=~Lock_flags;  } INT0=1;}//===========================void T0_isr() interrupt 1{ if(ovf_cnt==1846)flash_flags=~flash_flags; if(++ovf_cnt>=3691){  ovf_cnt=0;  flash_flags=~flash_flags;  if(!Lock_flags)if(time_temp--==1)change_flags=1; }} //===========================void delay_ms(uint xms){ uint i,j; for(i=0;i<xms;i++)  for(j=0;j<990;j++);}本设计的源文件与hex文件下载:http://www.51hei.com/ziliao/file/jiaotd1.rar,您的电路如果不同请自行更改端口.
/*--------------文件名:LCD1602.H----------------*/#ifndef  _LCD1602_H_#define  _LCD1602_H_#define  uint unsigned int#define  uchar unsigned char#define  LCM_P   P2    //LCD的控制端口#define  LCM_DATA  P0    //LCD的数据口#define  LCM_RS_0  LCM_P&=~(1<<5)#define  LCM_RS_1  LCM_P|=  1<<5#define  LCM_RW_0  LCM_P&=~(1<<6)#define  LCM_RW_1  LCM_P|=  1<
锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台