新闻中心

EEPW首页>嵌入式系统>设计应用> 基于STC12C5A32S2单片机设计的交通灯

基于STC12C5A32S2单片机设计的交通灯

作者: 时间:2012-10-24 来源:网络 收藏

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

#includereg52.h>#includeintrins.h>#includeLCD1602.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; //允许外部中断0EA=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;ixms;i++)for(j=0;j990;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=~(15)#define LCM_RS_1 LCM_P|= 15#define LCM_RW_0 LCM_P=~(16)#define LCM_RW_1 LCM_P|= 16 #define LCM_EN_0 LCM_P=~(17)#define LCM_EN_1 LCM_P|= 17/*========================================*/#define LCD_str(x,y,s) Locate(x,y);LCD_str_(s);#define LCD_int(x,y,n) Locate(x,y);LCD_int_(n);/*========================================*/void LCD_init(); //LCM1602的初始化函数,在使用1602之前都必须调用void Locate(uchar,uchar); //显示定位函数void LCD_half(uchar); //送半字节函数void LCD_char(uchar); //写一个字符函数void LCD_cmd(uchar); //写命令函数void LCD_int_(int); //写整型数据函数void LCD_str_(uchar str[]);//写字符串数据函数void LCD_delay(uint); //延时子函数/******************************************//*-函数功能:液晶使用初始化---------------*//*-入口参数:无*//******************************************/void LCD_init(){LCD_cmd(0x01);LCD_delay(10);LCD_cmd(0x28); //4位数据、双行显示、5*7(0x38为八位)LCM_EN_1;_nop_();_nop_();_nop_();LCM_EN_0; /*此处必须加上这两句*/LCD_delay(10);LCD_cmd(0x28);LCD_cmd(0x06);LCD_cmd(0x0c);LCD_cmd(0x01);LCD_delay(10);}/******************************************//*-函数功能:显示数据定位函数-------------*//*-入口参数:行坐标x、列坐标y-------------*//******************************************/ void Locate(uchar x,uchar y){x=0x01;LCD_cmd((x==0)?(y+0xbf):(y+0x7f));}/******************************************//*-函数功能:送半字节函数-----------------*//*-入口参数:要写到液晶指令或数据寄存器的-*//* 字节的高四位或低四位---------*//******************************************/void LCD_half(uchar dataw_){LCM_DATA=(LCM_DATA0x0f)|(dataw_);LCM_EN_1;_nop_();_nop_();_nop_();LCM_EN_0;LCD_delay(1);//实际使用中加上10ms的延时}/******************************************//*-函数功能:写一位数据函数---------------*//*-入口参数:数据内容---------------------*//******************************************/void LCD_char(uchar dataw){LCM_RS_1;LCM_RW_0;LCM_EN_0;_nop_();LCD_half(dataw0xf0);LCD_half(dataw4); }/*========================================*/void LCD_cmd(uchar cmd){ LCM_RS_0;LCM_RW_0;LCM_EN_0;_nop_();LCD_half(cmd0xf0);LCD_half(cmd4);} /*========================================*/void LCD_str_(uchar *str){while(*str)LCD_char(*str++);}/*========================================*/void LCD_int_(int num){uchar counter=0,num_str[10],neg_flags=0;if(num0){num=-num,neg_flags=1;}do{num_str[counter++]=num%10+0x30;num/=10; }while(num!=0);//循环结束后,counter的值比数长多1 if(neg_flags){num_str[counter++]='-';}while(counter) { LCD_char(num_str[--counter]);}//counter的值必须先减一 }/*========================================*/void LCD_delay(uint xus){uint i=0,j=0;for(i=0;ixus;i++)for(j=0;j123;j++);}/*========================================*/#endif


评论


相关推荐

技术专区

关闭