《51单片机交通灯(加急救车)(共7页).doc》由会员分享,可在线阅读,更多相关《51单片机交通灯(加急救车)(共7页).doc(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上51单片机控制交通灯一、实现功能:1、先南北红灯亮,东西绿灯亮。南北红灯亮维持25秒,在南北红灯亮的同时东西绿灯也亮,并维持20秒;到20秒时,东西绿灯闪亮,闪亮3秒后熄灭。在东西绿灯熄灭时,东西黄灯亮,并维持2秒。到2秒时,东西黄灯熄灭,东西红灯亮,同时,南北红灯熄灭,绿灯亮,东西红灯亮维持30秒。南北绿灯亮维持25秒,然后闪亮3秒后熄灭。同时南北黄灯亮,维持2秒后熄灭,这时南北红灯亮,东西绿灯亮。周而复始。2、用一个按键模拟急救车到达。急救车到达路口时,四个方向的红灯全亮,10秒钟后恢复正常显示。3、一辆急救车正在过路口时(即四个方向全红灯10秒倒计时未结束),又
2、来了一辆急救车,重新从10秒开始倒计时。4、急救车过去之后(即10秒倒计时结束),路灯状态要继续急救车到达之前的状态往下运行。5、数码管显示绿灯变红灯、红灯变绿灯以及急救车10秒钟的倒计时。6、急救车从路口过的时候,蜂鸣器响1s停1s的循环报警。二、proteus仿真电路图注:此图仅作为仿真使用。实际焊接电路时,由于单片机的驱动能力较弱,所以数码管的位选、LED的控制最好加上三极管进行电流放大,否则即使能实现功能,但是LED和数码管的发光的亮度也不强。三、 C语言程序程序#include #define uchar unsigned char#define uint unsigned int#
3、define duan P0#define wei P2code uchar tab=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf;uchar buff8 = 10,10,10,10,10,10,10,10;uchar weizhi = 0;uchar alarm = 0;uchar bk_TH1 = 0;uchar bk_TL1 = 0;uchar bk_cnt = 0;uchar bk_cnt_time = 0;uchar cnt_time = 0;uchar cnt = 0;uchar cnt_flash = 0;
4、bit cnt_finish = 0;bit LED = 0;bit beef_flag = 1;sbit Er = P10;/东西红灯sbit Eg = P11;/东西绿灯sbit Ey = P12;/东西黄灯sbit Sr = P13;/南北红灯sbit Sg = P14;/南北绿灯sbit Sy = P15;/南北黄灯sbit beef = P16;/蜂鸣器void display(void);void main(void)uchar time = 0;TMOD |= 0x11;TH0 = (65536-2000)/256;TL0 = (65536-2000)%256;TH1 = (65
5、536-50000)/256;TL1 = (65536-50000)%256;ET1 = 1;ET0 = 1;INT0 = 1;EX0 = 1;IP |= 0x02; EA = 1;TR0 = 1;TR1 = 1;Sr = 0;Sg = 0;Sy = 0;Er = 0;Eg = 0;Ey = 0;beef = 0;buff0 = 11;buff1 = 11;buff2 = 11;buff3 = 11;buff4 = 11;buff5 = 11;buff6 = 10;buff7 = 10;while(1)while(cnt20)&(alarm = 0)Sr = 1;Sg = 0;Sy = 0;E
6、r = 0;Eg = 1;Ey = 0;time = (25-cnt);buff7 = time%10;buff6 = time/10;while(cnt23)&(alarm = 0)Sr = 1;Sg = 0;Sy = 0;Er = 0;Eg = LED;Ey = 0;time = (25-cnt);buff7 = time%10;buff6 = time/10;while(cnt25)&(alarm = 0)Sr = 1;Sg = 0;Sy = 0;Er = 0;Eg = 0;Ey = 1;time = (25-cnt);buff7 = time%10;buff6 = time/10;wh
7、ile(cnt50)&(alarm = 0)Sr = 0;Sg = 1;Sy = 0;Er = 1;Eg = 0;Ey = 0;time = (55-cnt);buff7 = time%10;buff6 = time/10;while(cnt53)&(alarm = 0)Sr = 0;Sg = LED;Sy = 0;Er = 1;Eg = 0;Ey = 0;time = (55-cnt);buff7 = time%10;buff6 = time/10;while(cnt_finish != 1)&(alarm = 0)Sr = 0;Sg = 0;Sy = 1;Er = 1;Eg = 0;Ey
8、= 0;time = (55-cnt);buff7 = time%10;buff6 = time/10;cnt_finish = 0;while(alarm = 1)Er = 1;Eg = 0;Ey = 0;Sr = 1;Sg = 0;Sy = 0;time = (10-cnt);buff7 = time%10;buff6 = time/10;if(cnt = 10)TR1 = 0;TH1 = bk_TH1;TL1 = bk_TL1;cnt = bk_cnt;cnt_time = bk_cnt_time;TR1 = 1;alarm = 0;beef = beef_flag; beef = 0;
9、void int0(void) interrupt 0TR1 = 0;if(alarm = 0)bk_TH1 = TH1;bk_TL1 = TL1;bk_cnt = cnt;bk_cnt_time = cnt_time;TH1 = (65536-50000)/256;TL1 = (65536-50000)%256;cnt = 0;cnt_time = 0;alarm = 1;TR1 = 1;void timer0(void) interrupt 1TH0 = (65536-2000)/256;TL0 = (65536-2000)%256;display();void timer1(void) interrupt 3TH1 = (65536-50000)/256;TL1 = (65536-50000)%256;if(+cnt_time = 20)cnt_time = 0;cnt+;beef_flag = beef_flag;if(+cnt_flash = 4)cnt_flash = 0;LED = LED;if(cnt = 55)cnt = 0;cnt_finish = 1;void display(void)duan = 0xff;wei = (1weizhi);duan = tabbuffweizhi;if(+weizhi = 8)weizhi = 0;专心-专注-专业
限制150内