《基于52单片机的城市十字路口交通灯课程设计.doc》由会员分享,可在线阅读,更多相关《基于52单片机的城市十字路口交通灯课程设计.doc(22页珍藏版)》请在三一办公上搜索。
1、摘 要本设计是一个以微电子技术,计算机和通信技术为先导的,而计算机技术怎样与实际应用更有效的结合并有效的发挥其作用是科学界最热门的话题,也是当今计算机应用中空前活跃的领域。十字路口车辆穿梭,行人熙攘,车行车道,人行人道,有条不紊。那么靠什么来实现这井然秩序呢?靠的就是交通信号灯的自动指挥系统。本系统由单片机系统、按键、四位数码管显示、交通灯演示系统组成。设计一个用于东西、南北走向的交通管理。南北方向(主干道)车道和东西方向(支干道)车道两条交叉道路上的车辆交替运行,主干道每次通行时间都设为30秒、支干道每次通行间为20秒。系统除基本交通灯功能外,还具有倒计时、时间设置、紧急情况处理、分时段调整
2、信号灯的点亮时间以及根据具体情况手动控制等功能。本系统结构简单,操作方便;可实现自动控制,具有一定的智能性;对优化城市交通具有一定的意义。本设计将各任务进行细分包装,使各任务保持相对独立;能有效改善程序结构,便于模块化处理,使程序的可读性、可维护性和可移植性都得到进一步的提高。近年来随着科技的飞速发展,单片机的应用正在不断深入,同时带动传统控制检测技术日益更新。在实时检测和自动控制的单片机应用系统中,单片机往往作为一个核心部件来使用,仅单片机方面知识是不够的,还应根据具体硬件结构软硬件结合,加以完善。 本文主要从单片机的应用上来实现十字路口交通灯智能化的管理,用以控制过往车辆的正常运作。关键词
3、:交通灯;单片机;AT89S52目录1 概述31.1设计背景31.2设计任务32 系统总体方案及硬件设计 42.1 AT89S52单片机简介42.2 系统硬件电路的设计43 软件设计73.1 交通灯的设计程序流程图73.2定时器0及中断响应84 Proteus软件仿真104.1仿真电路图 104.2 仿真步骤105 课程设计体会11参考文献11附1: 源程序代码 12附2: 系统原理图 181 概述1.1 设计背景如今随着人们生活水平的提高,车辆越来越多,交通事故频繁发生。交通信号灯的出现,使交通得以有效管制,对于疏导交通流量,提高道路通行能力,减少交通事故有明显效果。交通灯在城市交通中起着重
4、要的作用,它与人们日常生活密切相关,是人们出行的安全保障。因此提供一个可靠、安全、便捷的多功能交通灯控制系统有着现实的必要性。为了解决这些问题,我们更应该提高交通控制和管理水平,合理使用现有交通设施,充分发挥其能力,提高交通效率,促进和谐交通的建立。近年来随着科技的飞速发展,单片机的应用正在不断地走向深入,同时带动传统控制检测日新月益更新。在实时检测和自动控制的单片机应用系统中,单片机往往是作为一个核心部件来使用,仅单片机方面知识是不够的,还应根据具体硬件结构,以及针对具体应用对象特点的软件结合,加以完善。本系统设计一个基于单片机的交通灯控制系统。能方便的对交通灯进行控制,使交通更和谐。1.2
5、设计任务 1)设计一个十字路口的交通灯控制电路,要求南北方向(主干道)车道和东西方向(支干道)车道两条交叉道路上的车辆交替运行,主干道每次通行时间都设为30秒、支干道每次通行间为20秒,时间可设置修改。2)在绿灯转为红灯时,要求黄灯先亮5秒钟,才能变换运行车道;3)黄灯亮时,要求每秒闪亮一次。4)东西方向、南北方向车道除了有红、黄、绿灯指示外,每一种灯亮的时间都用显示器进行显示(采用计时的方法)。5)一道有车而另一道无车(实验时用开关 K0 和 K1 控制),交通灯控制系统能立即让有车道放行。 6)有紧急车辆要求通过时,系统要能禁止普通车辆通行,A、B道均为红灯,紧急车由K2 开关模拟。2 系
6、统总体方案及硬件设计2.1 AT89S52单片机简介AT89S52是一种低功耗、高性能CMOS8位微控制器。片上Flash允许程序存储器在系统可编程,亦适于常规编程器。在单芯片上,拥有灵巧的8位CPU和在系统可编程Flash,使得AT89S52为众多嵌入式控制应用系统提供高灵活、超有效的解决方案。AT89S52具有以下标准功能:8k字节Flash,256字节RAM,32位I/O口线,看门狗定时器,2个数据指针,三个16位定时器/计数器,一个6向量2级中断结构,全双工串行口,片内晶振及时钟电路。空闲模式下,CPU停止工作,允许RAM、定时器/计数器、串口、中断继续工作。鉴于以上的优点本系统采用A
7、T89S52作为主控芯片,实现对整个系统的控制。2.2 系统硬件电路的设计(1)时钟电路设计图2-1时钟电路原理图如图2-1所示,采用内部时钟产生方式,在XTAL1和XTAL2两端跨接晶体或陶瓷振荡器,与内部反相器构成稳定的自击荡器。其发出的时钟脉冲直接送入片内定时控制部件。(2)复位电路设计 图2-2上电+按钮电平复位电路原理图如图2-2所示,采用上电+按钮电平复位方式,当按下按钮时,RST管脚高电平触发。为保证复位可靠,RC时间常数应大于两个机器周期,一般电容去22uF,电阻取1K。(3)灯控制电路设计 图2-3交通灯状态显示电路如图2-3所示,交通灯状态显示电路由东西南北四个方向各三个L
8、ED灯组成,分别显示四个方向上红、黄、绿三个状态,用以指示十字路口各方向车辆的行驶。通过软件编程,可使路口交通变化情况为:南北方向(主干道)车道和东西方向(支干道)车道两条交叉道路上的车辆交替运行,主干道每次通行时间都设为30秒、支干道每次通行间为20秒;在绿灯转为红灯时,要求黄灯先亮5秒钟,才能变换运行车道;黄灯亮时每秒闪亮一次。(4)倒计时显示电路设计图2-4 数码管显示电路原理图如图2-4所示,由一个共阳极四位数码管来实现倒计时。由于P0口输出电流小,需外接上拉电阻,COME端接5V电源。(5)按键控制电路设计图2-5键盘电路原理图如图2-5所示,开光控制由PD1-PD5四个连接到单片机
9、的控制按钮组成。通过软件编程,按下PD1电路进入外部中断响应,数码显示时间将停止倒计时,此时,可通过按钮PD3和PD4设置通行时间,每按一下PD3时间增加1s,而按下PD4时间减少1s。按下PD5将使程序重新进入循环点亮状态。3软件设计3.1 交通灯的设计程序流程图南北通行开始中断?中断程序返回?K0=0?K1=0?DX=0?东西通行中断?中断程序NB=0?K0=0?K1=0?返回?NYNYYNNYNNNNNNYYYYYY3.2定时器0及中断响应定时溢出计数初值加一 开始 N定时器0初始化 Y 计数初值=99? N Y 计数初值=04 Proteus软件仿真4.1 仿真图4.2 仿真步骤(1)
10、根据电路图选择器件连接电路(2)双击AT89S52装入源程序编译生成的HEX文件(3)单击运行按钮运行仿真(4)根据仿真情况与程序实现任务对比,对于不能实现的任务修改并调试程序,重新装载重新运行调试仿真,直到能完全实现所要求的功能为止(5)进一步改进和简化程序在进行调试仿真5 课程设计体会通过这次单片机实训课程学习,才深刻体会到自己实际操作能力的匮乏。从刚开始老师对单片机的整体介绍及其应用前景,对单片机产生了浓厚的兴趣,并利用自己的课余时间提前学习有关单片机的基本知识。但空有理路知识,却无半点的实际操作经验。后来在老师的建议下,选择运用单片机设计一个交通灯控制系统,从简单开始,后期逐步复杂化。
11、从给定电路图的分析到实际电路元件的识别、焊接,再到后期的程序设计以及系统调试,初步掌握了运用单片机系统设计特定功能的设计步骤,也对单片机有了更深的体会。了解和掌握了一些简单的编程思想,对单片机各管脚的功能,I/O口的使用条件都有了更深的理解。这次的课程设计让我把单片机的理论知识运用的实践中,实现了理论与实践的相结合,从中更懂得了理论是实践的基础,实践更能检验理论的真实性,让我受益匪浅。参考文献:1 李朝青.单片机原理及接口技术(修订版).北京:北京航空航天大学出版社,19982 李广弟.单片机基础.北京:北京航空航天大学出版社,19923 何立民.单片机应用技术大全.北京:北京航空航天大学出版
12、社, 19944 张毅刚. 单片机原理及接口技术.哈尔滨:哈尔滨工业大学出版社,19905 谭浩强.单片机课程设计. 北京:清华大学出版社,19896 余发山.单片机原理及应用技术 焦作:中国矿业大学出版社,2007附录1 源程序代码 /#include #include / 对单片机的口进行了定义#define uint unsigned int#define uchar unsigned charuchar code a10=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f;/段码组合,共阴极uchar code b4=0xfe,0xfd,
13、0xfb,0xf7; /位码组合 低有效uchar code c4=0xcc,0xd4,0x78,0xb8;/*P1 绿红、黄红、红绿、红黄*/ /12MHz /*uchar NB=25,DX=30,NBG=25,DXG=15,H=5; /*数码管显示值设置*/uchar i,k=0,cnt=0,j=0; sbit K0=P35; /*支干道通行*/sbit K1=P36; /*主干道通行*/sbit K4=P37; /*返回*/sbit K3=P32;/*设置时间*/sbit K2=P33;/*紧急刹车*/void delay(uchar t);/*定义延时程序*/void key();/*
14、定义键盘程序*/void display();/*定义显示程序*/void settime();/*定义时间设置显示程序*/*程序初始*void init(void)TMOD=0x01; /*使用定时器0模式一*/TH0=0x3c; /*(65536-5000)/256*/TL0=0xb0; /*(65536-5000)%256*/IT0=1; /开中断ET0=1;TR0=1;EA=1;EX0=1;EX1=1;P1=ck; /开启交通灯/*中断0处理程序*void int0(void) interrupt 0 /通行时间设置 EA=0; P1=0xd8;/*红灯全亮*/ for(;) sett
15、ime(); if(K1=0) /*P36=0设置主干道通行时间*/ delay(40); if(K1=0) while(!K1) settime(); NBG+;/*主干道通行时间加1*/ if(NBG=100) NBG=0; if(K0=0) /*P35=0设置支干道通行时间*/ delay(40); if(K0=0) while(!K0) settime(); DXG+;/*支干道通行时间加1*/ if(DXG=100) DXG=0; if(K4=0) /*P37=0返回*/ delay(40); if(K4=0) while(!K4) k=0;P1=ck; NB=NBG,DX=NBG+
16、H; display(); EA=1; break; /*中断1处理程序*void int1(void) interrupt 2 /紧急情况 /*东西南北红灯亮*/EA=0;TR0=!TR0; /*停止计数*/for(;) /*主支干道显示全为0*/ P1=0xd8,P0=a0; P2=0xfe; delay(20); P2=0xfd; delay(20); P2=0xfb; delay(20); P2=0xf7; delay(20); if(K4=0) /*返回*/ delay(20);EA=1; P1=ck; /*返回紧急前状态开始计数*/TR0=!TR0;/*重启计数*/break; v
17、oid time1(void) interrupt 1/*交通灯控制程序*/TH0=0x3c;/*定时50ms*/TL0=0xb0; cnt+;if(cnt=20) /*每20*50ms=1s自减一*/NB-;DX-;cnt=0;if(NB=0|DX=0)k+;if(k3)/*k取0,1,2,3*/k=0;switch(k)case 0:NB=NBG,DX=NBG+H;j=0;P1=ck;break; /*主干道通行显示绿红*/ case 1:NB=H;j=1;P1=ck;break; /*主干道黄灯闪烁*/case 2:NB=DXG+H,DX=DXG;j=0;P1=ck;break; /*支
18、干道通行显示红绿*/case 3:DX=H;j=2;P1=ck;break; /*支干道黄灯闪烁*/ void delay(uchar t) /延时程序延时0.1*nms uchar i; do for(i=0;i0;i-)display();/*执行for循环延时*/ P1=ck;/*再给黄灯一低电平黄灯亮*/ for(i=83;i0;i-)display();while(j=2) /*支干道黄灯闪烁*/ P1=0xf8; for(i=83;i0;i-)display(); P1=ck; for(i=83;i0;i-)display(); 附录2 系统原理图Emloyment tribuna
19、ls sort out disagreements between employers and employees. You may need to make a claim to an employment tribunal if: you dont agree with the disciplinary action your employer has taken against you your employer dismisses you and you think that you have been dismissed unfairly. For more informu, tak
20、e advice from one of the organisations listed underFurther help. Employment tribunals are less formal than some other courts, but it is still a legal process and you will need to give evidence under an oath or affirmation. Most people find making a claim to an employment tribunal challenging. If you
21、 are thinking about making a claim to an employment tribunal, you should get help straight away from one of the organisations listed underFurther help. ation about dismissal and unfair dismissal, seeDismissal. You can make a claim to an employment tribunal, even if you haventappealedagainst the disc
22、iplinary action your employer has taken against you. However, if you win your case, the tribunal may reduce any compensation awarded to you as a result of your failure to appeal. Remember that in most cases you must make an application to an employment tribunal within three months of the date when t
23、he event you are complaining about happened. If your application is received after this time limit, the tribunal will not usually accept i. If you are worried about how the time limits apply to you If you are being represented by a solicitor at the tribunal, they may ask you to sign an agreement whe
24、re you pay their fee out of your compensation if you win the case. This is known as adamages-based agreement. In England and Wales, your solicitor cant charge you more than 35% of your compensation if you win the case.youre clear about the terms of the agreement. It might be best to get advice from
25、an experienced adviser, for example, at a Citizens Advice Bureau. To find your nearest CAB, including those that give advice by e-mail, click onnearest CAB.For more information about making a claim to an employment tribunal, seeEmployment tribunals.The (lack of) air up there Watch m Cay man Islands-
26、based Webb, the head of Fifas anti-racism taskforce, is in London for the Football Associations 150th anniversary celebrations and will attend Citys Premier League match at Chelsea on Sunday.I am going to be at the match tomorrow and I have asked to meet Ya ya Toure, he told BBC Sport.For me its abo
27、ut how he felt and I would like to speak to him first to find out what his experience was.Uefa hasopened disciplinary proceedings against CSKAfor the racist behaviour of their fans duringCitys 2-1 win.Michel Platini, president of European footballs governing body, has also ordered an immediate inves
28、tigation into the referees actions.CSKA said they were surprised and disappointed by Toures complaint. In a statement the Russian side added: We found no racist insults from fans of CSKA.Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life
29、 became the appearance of the same day; May be back in the past, to oneself the paranoid weird belief disillusionment, these days, my mind has been very messy, in my mind constantly. Always feel oneself should go to do something, or write something. Twenty years of life trajectory deeply shallow, su
30、ddenly feel something, do it.一字开头的年龄已经到了尾声。或许是愧疚于自己似乎把转瞬即逝的很多个不同的日子过成了同一天的样子;或许是追溯过去,对自己那些近乎偏执的怪异信念的醒悟,这些天以来,思绪一直很凌乱,在脑海中不断纠缠。总觉得自己似乎应该去做点什么,或者写点什么。二十年的人生轨迹深深浅浅,突然就感觉到有些事情,非做不可了。The end of our life, and can meet many things really do?而穷尽我们的一生,又能遇到多少事情是真正地非做不可?During my childhood, think lucky money a
31、nd new clothes are necessary for New Year, but as the advance of the age, will be more and more found that those things are optional; Junior high school, thought to have a crush on just means that the real growth, but over the past three years later, his writing of alumni in peace, suddenly found th
32、at isnt really grow up, it seems is not so important; Then in high school, think dont want to give vent to out your inner voice can be in the high school children of the feelings in a period, but was eventually infarction when graduation party in the throat, later again stood on the pitch he has swe
33、at profusely, looked at his thrown a basketball hoops, suddenly found himself has already cant remember his appearance.童年时,觉得压岁钱和新衣服是过年必备,但是随着年龄的推进,会越来越发现,那些东西根本就可有可无;初中时,以为要有一场暗恋才意味着真正的成长,但三年过去后,自己心平气和的写同学录的时候,突然就发现是不是真正的成长了,好像并没有那么重要了;然后到了高中,觉得非要吐露出自己的心声才能为高中生涯里的懵懂情愫划上一个句点,但毕业晚会的时候最终还是被梗塞在了咽喉,后来再次
34、站在他曾经挥汗如雨的球场,看着他投过篮球的球框时,突然间发现自己已经想不起他的容颜。Originally, this world, can produce a chemical reaction to an event, in addition to resolutely, have to do, and time.原来,这个世界上,对某个事件能产生化学反应的,除了非做不可的坚决,还有,时间。A persons time, your ideas are always special to clear. Want, want, line is clear, as if nothing could
35、 shake his. Also once seemed to be determined to do something, but more often is he backed out at last. Dislike his cowardice, finally found that there are a lot of love, there are a lot of miss, like shadow really have been doomed. Those who do, just green years oneself give oneself an arm injection, or is a self-righteous spiritual.一个人的时候,自己的想法总是特别地清晰。想要的,不想要的,界限明确,好像没有什么可以撼动自己。也曾经好像已经下定了决心去做某件事,但更多的时候是最后又打起了退堂鼓。嫌恶过自己的怯懦,最终却发现有很多缘分,有很多错过,好像冥冥之中真的已经注定。那些曾经所谓的非做不可,只是青葱年华里自己给自己注射的一支强心剂,或者说,是自以为是的精神寄托罢了。At the moment, the sky is dark, the air is fresh factor afte