自动控制交通灯C语言程序

合集下载

交通灯控制c语言程序绝对正确

交通灯控制c语言程序绝对正确
RED_A=1;
if(++Flash_Count!=6) return; //闪烁
Flash_Count=0;
Operation_Type=1;
break;
}
}
//主程序
void main()
{
TMOD=0x11; //T0方式1
EA=1;
ET0=1; //定时器0 中断允许位
TL1=0X00;
if(++count1==200) //10S(50MS*200=10000MS)
{
TR0=1;
count1=0;
}
}
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit RED_A=P1^5; //东西向指示灯
sbit YELLOW_A=P1^4;
sbit GREEN_A=P1^3;
sbit RED_B=P1^2; //南北向指示灯
if(++Time_Count!=20) return; // 1s(50*20=1000ms)
Time_Count=0;
YELLOW_B=~YELLOW_B;GREEN_B=1;
RED_B=1;
if(++Flash_Count!=6) return; //闪烁次数
Flash_Count=0;
Operation_Type=3;
break;
case 3: //东西向红灯与南北向绿灯亮5s
RED_A=1;YELLOW_A=1;GREEN_A=0;

51单片机用C语言实现交通灯(红绿灯)源程序

51单片机用C语言实现交通灯(红绿灯)源程序

51单片机用C语言实现交通灯(红绿灯)源程序2009-10-29 23:00交通灯,红黄绿灯交替亮,怎样实现呢?其实就是根据单片机定时器及倒计时的程序修改。

源程序如下:/*1、程序目的:使用定时器学习倒计时红绿灯原理主要程序和倒计时一样2、硬件要求:数码管、晶振12M*/#include <reg52.h>bit red,green,yellow,turnred; //定义红、黄、绿及转红标志code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共阴数码管 0-9unsigned char Dis_Shiwei;//定义十位unsigned char Dis_Gewei; //定义个位void delay(unsigned int cnt) //用于动态扫描数码管的延时程序{while(--cnt);}main(){TMOD |=0x01;//定时器设置 10ms in 12M crystal定时器0,工作方式1,16位定时器TH0=0xd8; //65535-10000=55535=D8F0(十六进制)TL0=0xf0;IE= 0x82; //中断控制,EA=1开总中断,ET0=1:定时器0中断允许TR0=1; //开定时器0中断P1=0xfc;//红灯亮,根据红黄绿接灯的顺序。

red =1;while(1){P0=Dis_Shiwei;//显示十位,这里实现用8位数码管,即左1位P2=0;delay(300);//短暂延时P0=Dis_Gewei; //显示个位,左数,2位P2=1;delay(300);}}/********************************//* 定时中断 *//********************************/void tim(void) interrupt 1 using 1{static unsigned char second=60,count; //初值60TH0=0xd8;//重新赋值,10毫秒定时TL0=0xf0;count++;if (count==100){count=0;second--;//秒减1if(second==0){ //这里添加定时到0的代码,可以是灯电路,继电器吸合等,或者执行一个程序 if(red) //红灭,先转黄{red=0;yellow=1;second=4;P1=0xF3;//黄灯亮4秒,黄灯为过渡灯,再根据情况转绿或转红}else if(yellow && !turnred){yellow=0;green=1;second=25;P1=0xCF;//绿灯亮25秒,}else if(green){yellow=1;green=0;second=4;P1=0xF3;//黄灯亮4秒turnred=1;}else if(yellow && turnred) //绿灯灭,转黄灯,后红灯,turnred=1时{red=1;yellow=0;P1=0xFC;//红灯亮60秒second=60;turnred=0; //接下来是转黄,绿。

交通信号灯C语言源程序

交通信号灯C语言源程序
{
disp_ew = set_value[1];
EW_LED_GREEN=1;
EW_LED_YELLOW =0;
sign[1]=1;
sign[0]=0;
}
if(disp_ew == 0 && sign[1] ==1)
{
disp_ew = set_value[2] + set_value[3];
disp_sn = set_value[2];
disp_ew = set_value[0];
P1 = 0xf3;
falsh_sign = 0;
ET1 = 1;
TR1 = 1;
}
}
void scan_key()
{
while(P3^0 && P3^2 && P3^3);
delay_ms(15);
if(!P3^0)
{
set_process();
}
if(!P3^2)
if(set_value[2] < 0)
{
set_value[2] = set_value[2] + 100;
}
disp_sn = set_value[2];
}
if(key == 4)
{
set_value[3]--;
if(set_value[3] < 0)
{
set_value[3] = 9;
}
disp_sn = set_value[3];
}
delay_ms(1200);
}
}ቤተ መጻሕፍቲ ባይዱ
else
{
while(!P3^3);
if(key == 1)

51单片机用C语言实现交通灯_红绿灯_源程序

51单片机用C语言实现交通灯_红绿灯_源程序

c o d eu n s i g n e dc h a rt a b [ ] = { 0 x 3 f , 0 x 0 6 , 0 x 5 b , 0 x 4 f , 0 x 6 6 , 0 x 6 d , 0 x 7 d , 0 x 0 7 , 0 x 7 f , 0 x 6 f } ; / / 共阴数码管 0 9 u n s i g n e dc h a rD i s _ S h i w e i ; / / 定义十位 u n s i g n e dc h a rD i s _ G e w e i ;/ / 定 g n e di n tc n t ) { w h i l e ( c n t ) ; } m a i n ( ) { T M O D| = 0 x 0 1 ; / / 定时器设置 1 0 m si n1 2 Mc r y s t a l 定时器 0 ,工作方式 1 ,1 6 位定时器 T H 0 = 0 x d 8 ; / / 6 5 5 3 5 1 0 0 0 0 = 5 5 5 3 5 = D 8 F 0 (十六进制) T L 0 = 0 x f 0 ; I E =0 x 8 2 ;/ / 中断控制,E A = 1 开总中断,E T 0 = 1 :定时器 0 中断允许 T R 0 = 1 ; / / 开定时器 0 中断 P 1 = 0 x f c ; / / 红灯亮,根据红黄绿接灯的顺序。 r e d= 1 ; w h i l e ( 1 ) { P 0 = D i s _ S h i w e i ; / / 显示十位,这里实现用 8 位数码管,即左 1 位 P 2 = 0 ; d e l a y ( 3 0 0 ) ; / / 短暂延时 P 0 = D i s _ G e w e i ;/ / 显示个位,左数,2 位 P 2 = 1 ;

单片机控制交通灯

单片机控制交通灯

单片机控制交通灯要实现单片机控制交通灯,首先需要了解交通灯的工作原理和控制方式。

一般的交通灯控制有三种状态:红灯、黄灯和绿灯。

红灯表示停车,黄灯表示准备停车或准备起步,绿灯表示行驶。

下面是一个基本的单片机控制交通灯的程序示例:```c#include<reg52.h>//定义LED端口sbit redLight = P1^0;sbit yellowLight = P1^1; sbit greenLight = P1^2;//定义延时时间#define delayTime 1000void delay(unsigned int ms){ unsigned int i,j;for(i=ms;i>0;i--)for(j=110;j>0;j--);}void init(){//清零redLight = 0;yellowLight = 0;greenLight = 0;}void mn(){init(); //初始化while(1){//红灯redLight = 1;yellowLight = 0; greenLight = 0; delay(delayTime);//黄灯redLight = 0;yellowLight = 1; greenLight = 0; delay(delayTime);//绿灯redLight = 0;yellowLight = 0; greenLight = 1; delay(delayTime); }}```上面的代码使用了8051单片机的开发环境,通过定义三个LED端口,分别控制红、黄、绿三种交通灯的状态。

通过设置不同的IO口状态来控制交通灯的亮灭。

在`init()`函数中,先将所有LED端口设置为低电平,即熄灭状态。

在`mn()`函数中,使用循环控制交通灯额亮灭状态。

先点亮红灯,延时一段时间后熄灭。

然后点亮黄灯,延时一段时间后熄灭。

最后点亮绿灯,延时一段时间后停止。

单片机控制的交通灯C语言编程

单片机控制的交通灯C语言编程

单片机控制的交通灯红灯停,绿灯行,黄灯闪烁提示行人红绿灯即将切换。

四个方向各有一个红、黄、绿显示和两个数码管。

东西道为人行道(20秒),南北道为车行道(60秒),黄灯延时最后三秒时,闪烁并切换。

三、硬件电路设计此电中路设计采用AT89C51单片机,74LS47(数码管驱动)74LS373(数码管驱动输出锁存),8个数码管显示其延时值,四个红、黄、绿指示灯。

硬件设计关键在于,延时显示时,要考虑到当个位数字显示时,要确保十位数字显示输出的不变。

因此,可加输出锁存器。

在延时最后三秒时,要让黄灯进行闪烁,并同时显示数字(这一步在软件设计上很关键)。

四、软件程序(C语言)以下是整个设计的软件程序,直接可以编译成*。

Hex代码。

通过以上电路,下载到单片机,可直接运行。

//*****************************////程序名:十字路口交通灯控制//编写人:黄庭剑//初写时间:2009年1月2日//程序功能:南北为车行道,延时60秒;东西方向为人行道,延时20秒,且在最后3秒黄灯显示2秒钟再实现切换.//CPU说明:AT89C51型单片机; 24MHZ晶体振荡器//完成时间:2009年1月6日//*****************************//#include<stdio.h>#include<reg51.h>#include<intrins.h>sfr p0 = 0x80;sfr p1 = 0x90;sfr p2 = 0xA0;sfr p3 = 0xb0; //这部分内容其实在“#include<reg51.h>”里已经有,但里面定义的必须区分大小写,在这里,因为我程序采用的是小写,reg51.h里对各个端口与寄存器的定义都是大写,所以在编译连接时,会报错,所以,在本设计程序里,我只用到了端口,在这里也就只定义了四个,而没有去改reg51.h里面的内容。

智能交通灯c源代码

智能交通灯c源代码

智能交通灯源代码tl.h //头文件#include<graphics.h>#include<stdlib.h>#include<conio.h>#include<dos.h>#include<time.h>#include<string.h>#define LEN sizeof(Car)#define NULL 0#define wr 22#define wg 8#define creat_time 150#define change_time 300#define max_car 4typedef struct node{int x,y;int flag,flag1;struct node *next;}Car;void initial(void);void welcome(void);void core(void);void goodbye(void);void end(void);void drawcar(int x, int y,int flag);void graphstore (void *buffer[]);void shift (int flag,int *car1,int *car2,int *car3,int *car4,int *car5,int x,int y); void triangle(int a,int b,int c,int d,int e,int f,int g,int h);void arrowx(int x,int y,int color);void arrowz(int x,int y,int color);void arrows(int x,int y,int color);void arrowy(int x,int y,int color);void draw_tree (int x,int y);void draw_house(int x,int y);void drawline(void);void road(void);void light(void);void tree(void);void house(void);void clear(void);void initial(void) //初始化图形{int gdriver=DETECT,gmode;initgraph(&gdriver,&gmode,"");cleardevice();}void welcome(void) //欢迎界面的函数{int i;int Button=0,x=-20,y=-10,x1=-20,y1=-10;setbkcolor(BLUE);setfillstyle(1, DARKGRAY);bar(getmaxx()/2-200,300,getmaxx()/2+200,450);setcolor(GREEN);setfillstyle(1,GREEN);pieslice(getmaxx()/2-100,375,0,360,60);setcolor(RED);settextstyle(1,0,4);outtextxy(getmaxx()/2-140,355,"Enter");setfillstyle(1,RED);pieslice(getmaxx()/2+100,375,0,360,60);setcolor(GREEN);settextstyle(1,0,4);outtextxy(getmaxx()/2+70,355,"Exit");for(i=1;i<=8;i++){setwritemode(XOR_PUT);setcolor(MAGENTA);settextstyle(1,0,i);outtextxy(50+23*i,10+4*i,"Welcome");delay(300);settextstyle(1,0,i);outtextxy(50+23*i,10+4*i,"Welcome");}setwritemode(COPY_PUT);setcolor(YELLOW);settextstyle(1,0,9);outtextxy(130,40,"Welcome");for(i=1;i<=5;i++){setwritemode(XOR_PUT);setcolor(MAGENTA);settextstyle(1,0,i);outtextxy(190-19*i,275-7*i,"to our intellgent traffic");delay(300);settextstyle(1,0,i);outtextxy(190-19*i,275-7*i,"to our intellgent traffic");}setwritemode(COPY_PUT);setcolor(YELLOW);settextstyle(1,0,6);outtextxy(10,200,"to our intellgent traffic");while(1){mouse(3,&Button,&x,&y);draw_mouse(&x,&y,&x1,&y1);if(Button==1&&x>=180&&x<=260&&y>=355&&y<=395){cleardevice();break;}else if(Button==1&&x>=390&&x<=450&&y>=355&&y<395){closegraph();}}}void core(void) //核心函数,执行画界面,运行小车{void * buf[5];graphstore (buf);road( );light( );house();tree();signal();runcar(buf);}void goodbye(void) //再见界面函数{int i;int Button=0,x=-20,y=-10,x1=-20,y1=-10; char xie[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0xC0,0x00,0x02,0x00, 0x03,0x80,0x00,0x00,0xC0,0x00,0x07,0x00, 0x03,0xC0,0x00,0x01,0x80,0x00,0x07,0x00, 0x01,0xE0,0x00,0x01,0x80,0x00,0x07,0x00, 0x00,0x70,0x00,0x03,0x00,0x00,0x07,0x00, 0x00,0x38,0x00,0x07,0x00,0x00,0x07,0x00, 0x00,0x1C,0x00,0x06,0x00,0x00,0x07,0x00, 0x00,0x0E,0x00,0xFF,0xFC,0x00,0x07,0x00, 0x00,0x07,0x01,0xFF,0xFE,0x00,0x07,0x00, 0x00,0x03,0x01,0xC0,0x0E,0x1F,0xFF,0xF8, 0x00,0x00,0x03,0x80,0x07,0x1F,0xFF,0xF8, 0x00,0x00,0x03,0x80,0x07,0x00,0x07,0x00, 0x00,0x00,0x03,0x80,0x07,0x00,0x07,0x00, 0x00,0x00,0x03,0x80,0x07,0x00,0x07,0x00, 0x00,0x00,0x03,0x80,0x07,0x00,0x07,0x00, 0x00,0x00,0x03,0x80,0x07,0x00,0x07,0x00, 0x00,0x00,0x03,0xFF,0xFF,0x00,0x07,0x00, 0x00,0x00,0x03,0xFF,0xFF,0x00,0x07,0x00, 0x3F,0xE0,0x03,0x80,0x07,0x00,0x07,0x00,0x00,0x70,0x03,0x80,0x07,0x0C,0x07,0x00, 0x00,0x38,0x03,0x80,0x07,0x0C,0x07,0x00, 0x00,0x38,0x03,0x80,0x07,0x0E,0x07,0x00, 0x00,0x38,0x03,0x80,0x07,0x06,0x07,0x00, 0x00,0x38,0x03,0x80,0x07,0x06,0x07,0x00, 0x00,0x38,0x03,0xFF,0xFF,0x06,0x07,0x00, 0x00,0x38,0x03,0xFF,0xFF,0x07,0x07,0x00, 0x00,0x38,0x03,0x80,0x07,0x03,0x07,0x00, 0x00,0x38,0x03,0x80,0x07,0x03,0x07,0x00, 0x00,0x38,0x03,0x80,0x07,0x03,0x07,0x00, 0x00,0x38,0x03,0x80,0x07,0x03,0x87,0x00, 0x00,0x38,0x03,0x80,0x07,0x03,0x87,0x00, 0x00,0x38,0x03,0x80,0x07,0x01,0x87,0x00, 0x00,0x38,0x03,0x80,0x07,0x01,0x87,0x00, 0x00,0x38,0x03,0x83,0xFF,0x01,0x87,0x00, 0x00,0x38,0x1F,0xFF,0xFF,0x01,0x87,0x00, 0x00,0x38,0x1F,0xF8,0x07,0x01,0xC7,0x00, 0x00,0x38,0x10,0x00,0x0F,0x01,0xC7,0x00, 0x00,0x38,0x00,0x00,0x0F,0x00,0xC7,0x00, 0x00,0x38,0x00,0x00,0x1F,0x00,0xC7,0x00, 0x00,0x38,0x00,0x00,0x37,0x00,0xC7,0x00, 0x00,0x38,0x00,0x00,0x67,0x00,0x07,0x00, 0x00,0x38,0x00,0x00,0xE7,0x00,0x07,0x00, 0x00,0x38,0x18,0x01,0xC7,0x00,0x07,0x00, 0x00,0x38,0x18,0x03,0x87,0x00,0x07,0x00, 0x00,0x38,0x30,0x07,0x07,0x00,0x07,0x00, 0x00,0x38,0x70,0x0E,0x07,0x00,0x07,0x00, 0x00,0x38,0xE0,0x3C,0x07,0x00,0x07,0x00, 0x00,0x38,0xC0,0x70,0x07,0x00,0x07,0x00, 0x00,0x39,0x80,0xE0,0x07,0x00,0x07,0x00, 0x00,0x3F,0x03,0xC0,0x07,0x00,0x07,0x00, 0x00,0x3E,0x07,0x80,0x07,0x00,0x07,0x00, 0x00,0x1C,0x1E,0x00,0x07,0x00,0x07,0x00, 0x00,0x18,0x1C,0x00,0x07,0x00,0x07,0x00, 0x00,0x00,0x00,0x1F,0x0E,0x0E,0x07,0x00, 0x00,0x00,0x00,0x1F,0xFE,0x0F,0xFE,0x00, 0x00,0x00,0x00,0x01,0xF8,0x03,0xFC,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };char zhi[]={0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00, 0x00,0x08,0x01,0x80,0x00,0x0C,0x00,0x80,0x00,0xC7,0x00,0x80,0x00,0xCF,0xA1,0x80, 0x00,0xFE,0x11,0x80,0x00,0xFC,0x19,0x80, 0x00,0x7D,0x89,0x80,0x00,0x0F,0xC9,0x80, 0x00,0x1E,0x09,0x80,0x00,0x78,0x09,0x80, 0x00,0xC3,0x08,0x80,0x03,0x07,0x98,0x80, 0x0E,0x0C,0x88,0x80,0x78,0x9C,0xC0,0x80, 0x30,0xEC,0x80,0x80,0x00,0xCC,0x80,0x80, 0x00,0xCC,0x80,0x80,0x01,0x8D,0x80,0x80, 0x01,0x8D,0x80,0x80,0x00,0x8C,0x00,0x80, 0x00,0x04,0x00,0x80,0x00,0x04,0x07,0xC0, 0x00,0x00,0x03,0xC0,0x00,0x00,0x01,0xC0, 0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };char zuo[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0xC0,0x00,0x00,0x70,0xE3,0x00, 0x00,0x70,0xEF,0x00,0x00,0x79,0xFE,0x00, 0x00,0xF1,0xF8,0x00,0x00,0xE3,0xF0,0x00, 0x01,0xC3,0xB0,0x00,0x01,0xC3,0x30,0x00, 0x03,0x80,0x30,0x00,0x07,0x80,0x3E,0x00, 0x03,0xC0,0x23,0x00,0x00,0x60,0x22,0x00, 0x00,0x60,0x22,0x00,0x00,0x60,0x24,0x00, 0x00,0x60,0x27,0x00,0x00,0xE0,0x27,0x80, 0x00,0xE0,0x27,0x80,0x00,0x60,0x20,0x80, 0x00,0x60,0x20,0x00,0x00,0x00,0x20,0x00, 0x00,0x00,0x30,0x00,0x00,0x00,0x10,0x00, 0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };char ren[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00, 0x00,0x07,0x00,0x00,0x00,0x07,0x00,0x00, 0x00,0x0E,0x00,0x00,0x00,0x1C,0x00,0x00, 0x00,0x38,0x00,0x00,0x00,0x38,0x00,0x00, 0x00,0x7E,0x00,0x00,0x00,0x43,0xC0,0x00,0x01,0x00,0x1E,0x00,0x03,0x00,0x0F,0x80, 0x02,0x00,0x07,0xF0,0x0E,0x00,0x03,0xFC, 0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };char l[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x20,0x00,0x00,0x00,0x60,0x00,0x00, 0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00, 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };char lin[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00, 0x00,0x00,0x04,0x00,0x00,0x08,0x06,0x00, 0x00,0x18,0x06,0x00,0x00,0x18,0x04,0x00, 0x00,0x18,0x05,0xC0,0x00,0x1C,0x07,0xC0, 0x00,0x38,0x07,0x00,0x00,0xF8,0x1C,0x00, 0x01,0xF8,0x7C,0x00,0x0F,0xD9,0xFC,0x00, 0x07,0x1A,0x30,0x00,0x02,0x1C,0x30,0x00, 0x00,0x38,0x3C,0x00,0x00,0x30,0x7F,0x00, 0x00,0x70,0x43,0xC0,0x01,0xF0,0x80,0x70, 0x01,0xF1,0x00,0x38,0x01,0x90,0x00,0x0C, 0x01,0x90,0x00,0x04,0x00,0x30,0x00,0x00, 0x00,0x10,0x3C,0x00,0x00,0x10,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };char chen[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x40,0x00,0x00,0x00,0x60,0x18,0x00, 0x00,0x60,0x18,0x00,0x00,0x60,0x18,0x00, 0x00,0x40,0x18,0x00,0x00,0x40,0x19,0x80, 0x00,0xF0,0x11,0xC0,0x04,0x41,0xE3,0x80, 0x06,0x41,0xE7,0x00,0x0E,0x03,0xEE,0x00, 0x0C,0x07,0x48,0x00,0x1C,0x02,0x5C,0x00, 0x18,0x00,0xC8,0x00,0x08,0x00,0x88,0x00, 0x00,0x01,0x90,0x00,0x00,0x41,0x10,0x00, 0x00,0xC2,0x00,0x08,0x00,0xC0,0x20,0x08, 0x00,0xC0,0x10,0x08,0x00,0xC0,0x0F,0xF8, 0x00,0x40,0x00,0xE0,0x00,0x40,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };char zhao[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x10,0x00,0x00,0x00,0x18,0x00,0x00, 0x00,0x1C,0x00,0x00,0x00,0x1C,0x00,0x00, 0x00,0x1E,0x04,0x00,0x00,0x1E,0x03,0x00, 0x00,0xF8,0x03,0x80,0x01,0xF8,0x03,0x00, 0x00,0x70,0x03,0x00,0x00,0x3F,0xE2,0x00, 0x00,0x3F,0xFE,0x00,0x00,0xFF,0x7E,0x00, 0x01,0x8C,0x1E,0x00,0x06,0x18,0x0F,0x00, 0x1C,0x30,0x0B,0x00,0x78,0xFE,0x11,0x80, 0x31,0xC6,0x70,0x00,0x01,0x8E,0x40,0x00, 0x00,0x18,0x00,0x00,0x00,0x30,0x00,0x00, 0x01,0xF0,0x00,0x00,0x01,0xC0,0x20,0x00, 0x00,0x00,0x07,0xFF,0x00,0x00,0x00,0xE0, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };char ming[]={0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xC0, 0x00,0x00,0x06,0x60,0x00,0x30,0x20,0x60, 0x00,0x7C,0x20,0x60,0x00,0x06,0x20,0x40, 0x02,0x06,0x20,0x40,0x01,0x06,0x3E,0x40, 0x01,0x04,0x26,0xC0,0x01,0xE4,0x26,0xC0, 0x03,0xF4,0x64,0xC0,0x03,0x2C,0x28,0xC0, 0x03,0x2C,0x28,0xC0,0x03,0x0C,0x66,0xC0, 0x03,0x1C,0x66,0xC0,0x01,0x38,0x66,0xC0, 0x01,0xF8,0x4C,0xC0,0x00,0xC8,0x40,0xC0, 0x00,0x00,0xC0,0xC0,0x00,0x00,0x80,0xC0, 0x00,0x00,0x80,0xC0,0x00,0x00,0x00,0xC0, 0x00,0x01,0x02,0xC0,0x00,0x00,0x01,0xC0, 0x00,0x00,0x00,0xC0,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };char feng[]={0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00, 0x00,0x00,0x60,0x00,0x00,0x40,0x78,0x00, 0x00,0x41,0xC8,0x00,0x00,0x61,0x08,0x00, 0x00,0x40,0x10,0x00,0x00,0x44,0x30,0x80, 0x08,0x47,0xF0,0x00,0x08,0x4F,0xF0,0x00, 0x08,0xD8,0xBE,0x00,0x08,0x78,0x3E,0x00, 0x08,0xC1,0x3C,0x00,0x1F,0x83,0xF8,0x00, 0x0E,0x04,0x38,0x00,0x04,0x00,0x3E,0x00, 0x00,0x00,0xFC,0x00,0x00,0x00,0xF8,0x00, 0x00,0x00,0x19,0x00,0x00,0x00,0x38,0x00, 0x00,0x03,0x98,0x00,0x00,0x00,0x10,0x00, 0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };cleardevice();setbkcolor(BLACK);drawmat(xie,64, 200, 100, RED);drawmat(xie,64, 350, 100, RED);drawmat(zhi,32, 150, 250, BLUE); drawmat(zuo,32, 200, 250, BLUE); drawmat(ren,32, 250, 250, BLUE); drawmat(l,32, 300, 250, BLUE);drawmat(lin,32, 350, 250, BLUE);drawmat(chen,32, 400, 250, BLUE);drawmat(zhao,32, 350, 320, BLUE);drawmat(ming,32, 400, 320, BLUE);drawmat(feng,32, 450, 320, BLUE);setcolor(GREEN);setlinestyle(0,0,THICK_WIDTH);rectangle(575,295,635,335);setfillstyle(1, YELLOW);bar(575,295,635,335);setfillstyle(1, DARKGRAY);bar(580,300,630,330);setcolor(RED);settextstyle(1,0,3);outtextxy(580,300,"Exit");while(1){mouse(3,&Button,&x,&y);draw_mouse(&x,&y,&x1,&y1);if(Button==1&&x>=580&&x<=630&&y>=300&&y<330) {break;}}cleardevice();for(i=1;i<=8;i++){setwritemode(XOR_PUT);setcolor(MAGENTA);settextstyle(1,0,i);outtextxy(50+19*i,100+3*i,"byebye");delay(300);settextstyle(1,0,i);outtextxy(50+19*i,100+3*i,"byebye");}setwritemode(COPY_PUT);setcolor(YELLOW);settextstyle(1,0,9);outtextxy(160,130,"ByeBye");getch();}void end(void) //结束关闭图像界面{getch();closegraph();}void drawcar(int x, int y,int flag) //画车的函数{int car1[18],car2[10],car3[10],car4[10],car5[10];car1[0]=x-10; car1[1]=y-3;car1[2]=x-6; car1[3]=y-5;car1[4]=x+6;car1[5]=y-5;car1[6]=x+10;car1[7]=y-3;car1[8]=x+10; car1[9]=y+3;car1[10]=x+6;car1[11]=y+5;car1[12]=x-6;car1[13]=y+5;car1[14]=x-10;car1[15]=y+3;car1[16]=x-10;car1[17]=y-3;car2[0]=x-5; car2[1]=y-4;car2[2]=x+5; car2[3]=y-4;car2[4]=x+3; car2[5]=y-2;car2[6]=x-3; car2[7]=y-2;car2[8]=x-5; car2[9]=y-4;car3[0]=x-5; car3[1]=y+4;car3[2]=x+5; car3[3]=y+4;car3[4]=x+3; car3[5]=y+2;car3[6]=x-3; car3[7]=y+2;car3[8]=x-5; car3[9]=y+4;car4[0]=x-5; car4[1]=y+4;car4[2]=x-5; car4[3]=y-4;car4[4]=x-3; car4[5]=y-2;car4[6]=x-3; car4[7]=y+2;car4[8]=x-5; car4[9]=y+4;car5[0]=x+5; car5[1]=y+4;car5[2]=x+5; car5[3]=y-4;car5[4]=x+3; car5[5]=y-2;car5[6]=x+3; car5[7]=y+2;car5[8]=x+5; car5[9]=y+4;shift(flag,car1,car2,car3,car4,car5,x,y);setlinestyle(0,0,1);setcolor(DARKGRAY);drawpoly(9,car1);setfillstyle(1,BLUE);fillpoly(9,car1);setlinestyle(0,0,1);setcolor(BLUE);drawpoly(5,car2);drawpoly(5,car3);drawpoly(5,car4);drawpoly(5,car5);setfillstyle(1,WHITE);fillpoly(5,car2);fillpoly(5,car3);fillpoly(5,car4);fillpoly(5,car5);}void graphstore (void *buffer[]) //将各个方向的小车画出,保存在buffer指向的区域内的函数{unsigned s[5];int i;cleardevice();setfillstyle(1,DARKGRAY);bar(90,95,110,105);bar(189,190,210,210);bar(290,290,310,310);bar(394,390,406,410);bar(250,250,270,270);for(i=0;i<4;i++){drawcar((i+1)*100,(i+1)*100,i);}s[0]=imagesize(90,95,110,105);s[1]=imagesize(189,190,210,210);s[2]=imagesize(290,290,310,310);s[3]=imagesize(394,390,406,410);s[4]=imagesize(250,250,270,270);for(i=0;i<5;i++){buffer[i]=malloc(s[i]);}getimage(90,95,110,105,buffer[0]);getimage(189,190,210,210,buffer[1]);getimage(290,290,310,310,buffer[2]);getimage(394,390,406,410,buffer[3]);getimage(250,250,270,270,buffer[4]);cleardevice();}void shift (int flag,int *car1,int *car2,int *car3,int *car4,int *car5,int x,int y) //将画出的小车通过旋转画出各个方向的小车{int i,mid;float a,b;switch(flag){case 0 : a=1;b=0;break;case 1 : a=b=0.70710678;break;case 2 : a=0.70710678;b=-a;break;case 3 : a=0;b=1;break;}for(i=0;i<18;i+=2){mid=car1[i];car1[i]=a*(car1[i]-x)-b*(car1[i+1]-y)+x;car1[i+1]=b*(mid-x)+a*(car1[i+1]-y)+y;}for(i=0;i<10;i+=2){mid=car2[i];car2[i]=a*(car2[i]-x)-b*(car2[i+1]-y)+x;car2[i+1]=b*(mid-x)+a*(car2[i+1]-y)+y;}for(i=0;i<10;i+=2){mid=car3[i];car3[i]=a*(car3[i]-x)-b*(car3[i+1]-y)+x;car3[i+1]=b*(mid-x)+a*(car3[i+1]-y)+y;}for(i=0;i<10;i+=2){mid=car4[i];car4[i]=a*(car4[i]-x)-b*(car4[i+1]-y)+x;car4[i+1]=b*(mid-x)+a*(car4[i+1]-y)+y;}for(i=0;i<10;i+=2){mid=car5[i];car5[i]=a*(car5[i]-x)-b*(car5[i+1]-y)+x;car5[i+1]=b*(mid-x)+a*(car5[i+1]-y)+y;}}void triangle(int a,int b,int c,int d,int e,int f,int g,int h) //画三角形,修饰道路{int s[8];s[0]=a;s[1]=b;s[2]=c;s[3]=d;s[4]=e;s[5]=f;s[6]=g;s[7]=h;setlinestyle(0,0,1);setcolor(DARKGRAY);drawpoly(3,s);fillpoly(3,s);}void arrowx(int x,int y,int color) //向下的箭头{int s[16];s[0]=x-3; s[1]=y;s[2]=x-6; s[3]=y;s[4]=x; s[5]=y+6;s[6]=x+6; s[7]=y;s[8]=x+3; s[9]=y;s[10]=x+3;s[11]=y-6;s[12]=x-3;s[13]=y-6;s[14]=x-3; s[15]=y;setcolor(color);setfillstyle(1,color);drawpoly(7,s);fillpoly(7,s);}void arrowz(int x,int y,int color) //向坐的箭头{int s[16];s[0]=x; s[1]=y+3;s[2]=x; s[3]=y+6;s[4]=x-6; s[5]=y;s[6]=x; s[7]=y-6;s[8]=x; s[9]=y-3;s[10]=x+6;s[11]=y-3;s[12]=x+6;s[13]=y+3;s[0]=x; s[1]=y+3;setcolor(color);setfillstyle(1,color);drawpoly(7,s);fillpoly(7,s);}void arrows(int x,int y,int color) //向上的箭头{int s[16];s[0]=x-3; s[1]=y;s[2]=x-6; s[3]=y;s[4]=x; s[5]=y-6;s[6]=x+6; s[7]=y;s[8]=x+3; s[9]=y;s[10]=x+3;s[11]=y+6;s[12]=x-3;s[13]=y+6;s[14]=x-3; s[15]=y;setcolor(color);setfillstyle(1,color);drawpoly(7,s);fillpoly(7,s);}void arrowy(int x,int y,int color) //向右的箭头{int s[16];s[0]=x; s[1]=y+3;s[2]=x; s[3]=y+6;s[4]=x+6; s[5]=y;s[6]=x; s[7]=y-6;s[8]=x; s[9]=y-3;s[10]=x-6;s[11]=y-3;s[12]=x-6;s[13]=y+3;s[0]=x; s[1]=y+3;setcolor(color);setfillstyle(1,color);drawpoly(7,s);fillpoly(7,s);}void draw_tree (int x,int y) //画树函数{int tree[24];tree[0]=x;tree[1]=y;tree[2]=x-8;tree[3]=y+6;tree[4]=x-2;tree[5]=y+6;tree[6]=x-10;tree[7]=y+12;tree[8]=x-2;tree[9]=y+12;tree[10]=x-14;tree[11]=y+20;tree[12]=x+14;tree[13]=y+20;tree[14]=x+2;tree[15]=y+12;tree[16]=x+10;tree[17]=y+12;tree[18]=x+2;tree[19]=y+6;tree[20]=x+8;tree[21]=y+6;tree[22]=x;tree[23]=y;setcolor(GREEN);setlinestyle(0,0,1);drawpoly(12,tree);setfillstyle(1,GREEN);fillpoly(12,tree);setcolor(BROWN);setfillstyle(1,BROWN);bar(x-2,y+20,x+2,y+30);rectangle(x-2,y+20,x+2,y+30);}void draw_house(int x,int y) //画房子函数{int house1[10];int house2[10];int house3[14];house1[0]=x;house1[1]=y-1;house1[2]=x-16;house1[3]=y-8;house1[4]=x-16;house1[5]=y-30;house1[6]=x;house1[7]=y-30;house1[8]=x;house1[9]=y-1;house2[0]=x;house2[1]=y-1;house2[2]=x+16;house2[3]=y-8;house2[4]=x+16;house2[5]=y-30;house2[6]=x;house2[7]=y-30;house2[8]=x;house2[9]=y-1;house3[0]=x+5;house3[1]=y-30;house3[2]=x-15;house3[3]=y-15;house3[4]=x-25;house3[5]=y-25;house3[6]=x-5;house3[7]=y-40;house3[8]=x+14;house3[9]=y-34;house3[10]=x+24;house3[11]=y-24;house3[12]=x+5;house3[13]=y-30;setcolor(WHITE);setlinestyle(0,0,1);drawpoly(5,house1);setfillstyle(1,LIGHTGRAY);fillpoly(5,house1);setcolor(WHITE);setlinestyle(0,0,1);drawpoly(5,house2);setfillstyle(1,LIGHTGRAY);fillpoly(5,house2);setcolor(WHITE);setlinestyle(0,0,1);drawpoly(7,house3);setfillstyle(1,RED);fillpoly(7,house3);setcolor(WHITE);line(x+5,y-28,x-5,y-38);}void drawline(void) //画车道线{int i,j,x,y;x=getmaxx();y=getmaxy();setlinestyle(3,0,1);setcolor(12);for(i=0;i<(y/2-3*wr-wg);i+=20){line(x/2-2*wr-wg,i,x/2-2*wr-wg,i+14);line(x/2-2*wr-wg,i+y/2+3*wr+wg,x/2-2*wr-wg,i+14+y/2+3*wr+wg);line(x/2-wr-wg,i,x/2-wr-wg,i+14);line(x/2-wr-wg,i+y/2+3*wr+wg,x/2-wr-wg,i+14+y/2+3*wr+wg);line(x/2+wr+wg,i,x/2+wr+wg,i+14);line(x/2+wr+wg,i+y/2+3*wr+wg,x/2+wr+wg,i+14+y/2+3*wr+wg);line(x/2+2*wr+wg,i,x/2+2*wr+wg,i+14);line(x/2+2*wr+wg,i+y/2+3*wr+wg,x/2+2*wr+wg,i+14+y/2+3*wr+wg);}for(i=0;i<(x/2-3*wr-wg);i+=20){line(i,y/2-2*wr-wg,i+14,y/2-2*wr-wg);line(i+x/2+3*wr+wg,y/2-2*wr-wg,i+x/2+3*wr+wg+14,y/2-2*wr-wg);line(i,y/2-wr-wg,i+14,y/2-wr-wg);line(i+x/2+3*wr+wg,y/2-wr-wg,i+x/2+3*wr+wg+14,y/2-wr-wg);line(i,y/2+wr+wg,i+14,y/2+wr+wg);line(i+x/2+3*wr+wg,y/2+wr+wg,i+x/2+3*wr+wg+14,y/2+wr+wg);line(i,y/2+2*wr+wg,i+14,y/2+2*wr+wg);line(i+x/2+3*wr+wg,y/2+2*wr+wg,i+x/2+3*wr+wg+14,y/2+2*wr+wg);}}void road( void) //画道路{int x,y;x=getmaxx();y=getmaxy();setbkcolor(WHITE);setfillstyle(1,DARKGRAY);bar(x/2-3*wr-wg,0,x/2+3*wr+wg,y);bar(0,y/2-3*wr-wg,x,y/2+3*wr+wg);drawline();bar(x/2-3*wr-wg-36,y/2-4*wr-wg-16,x/2-3*wr-wg-4,y/2-4*wr-wg);bar(x/2+3*wr+wg+36,y/2+4*wr+wg+16,x/2+3*wr+wg+4,y/2+4*wr+wg); bar(x/2+4*wr+wg,y/2-3*wr-wg-36,x/2+4*wr+wg+16,y/2-3*wr-wg-4);bar(x/2-4*wr-wg-16,y/2+3*wr+wg+4,x/2-4*wr-wg,y/2+3*wr+wg+36);setfillstyle(1,GREEN);bar(0,y/2-wg,x/2-3*wr-wg,y/2+wg);bar(x/2+3*wr+wg,y/2-wg,x,y/2+wg);bar(x/2-wg,0,x/2+wg,y/2-3*wr-wg);bar(x/2-wg,y/2+3*wr+wg,x/2+wg,y);setfillstyle(1,YELLOW);triangle(x/2+2*wr+wg,y/2+2*wr+wg,x/2+3*wr+wg,y/2+2*wr+wg,x/2+2*wr+wg,y/2+3*wr+wg,x /2+2*wr+wg,y/2+2*wr+wg);triangle(x/2+2*wr+wg,y/2-2*wr-wg,x/2+3*wr+wg,y/2-2*wr-wg,x/2+2*wr+wg,y/2-3*wr-wg,x/2+ 2*wr+wg,y/2-2*wr-wg);triangle(x/2-2*wr-wg,y/2-2*wr-wg,x/2-3*wr-wg,y/2-2*wr-wg,x/2-2*wr-wg,y/2-3*wr-wg,x/2-2*w r-wg,y/2-2*wr-wg);triangle(x/2-2*wr-wg,y/2+2*wr+wg,x/2-3*wr-wg,y/2+2*wr+wg,x/2-2*wr-wg,y/2+3*wr+wg,x/2-2*wr-wg,y/2+2*wr+wg);setfillstyle(1,DARKGRAY);triangle(x/2-3*wr-wg,y/2-3*wr-wg,x/2-4*wr-wg,y/2-3*wr-wg,x/2-3*wr-wg,y/2-4*wr-wg,x/2-3*w r-wg,y/2-3*wr-wg);triangle(x/2-3*wr-wg,y/2+3*wr+wg,x/2-4*wr-wg,y/2+3*wr+wg,x/2-3*wr-wg,y/2+4*wr+wg,x/2-3*wr-wg,y/2+3*wr+wg);triangle(x/2+3*wr+wg,y/2+3*wr+wg,x/2+4*wr+wg,y/2+3*wr+wg,x/2+3*wr+wg,y/2+4*wr+wg,x /2+3*wr+wg,y/2+3*wr+wg);triangle(x/2+3*wr+wg,y/2-3*wr-wg,x/2+4*wr+wg,y/2-3*wr-wg,x/2+3*wr+wg,y/2-4*wr-wg,x/2+ 3*wr+wg,y/2-3*wr-wg);}void light(void) //摆放红绿灯{int x,y;x=getmaxx();y=getmaxy();arrowy(x/2-3*wr-wg-12,y/2-4*wr-wg-8,4);arrowx(x/2-3*wr-wg-28,y/2-4*wr-wg-8,4);arrows(x/2-4*wr-wg-8,y/2+3*wr+wg+12,4); arrowy(x/2-4*wr-wg-8,y/2+3*wr+wg+28,4);arrowz(x/2+3*wr+wg+12,y/2+4*wr+wg+8,4); arrows(x/2+3*wr+wg+28,y/2+4*wr+wg+8,4);arrowx(x/2+4*wr+wg+8,y/2-3*wr-wg-12,4); arrowz(x/2+4*wr+wg+8,y/2-3*wr-wg-28,4); }void tree(void) //摆放树的函数{draw_tree(20,getmaxy()/2-(3*wr+wg)-35); draw_tree(60,getmaxy()/2-(3*wr+wg)-35); draw_tree(100,getmaxy()/2-(3*wr+wg)-35); draw_tree(140,getmaxy()/2-(3*wr+wg)-35); draw_tree(180,getmaxy()/2-(3*wr+wg)-35); draw_tree(620,getmaxy()/2-(3*wr+wg)-35); draw_tree(580,getmaxy()/2-(3*wr+wg)-35); draw_tree(540,getmaxy()/2-(3*wr+wg)-35); draw_tree(500,getmaxy()/2-(3*wr+wg)-35); draw_tree(460,getmaxy()/2-(3*wr+wg)-35);draw_tree(20,getmaxy()/2+(3*wr+wg)+5); draw_tree(60,getmaxy()/2+(3*wr+wg)+5); draw_tree(100,getmaxy()/2+(3*wr+wg)+5); draw_tree(140,getmaxy()/2+(3*wr+wg)+5); draw_tree(180,getmaxy()/2+(3*wr+wg)+5); draw_tree(620,getmaxy()/2+(3*wr+wg)+5); draw_tree(580,getmaxy()/2+(3*wr+wg)+5); draw_tree(540,getmaxy()/2+(3*wr+wg)+5); draw_tree(500,getmaxy()/2+(3*wr+wg)+5); draw_tree(460,getmaxy()/2+(3*wr+wg)+5);draw_tree(410,getmaxy()/2+(3*wr+wg)+45); draw_tree(410,getmaxy()/2+(3*wr+wg)+85);draw_tree(410,getmaxy()/2-(3*wr+wg)-75); draw_tree(410,getmaxy()/2-(3*wr+wg)-115); draw_tree(410,getmaxy()/2-(3*wr+wg)-155);draw_tree(230,getmaxy()/2-(3*wr+wg)-75);draw_tree(230,getmaxy()/2-(3*wr+wg)-115);draw_tree(230,getmaxy()/2-(3*wr+wg)-155);draw_tree(230,getmaxy()/2+(3*wr+wg)+45);draw_tree(230,getmaxy()/2+(3*wr+wg)+85);draw_tree(230,getmaxy()/2+(3*wr+wg)+125);}void house(void) //摆放房子{draw_house(30,120);draw_house(90,100);draw_house(110,50);draw_house(160,130);draw_house(60,410);draw_house(120,450);draw_house(180,390);draw_house(530,130);draw_house(480,80);draw_house(550,60);draw_house(540,450);draw_house(470,400);}void clear(void) //清除小车{setfillstyle(1,DARKGRAY);bar(0,getmaxy()/2-3*wr-wg,21,getmaxy()/2-wg-1);bar(getmaxx()/2-3*wr-wg,getmaxy()-21,getmaxx()/2-wg-1,getmaxy()); bar(getmaxx()-21,getmaxy()/2+wg+1,getmaxx(),getmaxy()/2+3*wr+wg); bar(getmaxx()/2+wg+1,0,getmaxx()/2+3*wr+wg,21);drawline();}void runcar(void * buf[]) //控制车辆的产生与运行{Car *head[4],*p[4],*q[4];int count=0;int i,j,data[4][2];int size;j=0;do{if(count==0||count%creat_time==0){for(i=0;i<4;i++){head[i]=Creat_Car(head[i],i);}}for(i=0;i<4;i++){p[i]=head[i];}for(i=0;i<4;i++){while(p[i]!=NULL){if(halt(p[i])){if(stop(p[i])){switch(i){case 0:movingcar1(&(p[i]->x),&(p[i]->y),p[i]->flag,p[i]->flag1,buf[0]);movingcar2(&(p[i]->x),&(p[i]->y),p[i]->flag,p[i]->flag1,buf[1],buf[4]);movingcar3(&(p[i]->x),&(p[i]->y),p[i]->flag,p[i]->flag1,buf[2],buf[4]);movingcar4(&(p[i]->x),&(p[i]->y),p[i]->flag,p[i]->flag1,buf[3]);break;case 2:movingcar1(&(p[i]->x),&(p[i]->y),p[i]->flag,p[i]->flag1,buf[0]);movingcar2(&(p[i]->x),&(p[i]->y),p[i]->flag,p[i]->flag1,buf[1],buf[4]);movingcar3(&(p[i]->x),&(p[i]->y),p[i]->flag,p[i]->flag1,buf[2],buf[4]);movingcar4(&(p[i]->x),&(p[i]->y),p[i]->flag,p[i]->flag1,buf[3]);break;case 1:movingcar1(&(p[i]->x),&(p[i]->y),p[i]->flag,p[i]->flag1,buf[3]);movingcar2(&(p[i]->x),&(p[i]->y),p[i]->flag,p[i]->flag1,buf[2],buf[4]);movingcar3(&(p[i]->x),&(p[i]->y),p[i]->flag,p[i]->flag1,buf[1],buf[4]);movingcar4(&(p[i]->x),&(p[i]->y),p[i]->flag,p[i]->flag1,buf[0]);break;case 3:movingcar1(&(p[i]->x),&(p[i]->y),p[i]->flag,p[i]->flag1,buf[3]);movingcar2(&(p[i]->x),&(p[i]->y),p[i]->flag,p[i]->flag1,buf[2],buf[4]);movingcar3(&(p[i]->x),&(p[i]->y),p[i]->flag,p[i]->flag1,buf[1],buf[4]);movingcar4(&(p[i]->x),&(p[i]->y),p[i]->flag,p[i]->flag1,buf[0]);break;}}}if(out(p[i],i)){if(p[i]==head[i]){q[i]=head[i];head[i]=head[i]->next;free(q[i]);}else{q[i]->next=p[i]->next;free(p[i]);}}q[i]=p[i];p[i]=p[i]->next;}}count_car(head,data);intelligent_light(data,&j,&count);clear( );delay(20);count++;}while(!kbhit());}void movingcar1(int *x,int *y,int flag,int flag1,void *buf) //所有小车出车后直行的函数{if(flag==0&&flag1==0&&*x<=getmaxx()/2-(3*wr+wg+10)){putimage(*x-10,*y-5,buf,0);*x+=1;}else if(flag==0&&flag1==1){putimage(*x-10,*y-5,buf,0);*x+=1;}else if(flag==0&&flag1==2&&*x<=getmaxx()/2-(3*wr/2+wg)){putimage(*x-10,*y-5,buf,0);*x+=1;}if(flag==2&&flag1==0&&*x>=getmaxx()/2+(3*wr+wg+10)){putimage(*x-10,*y-5,buf,0);*x-=1;}else if(flag==2&&flag1==1){putimage(*x-10,*y-5,buf,0);*x-=1;}else if(flag==2&&flag1==2&&*x>=getmaxx()/2+(3*wr/2+wg)){putimage(*x-10,*y-5,buf,0);*x-=1;}if(flag==1&&flag1==0&&*y>=getmaxy()/2+(3*wr+wg+10)){putimage(*x-5,*y-10,buf,0);*y-=1;}else if (flag==1&&flag1==1){putimage(*x-5,*y-10,buf,0);*y-=1;}else if(flag==1&&flag1==2&&*y>=getmaxy()/2+(3*wr/2+wg)){putimage(*x-5,*y-10,buf,0);*y-=1;}if(flag==3&&flag1==0&&*y<=getmaxy()/2-(3*wr+wg+10)){putimage(*x-5,*y-10,buf,0);*y+=1;}else if(flag==3&&flag1==1){putimage(*x-5,*y-10,buf,0);*y+=1;}else if(flag==3&&flag1==2&&*y<=getmaxy()/2-(3*wr/2+wg)){putimage(*x-5,*y-10,buf,0);*y+=1;}}void movingcar2(int *x,int *y,int flag,int flag1,void *buf,void *buf1) //所有右转小车右转的函数{if(flag==0&&flag1==0&&*x>=getmaxx()/2-(3*wr+wg+10)&&*x<getmaxx()/2-(5*wr/2+wg)) {putimage(*x-10,*y-10,buf,0);*x+=1;*y+=1;if(*x==getmaxx()/2-(5*wr/2+wg)){putimage(*x-10,*y-10,buf1,0);}}。

51单片机用C语言实现交通灯

51单片机用C语言实现交通灯

51单片机用C语言实现交通灯51 单片机用C 语言实现交通灯(红绿灯)源程序交通灯,红黄绿灯交替亮,怎样实现呢?其实就是根据单片机定时器及倒计时的程序修改。

源程序如下:#include bit red,green,yellow,turnred; //定义红、黄、绿及转红标志code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共阴数码管0- 9unsigned char Dis_Shiwei;//定义十位unsigned char Dis_Gewei; //定义个位void delay(unsigned int cnt) //用于动态扫描数码管的延时程序{while(--cnt);}main() {TMOD |=0x01;//定时器设置10ms in 12M crystal 定时器0,工作方式1,16 位定时器TH0=0xd8; //65535-10000=55535=D8F0(十六进制)TL0=0xf0;IE= 0x82; //中断控制,EA=1 开总中断,ET0=1:定时器0 中断允许TR0=1; //开定时器0中断P1=0xfc;//红灯亮,根据红黄绿接灯的顺序。

red =1;while(1) {P0=Dis_Shiwei;//显示十位,这里实现用8 位数码管,即左1 位P2=0;delay(300); //短暂延时P0=Dis_Gewei; //显示个位,左数,2 位P2=1;delay(300);}}void tim(void) interrupt 1 using 1{static unsigned char second=60,count; //初值60TH0=0xd8;//重新赋值,10 毫秒定时TL0=0xf0;count++;if (count==100) {count=0;second--;//秒减1if(second==0){ //这里添加定时到0 的代码,可以是灯电路,继电器吸合等,或者执行一个程序if(red) //红灭,先转黄{red=0;yellow=1;second=4;P1=0xF3;//黄灯亮4 秒,黄灯为过渡灯,再根据情况转绿或转红}else if(yellow && !turnred){yellow=0;green=1;second=25;P1=0xCF;// 绿灯亮25 秒,}else if(green){yellow=1;green=0;second=4;P1=0xF3;//黄灯亮4 秒turnred=1;}else if(yellow && turnred) //绿灯灭,转黄灯,后红灯,turnred=1 时{red=1;yellow=0;P1=0xFC;//红灯亮60 秒second=60;turnred=0; //接下来是转黄,绿。

简易交通灯c语言程序

简易交通灯c语言程序

uchar duanma[10]={0xc0,0xf9,0xa4, 0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //段码unchar weima [4]={0x01,0x02,0x04,0x08}; //位码unchar nanbei [3]={40,35,4}; //南北方向三组倒记时对应红,绿,黄灯unchar dongxi [3]={35,4,40}; //东西方向三组倒计时对应绿,黄,红灯unchar nbdeng [3]={0x01,0x04,0x02}; //南北方向三组灯的状态红绿黄unchar dxdeng [3]={0x8x,0x40,0x20} //东西方向三组灯的状态绿黄红unchar jishu1,jishu2,dxjishu,nbjishu,dxmiao,nbmiao,nbmiao; //定义变量unchar xianshi [4]={0,0,0,0}; //显示缓存器void yanshi10ms (void) //延时10ms函数{Unchar i,j;for (i=20;i>0;i--)for (j=250;j.0;j--);}Void chushihua (void) //初始化函数{TMOD=0x01; //定时器T0方式1THO=(65536-10000)/256 //10ms 定时高八位初值TLO=(65536-10000)%256; //10ms 定时低八位初值EA=1;ETO=1;EXO=1;ITO=1TRO=1;//CPU,T0,INTO允许中断,外部中断下降沿触发,启动定时器}void main(void) //主函数{dxmiao=dongxi [0] //东西倒计时赋初值nbmiao=nanbei [0] //南北倒计时赋初值chushihua (); //调试初始化函数while (1); //无限循环,踏步}Void anjian (void) interrupt 0 //外部中断 INTO 中断服务函数{yanshi10ms (); //延时去抖动if (INTO==0); //二次判断{If ((dxmiao<=50) && (nbmiao<=50)); //东西,南北当前显示值不大于50 {dxmiao+=10; //东西显示值+10nbmiao+=10 //南北显示着+10}//******************************************************************* }}void daojishi (void) interrupt 1 //定时器T0中断服务函数{THO=(65536-10000)/256; //高八位初值重装TLO=(65536-10000)/256; //低八位初值重装xiaoshi[0]=duanma [nbmiao/10]; //分离南北方向秒的十位xiaoshi[1]=duanma [nbmiao%10]; //分离南北方向秒的个位xiaoshi[2]=duanma [nbmiao/10]; //分离东西方向秒的十位xiaoshi[3]=duanma [nbmiao/10]; //分离东西方向秒的个位//**********************动态扫描*********************************** jishu1 ++; //指向扫描的下一位if (jishu1==4) jishu1=0; //完成四位扫描后计数归0P0=weima [jishu1]; //送位码P1=xiaoshi [jishu1]; //送段码//********************控制灯的状态********************************* P2=dxdeng [dxjishu] | nbdeng [nbjishu]; //送灯的状态码//东西方向状态和南北方向状态组合输出//********************产生1秒记时及处理程序*********************** jishu2++; //10ms 计数if (jishu2==100) //计够100次为1秒{jishu2=0; //计够1秒后,计数归0dxmiao --; nbmiao --; //东西,南北秒各减1,实现倒计时if (dxmiao==0xff) //东西方向是否减到0{dxjishu ++ ; //是,指东西方向交通灯的下一个状态if (dxjishu==3) dxjishu==0; //完成三个状态后再重新开始dxmiao=dongxi [dxjishu]; //送东西方向数码显示的初值}if (nanbei==0xff) //南北方向是否减到0}nbjishu++; //是,指向南北方向交通灯的下一个状态if (nbjishu==3) nbjishu=0; //完成三个状态后再重新开始nbmiao==nanbei[nbjishu]; //送南北方向数码显示的初值 }}}。

51单片机控制交通灯原理图及C语言程序

51单片机控制交通灯原理图及C语言程序

Proteus仿真原理图:Keil C源程序:#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit RED_DONGXI = P1^0;//南北方向红灯亮sbit YELLOW_DONGXI = P1^1;//南北方向黄灯亮sbit RED_NANBEI = P1^3;//东西方向红灯亮sbit GREEN_DONGXI = P1^2;//南北方向绿灯亮sbit YELLOW_NANBEI = P1^4;//东西方向黄灯亮sbit GREEN_NANBEI = P1^5;//东西方向绿灯亮sbit DXweixuan1 = P1^6;//南北方向数码管位选1sbit DXweixuan2 = P1^7;//南北方向数码管位选2sbit NBweixuan1 = P3^0;//东西方向数码管位选1sbit NBweixuan2 = P3^1;//东西方向数码管位选2sbit L1=P3^5;sbit L2=P3^6;sbit L3=P3^7;uint aa, bai,shi,ge,bb;uint shi1,ge1,shi2,ge2;uint code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; uint code table1[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};void delay(uint z);void init(uint a);void display(uint shi1,uint ge1,uint shi2,uint ge2);void xtimer0();void init1();void init2();void init3();void init4();void init5();void xint1();void xint0();void LED_ON();void LED_OFF();void main(){P0=0xFF;P1=0xFF;P2=0x00;P3=0xFF;EA=1;EX0=1;IT0=0;init1();while(1){init2();//第2个状态init3(); //第3个状态init4(); //第4个状态init5();//第5个状态}}void init1()//第一个状态:东西、南北方向均亮红灯5S {uint temp;temp=5;TMOD=0x01;TH0=(65535-50000)/256;TL0=(65535-50000)%256;EA=1;ET0=1;TR0=1;while(1){RED_DONGXI=0; //第一个状态东西、南北均亮红灯5SRED_NANBEI=0;GREEN_DONGXI=1;GREEN_NANBEI=1;YELLOW_DONGXI=1;YELLOW_NANBEI=1;if(aa==20)//定时20*50MS=1S{aa=0;temp--;}shi1=shi2=temp/10;ge1=ge2=temp%10;if(temp==0){temp=5;break;}display(ge1,shi1,ge2,shi2);}}void init2()//第二个状态:东西亮红灯30S~5S、南北亮绿灯25~0S;{uint temp;temp=26;TMOD=0x01;TH0=(65535-50000)/256;TL0=(65535-50000)%256;EA=1;ET0=1;TR0=1;while(1){RED_DONGXI=1;RED_NANBEI=0;GREEN_DONGXI=0;GREEN_NANBEI=1;YELLOW_DONGXI=1;//第二个状态:东西亮绿灯25S、南北亮红灯YELLOW_NANBEI=1;if(aa==20)//定时20*50MS=1S{aa=0;temp--;shi1=(temp+5)/10;ge1=(temp+5)%10;shi2=temp/10;ge2=temp%10;if(temp==0){temp=26;break;}}display(ge1,shi1,ge2,shi2);}}void init3() //第三个状态:东西绿灯变为黄灯闪5次、南北亮红灯5S {uint temp;temp=6;TMOD=0x01;TH0=(65535-50000)/256;TL0=(65535-50000)%256;EA=1;ET0=1;TR0=1;while(1){RED_NANBEI=0;GREEN_DONGXI=1;if(aa==20)//定时20*50MS=1S{aa=0;temp--;YELLOW_DONGXI=~YELLOW_DONGXI;shi1=temp/10;shi2=shi1;ge1=temp%10;ge2=ge1;}if(temp==0){temp=6;break;}display(ge1,shi1,ge2,shi2);}}void init4()//第四个状态:东西亮绿灯25~0S,南北方向亮红灯30~5S;{uint temp;temp=26;TMOD=0x01;TH0=(65535-50000)/256;TL0=(65535-50000)%256;EA=1;ET0=1;TR0=1;while(1){RED_DONGXI=0;RED_NANBEI=1;YELLOW_DONGXI=1;//第一个状态东西、南北均亮红灯5SGREEN_NANBEI=0;if(aa==20){aa=0;temp--;shi1=temp/10;shi2=(temp+5)/10;ge1=temp%10;ge2=(temp+5)%10;if(temp==0){temp=26;break;}}display(ge1,shi1,ge2,shi2);}}void init5()//第五个状态:东西亮红灯、南北绿灯闪5次转亮黄灯5S {uint temp;temp=6;TMOD=0x01;TH0=(65535-50000)/256;TL0=(65535-50000)%256;EA=1;ET0=1;TR0=1;while(1){RED_NANBEI=1;RED_DONGXI=0;GREEN_DONGXI=1;GREEN_NANBEI=1;if(aa==20){aa=0;temp--;YELLOW_NANBEI=~YELLOW_NANBEI;shi1=temp/10;shi2=shi2;ge1=temp%10;ge2=ge1;if(temp==0){temp=6;break;}}display(ge1,shi1,ge2,shi2);}}void display(uint shi1,uint ge1,uint shi2,uint ge2) {DXweixuan1=0;DXweixuan2=1;NBweixuan1=1;NBweixuan2=1;P0=table[ge1];delay(5);DXweixuan1=1;DXweixuan2=0;NBweixuan1=1;NBweixuan2=1;P0=table[shi1];delay(5);DXweixuan1=1;DXweixuan2=1;NBweixuan1=0;NBweixuan2=1;P0=table[ge2];delay(5);DXweixuan1=1;DXweixuan2=1;NBweixuan1=1;NBweixuan2=0;P0=table[shi2];delay(5);}void xint0() interrupt 0 {RED_NANBEI=0;RED_DONGXI=0;GREEN_NANBEI=1;GREEN_DONGXI=1;YELLOW_NANBEI=1;YELLOW_DONGXI=1;P0=0x00;NBweixuan1=0;NBweixuan2=0;DXweixuan1=0;DXweixuan2=0;delay(2);return ;}void xint1() interrupt 2 {RED_NANBEI=1;RED_DONGXI=1;GREEN_NANBEI=0;GREEN_DONGXI=0;YELLOW_NANBEI=1;YELLOW_DONGXI=1;P0=0x00;NBweixuan1=0;NBweixuan2=0;DXweixuan1=0;DXweixuan2=0;delay(2);return ;}void xtimer0() interrupt 1 {TH0=(65535-50000)/256;TL0=(65535-50000)%256;aa++;}void delay(uint z){uint x,y;for(x=0;x<z;x++)for(y=0;y<110;y++); }。

单片机-交通灯程序(C语言)

单片机-交通灯程序(C语言)

/****************************************************************************** ** 实验名: 动态显示数码管实验* 使用的IO :* 实验效果: 数码管显示76543210。

* 注意:当位选用P1口的时候注意可能会有一位不亮,那么调整J21******************************************************************************* /#include<reg51.h>//--定义使用的IO口--//#define GPIO_DIG P0#define GPIO_PLACE P1#define GPIO_TRAFFIC P2sbit RED10 = P2^0; //上人行道红灯sbit GREEN10 = P2^1; //上人行道绿灯sbit RED11 = P2^2;sbit YELLOW11= P2^3;sbit GREEN11 = P2^4;sbit RED00 = P3^0; //右人行道红灯sbit GREEN00 = P3^1; //右人行道绿灯sbit RED01 = P2^5;sbit YELLOW01= P2^6;sbit GREEN01 = P2^7;//--定义全局变量--//unsigned char code DIG_PLACE[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//位选控制查表的方法控制unsigned char code DIG_CODE[17] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的显示码unsigned char DisplayData[8];//用来存放要显示的8位数的值unsigned char Time, Second; //用来存放定时时间//--声明全局函数--//void DigDisplay(); //动态显示函数void Timer0Cofig(void);/****************************************************************************** ** 函数名: main* 函数功能: 主函数* 输入: 无* 输出: 无******************************************************************************* /void main(void){Second = 1;Timer0Cofig();while(1){if(Second == 70){Second = 1;}//--宝田路通行,30秒--//if(Second < 31){DisplayData[0] = 0x00;DisplayData[1] = 0x00;DisplayData[2] = DIG_CODE[(30 - Second) % 100 / 10];DisplayData[3] = DIG_CODE[(30 - Second) %10];DisplayData[4] = 0x00;DisplayData[5] = 0x00;DisplayData[6] = DisplayData[2];DisplayData[7] = DisplayData[3];DigDisplay();//--宝田路通行--//GPIO_TRAFFIC = 0xFF; //将所有的灯熄灭RED00 = 1;GREEN00 = 1;GREEN11 = 0; //宝田路绿灯亮GREEN10 = 0; //宝田路人行道绿灯亮RED01 = 0; //前进路红灯亮RED00 = 0; //前进路人行道红灯亮}//--黄灯等待切换状态,5秒--//else if(Second < 36){DisplayData[0] = 0x00;DisplayData[1] = 0x00;DisplayData[2] = DIG_CODE[(35 - Second) % 100 / 10];DisplayData[3] = DIG_CODE[(35 - Second) %10];DisplayData[4] = 0x00;DisplayData[5] = 0x00;DisplayData[6] = DisplayData[2];DisplayData[7] = DisplayData[3];DigDisplay();//--黄灯阶段--//GPIO_TRAFFIC = 0xFF; //将所有的灯熄灭RED00 = 1;GREEN00 = 1;YELLOW11 = 0; //宝田路黄灯亮RED10 = 0; //宝田路人行道红灯亮YELLOW01 = 0; //前进路红灯亮RED00 = 0; //前进路人行道红灯亮}//--前进路通行--//else if(Second < 66){DisplayData[0] = 0x00;DisplayData[1] = 0x00;DisplayData[2] = DIG_CODE[(65 - Second) % 100 / 10];DisplayData[3] = DIG_CODE[(65 - Second) %10];DisplayData[4] = 0x00;DisplayData[5] = 0x00;DisplayData[6] = DisplayData[2];DisplayData[7] = DisplayData[3];DigDisplay();//--黄灯阶段--//GPIO_TRAFFIC = 0xFF; //将所有的灯熄灭RED00 = 1;GREEN00 = 1;RED11 = 0; //宝田路红灯亮RED10 = 0; //宝田路人行道红灯亮GREEN01 = 0; //前进路绿灯亮GREEN00 = 0; //前进路人行道绿灯亮}//--黄灯等待切换状态,5秒--//else{DisplayData[0] = 0x00;DisplayData[1] = 0x00;DisplayData[2] = DIG_CODE[(70 - Second) % 100 / 10];DisplayData[3] = DIG_CODE[(70 - Second) %10];DisplayData[4] = 0x00;DisplayData[5] = 0x00;DisplayData[6] = DisplayData[2];DisplayData[7] = DisplayData[3];DigDisplay();//--黄灯阶段--//GPIO_TRAFFIC = 0xFF; //将所有的灯熄灭RED00 = 1;GREEN00 = 1;YELLOW11 = 0; //宝田路黄灯亮RED10 = 0; //宝田路人行道红灯亮YELLOW01 = 0; //前进路红灯亮RED00 = 0; //前进路人行道红灯亮}}}/****************************************************************************** ** 函数名: DigDisplay* 输入: 无* 输出: 无******************************************************************************* /void DigDisplay(){unsigned char i;unsigned int j;for(i=0; i<8; i++){GPIO_PLACE = DIG_PLACE[i]; //发送位选GPIO_DIG = DisplayData[i]; //发送段码j = 10; //扫描间隔时间设定while(j--);GPIO_DIG = 0x00; //消隐}}/****************************************************************************** ** 函数名: Timer0Cofig* 函数功能: 配置定时器* 输入: 无* 输出: 无******************************************************************************* /void Timer0Cofig(void){TMOD = 0x01; //定时器0选择工作方式1TH0 = 0x3C; //设置初始值,定时50MSTL0 = 0xB0;EA = 1; //打开总中断ET0 = 1; //打开定时器0中断TR0 = 1; //启动定时器0}/****************************************************************************** ** 函数名: Timer0* 输入: 无* 输出: 无******************************************************************************* /void Timer0() interrupt 1{TH0 = 0x3C; //设置初始值TL0 = 0xB0;Time++;if(Time == 20){Second ++;Time = 0;}}。

交通灯控制系统C程序代码

交通灯控制系统C程序代码
delay(100); //延时
i=v%100/10; //暂存十位
P0=led_seg_code[i];
P2=0xfd;
delay(100); //延时
-----------
void timer1() interrupt 3 //T1中断
{ T1_cnt++;
EW_red=1 ;//东西方向红灯
break;
}
}
}
}
//-------------------------
main()
{//初始化各变量
cnt_sn=init_sn[0];
cnt_ew=init_ew[0];
T1_cnt=0;
state_val_sn=0; //启动后,默认工作在序号为1 的状态
if (state_val_ew>2) state_val_ew=0;
cnt_ew=init_ew[state_val_ew];
switch (state_val_ew) //根据状态值,刷新各信号灯的状态
{ case 0: EW_green=1 ;//东西方向绿灯
EW_yellow=1;//东西方向黄灯
//晶振:11.0592M T1-250微秒溢出一次
/*变量的定义:
show_val_sn,show_val_ew: 显示的值0-59
state_val_sn,state_val_ew: 状态值 南北方向0-绿灯亮;1-黄灯亮;2-红灯亮
T1_cnt: 定时器计数溢出数
cnt_sn,cnt_ew: 倒计时的数值
init_sn[3],init_ew[3] 倒计时

交通灯C语言程序

交通灯C语言程序

#include <reg51.h>#define uchar unsigned char#define uint unsigned intuchar data buf[4];uchar data sec_dx=20; //东西数默认uchar data sec_nb=30; //南北默认值uchar data set_timedx=20; //设置东西方向的时间uchar data set_timenb=30; //设置南北方向的时间int n;uchar data countt0;//定时器0中断次数//定义5组开关sbit k4=P3^7; //切换方向sbit k1=P3^5; //时间加sbit k2=P3^6; //时间减sbit k3=P3^4; //确认sbit k5=P3^1; //禁止// P3^2 //只允许东西方向通行,中断0// P3^3 //只允许南北方向通行,中断1sbit Red_nb=P2^6; //南北红灯标志sbit Yellow_nb=P2^5; //南北黄灯标志sbit Green_nb=P2^4; //南北绿灯标志sbit Red_dx=P2^3; //东西红灯标志sbit Yellow_dx=P2^2; //东西黄灯标志sbit Green_dx=P2^1; //东西绿灯标志sbit Buzz=P3^0;bit Buzzer_Indicate;bit set=0; //调时方向切换键标志=1时,南北,=0时,东西bit dx_nb=0; //东西南北控制位bit shanruo=0;uchar code table[11]={ //共阴极字型码0x3f, //--00x06, //--10x5b, //--20x4f, //--30x66, //--40x6d, //--50x7d, //--60x07, //--70x7f, //--80x6f, //--90x00 //--NULL};//函数的声明部分void delay(int ms); //延时子程序void key(); //按键扫描子程序void key_to1(); //键处理子程序void key_to2();void key_to3();void display(); //显示子程序void logo(); //开机LOGOvoid Buzzer();//主程序void main(){TMOD=0X11; //定时器设置TH0=0X3C; //定时器0置初值0.05STL0=0XB0;EA=1; //开总中断ET0=1; //定时器0中断开启TR0=1; //启动定时0EX0=1; //开外部中断0EX1=1; //开外部中断1logo();P2=0Xc3; // 开始默认状态,东西绿灯,南北黄灯sec_nb=sec_dx+5; //默认南北通行时间比东西多5秒while(1){key(); //调用按键扫描程序display(); //调用显示程序Buzzer();}}//函数的定义部分void key(void) //按键扫描子程序{if(k1!=1) //当K1(时间加)按下时{display(); //调用显示,用于延时消抖if(k1!=1){TR0=0; //关定时器shanruo=0;P2=0x00;if(set==0)set_timedx++; //南北加1Selseset_timenb++; //东西加1Sif(set_timenb==100)set_timenb=1;if( set_timedx==100)set_timedx=1; //加到100置1sec_nb=set_timenb ; //设置的数值赋给东西南北sec_dx=set_timedx; //do{display();}while(k1!=1);}}if(k2!=1) //当K2(时间减)按键按下时{display(); //调用显示,用于延时消抖if(k2!=1){TR0=0; //关定时器shanruo=0;P2=0x00;if(set==0)set_timedx--; //南北减1Selseset_timenb--; //东西减1Sif(set_timenb==0)set_timenb=99;if( set_timedx==0 )set_timedx=99; //减到1重置99sec_nb=set_timenb ; //设置的数值赋给东西南北sec_dx=set_timedx;do{display(); //调用显示,用于延时}while(k2!=1);}}if(k3!=1) //当K3(确认)键按下时{display(); //调用显示,用于延时消抖if(k3!=1){TR0=1; //启动定时器Buzzer_Indicate=0;sec_nb=set_timenb; //从中断回复,仍显示设置过的数值sec_dx=set_timedx; //显示设置过的时间if(set==0) //时间倒时到0时{P2=0X00; //东西绿灯,南北红灯Green_dx=1;Red_nb=1;sec_nb=sec_dx+5; //回到初值}else{P2=0x00; //南北绿灯,东西红灯Green_nb=1;Red_dx=1;sec_dx=sec_nb+5;}}}if(k4!=1) //当K4(切换)键按下{display(); //调用显示,用于延时消抖if(k4!=1){TR0=0;set=!set; //取反set标志位,以切换调节方向dx_nb=set;do{display();}while(k4!=1);}}if(k5!=1) //当K5(禁止)键按下时{display(); //调用显示,用于延时消抖if(k5!=1){TR0=0; //关定时器P2=0x00;Red_dx=1;Red_nb=1; //全部置红灯sec_dx=00; //四个方向的时间都为00sec_nb=00;Buzzer_Indicate=1;do{display();}while(k5!=1);}}}void display(void) //显示子程序{buf[1]=sec_nb/10; //第1位东西秒十位buf[2]=sec_nb%10; //第2位东西秒个位buf[3]=sec_dx/10; //第3位南北秒十位buf[0]=sec_dx%10; //第4位南北秒个位P1=0xff; // 初始灯为灭的P0=0x00;P1=0xfe; //片选LED1P0=table[buf[1]]; //送东西时间十位的数码管编码delay(1); //延时P1=0xff; //关显示P0=0x00;P1=0xfd; //片选LED2P0=table[buf[2]];delay(1);P1=0xff;P0=0x00;P1=0Xfb; //片选LED3P0=table[buf[3]];delay(1);P1=0xff;P0=0x00;P1=0Xf7;P0=table[buf[0]]; //片选LED4delay(1);}void time0(void) interrupt 1 using 1 //定时中断子程序{TH0=0X3C; //重赋初值TL0=0XB0;TR0=1; //重新启动定时器countt0++; //软件计数加1if(countt0==10){if((sec_nb<=5)&&(dx_nb==0)&&(shanruo==1)) //东西黄灯闪{Green_dx=0;Yellow_dx=0;}if((sec_dx<=5)&&(dx_nb==1)&&(shanruo==1)) //南北黄灯闪{Green_nb=0;Yellow_nb=0;}}if(countt0==20) // 定时器中断次数=20时(即1秒时){ countt0=0; //清零计数器sec_dx--; //东西时间减1sec_nb--; //南北时间减1if((sec_nb<=5)&&(dx_nb==0)&&(shanruo==1)) //东西黄灯闪{Green_dx=0;Yellow_dx=1;}if((sec_dx<=5)&&(dx_nb==1)&&(shanruo==1)) //南北黄灯闪{Green_nb=0;Yellow_nb=1;}if(sec_dx==0&&sec_nb==5) //当东西倒计时到0时,重置5秒,用于黄灯闪烁时间{sec_dx=5;shanruo=1;}if(sec_nb==0&&sec_dx==5) //当南北倒计时到0时,重置5秒,用于黄灯闪烁时间{sec_nb=5;shanruo=1;}if(dx_nb==0&&sec_nb==0) //当黄灯闪烁时间倒计时到0时,{P2=0x00; //重置东西南背方向的红绿灯Green_nb=1;Red_dx=1;dx_nb=!dx_nb;shanruo=0;sec_nb=set_timenb; //重赋南北方向的起始值sec_dx=set_timenb+5; //重赋东西方向的起始值}if(dx_nb==1&&sec_dx==0) //当黄灯闪烁时间到{P2=0X00; //重置东西南北的红绿灯状态Green_dx=1;Red_nb=1;dx_nb=!dx_nb;shanruo=0;sec_dx=set_timedx; //重赋东西方向的起始值sec_nb=set_timedx+5; //重赋南北方向的起始值}}}//外部中断0void int0(void) interrupt 0 using 1 //只允许东西通行{TR0=0; //关定时器P2=0x00;Green_dx=1; //东西方向置绿灯Red_nb=1; //南北方向为红灯Buzzer_Indicate=0;sec_dx=00; //四个方向的时间都为00sec_nb=00;}//外部中断1void int1(void) interrupt 2 using 1 //只允许南北通行{TR0=0; //关定时器P2=0x00;Green_nb=1; //置南北方向为绿灯Red_dx=1; //东西方向为红灯Buzzer_Indicate=0;sec_nb=00; //四个方向的时间都为00 sec_dx=00;}void logo()//开机的Logo "- - - -"{for(n=0;n<50;n++){P0=0x40;P1=0xfe;delay(1);P1=0xfd;delay(1);P1=0Xfb;delay(1);P1=0Xf7;delay(1);P1 = 0xff;}}void Buzzer(){if(Buzzer_Indicate==1)Buzz=!Buzz;else Buzz=1;}void delay(int ms) //延时子程序{uint j,k;for(j=0;j<ms;j++) //延时msfor(k=0;k<124;k++); //大约1毫秒的延时}。

C语言智能交通信号灯优化交通信号控制

C语言智能交通信号灯优化交通信号控制

C语言智能交通信号灯优化交通信号控制智能交通信号灯是现代城市交通管理的重要组成部分。

通过合理优化交通信号控制,可以提高交通效率、减少交通拥堵,改善交通流畅度和安全性。

本文将重点介绍基于C语言的智能交通信号灯优化算法,以实现更高效的交通信号控制。

一、引言在城市交通信号灯控制系统中,传统方法往往是固定时间间隔控制,无法根据实时交通流量做出调整。

此外,传统方法忽略了交通流的复杂性和非均匀性,导致效果较差。

因此,利用C语言编写智能交通信号灯优化算法成为了一种重要的解决方案。

二、目标与原理智能交通信号灯优化的目标是使车辆在不同方向上的等待时间最短,从而提高交通流畅度。

该算法主要基于两个原理:信号灯状态的优化和灯色切换的策略。

1. 信号灯状态的优化智能交通信号灯优化算法通过根据交通流量和道路拥堵情况判断各个信号灯状态,以实现最优的交通流量。

算法会根据道路的状况,动态调整信号灯的绿灯亮起时间和红灯亮起时间,使得交通流量得到最优分配。

2. 灯色切换的策略智能交通信号灯优化算法根据交通流量和道路拥堵情况,采用不同的灯色切换策略。

算法会根据交通流量的变化,自动调整信号灯的切换频率,以更快速地适应不同道路状况,保障交通流畅。

三、实现步骤基于C语言的智能交通信号灯优化算法的实现步骤如下:1. 数据采集首先,需要利用传感器等设备采集交通流量、道路状况等数据。

这些数据将作为优化算法的输入,用于分析判断道路的拥堵情况。

2. 信号灯状态判断根据采集到的数据,通过一定的判定条件,判断各个信号灯的状态,即判断应该亮起绿灯还是红灯。

3. 信号灯状态优化根据判断结果,对信号灯的状态进行优化调整。

优化的方法可以是基于规则的判定,也可以是基于算法的优化策略。

根据交通流量和道路状况,调整信号灯的绿灯亮起时间和红灯亮起时间,以实现最优的交通流量。

4. 灯色切换策略调整根据交通流量的变化,自动调整信号灯的切换频率。

通过实时的交通数据分析,不断调整灯色切换的策略,使得交通信号灯能够更快速地适应不同道路状况。

交通灯C语言程序交通灯C语言程序,AT89S52单片机控制,倒计时,红黄绿灯

交通灯C语言程序交通灯C语言程序,AT89S52单片机控制,倒计时,红黄绿灯

交通灯//==================================//====交通灯C语言程序//====作者:2009的竹蜻蜓//====时间:2009-4-26//====E-MAIL:guangxian616@//==================================#include<reg52.h>#define uchar unsigned char#define time 50000#define ON 0#define OFF 1sbit NSB=P1^5; //南北绿灯sbit NSY=P1^4; //南北黄灯sbit NSR=P1^3; //南北红灯sbit EWB=P1^2; //东西绿灯sbit EWY=P1^1; //东西黄灯sbit EWR=P1^0; //东西红灯sbit P27=P2^7;sbit P26=P2^6;sbit P23=P2^3;sbit P22=P2^2;/*===============倒计时=========================*/ uchar N=15,Y=12,Z=15,M=27,G=30,U=30;uchar C50ms;uchar seg=1;void InitialT1(void){ET1=0;TMOD=0x10;TH1=(65536-time)>>8;TL1=(65536-time)&0xff;TR1=1;ET1=1;EA=1;}void ISRT1(void) interrupt 3{TR1=0;TH1=(65536-time)>>8;TL1=(65536-time)&0xff;TR1=1;C50ms++;if(C50ms>19){N--;M--;Y--;Z--;G--;U--;C50ms=0;}}/*=================延时=======================*/void Delay10uS(uchar second){int i;while((second--)!=0){for(i=0;i<3;i++){;}}}/*==================led控制========================*/uchar discode[10]={0xC0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};void display1(uchar num1,uchar num2){P0=discode[num1]; P22=ON; Delay10uS(1); P22=OFF; //控制南led显示P0=discode[num2]; P23=ON; Delay10uS(1); P23=OFF;}void display2(uchar num3,uchar num4){P0=discode[num3]; P26=ON; Delay10uS(1); P26=OFF; //控制东西led显示P0=discode[num4]; P27=ON; Delay10uS(1); P27=OFF;}main(){int i;// P0=0xff;/*=============初始状态:东西南北都红灯==================*/ NSR=ON;EWR=ON;for(i=0;i<800;i++){Delay10uS(20);}NSR=OFF;EWR=OFF;while(1){InitialT1(); // 初始化计时器/*=============状态1:东西绿灯(12s) 南北红灯(15s)==================*/ /*=============状态2:东西黄灯(3s) 南北红灯(15s)=================*/ EWB=ON;NSR=ON;while(N!=0){display1(N/10,N%10);// 南北红灯(15s)display2(Y/10,Y%10);// 东西绿灯(12s)while(N==3){while(Z!=0){display1(N/10,N%10);// 南北红灯(3s)display2(Z/10,Z%10);// 东西黄灯(3s)EWB=OFF;EWY=ON;}}}NSR=OFF;EWB=OFF;EWY=OFF;P0=0xff; //全关掉//Delay10uS(1);/*=============状态3:东西红灯(15s) 南北绿灯(12s)==================*/ /*=============状态4:东西红灯(15s) 南北黄灯(3s)==================*/ EWR=ON;NSB=ON;while(G!=0){display2(G/10,G%10);// 东西红灯(12s)display1(M/10,M%10);// 南北绿灯(12s)while(G==3){while(U!=0){display2(G/10,G%10);// 东西红灯(3s)display1(U/10,U%10);// 南北绿灯(3s)NSB=OFF;NSY=ON;}}}EWR=OFF;NSB=OFF;NSY=OFF;//P0=0xFF;//Delay10uS(1);/*=====================重新赋值===============================*/N=15,Y=12,Z=15,M=27,G=30,U=30;}}//欢迎访问我的百度空间:2009的竹蜻蜓更多精彩等着你!。

智能交通灯c源代码

智能交通灯c源代码

0x00,0x38,0x03,0x80,0x07,0x03,0x07,0x00, 0x00,0x38,0x03,0x80,0x07,0x03,0x87,0x00, 0x00,0x38,0x03,0x80,0x07,0x03,0x87,0x00, 0x00,0x38,0x03,0x80,0x07,0x01,0x87,0x00, 0x00,0x38,0x03,0x80,0x07,0x01,0x87,0x00, 0x00,0x38,0x03,0x83,0xFF,0x01,0x87,0x00, 0x00,0x38,0x1F,0xFF,0xFF,0x01,0x87,0x00, 0x00,0x38,0x1F,0xF8,0x07,0x01,0xC7,0x00, 0x00,0x38,0x10,0x00,0x0F,0x01,0xC7,0x00, 0x00,0x38,0x00,0x00,0x0F,0x00,0xC7,0x00, 0x00,0x38,0x00,0x00,0x1F,0x00,0xC7,0x00, 0x00,0x38,0x00,0x00,0x37,0x00,0xC7,0x00, 0x00,0x38,0x00,0x00,0x67,0x00,0x07,0x00, 0x00,0x38,0x00,0x00,0xE7,0x00,0x07,0x00, 0x00,0x38,0x18,0x01,0xC7,0x00,0x07,0x00, 0x00,0x38,0x18,0x03,0x87,0x00,0x07,0x00, 0x00,0x38,0x30,0x07,0x07,0x00,0x07,0x00, 0x00,0x38,0x70,0x0E,0x07,0x00,0x07,0x00, 0x00,0x38,0xE0,0x3C,0x07,0x00,0x07,0x00, 0x00,0x38,0xC0,0x70,0x07,0x00,0x07,0x00, 0x00,0x39,0x80,0xE0,0x07,0x00,0x07,0x00, 0x00,0x3F,0x03,0xC0,0x07,0x00,0x07,0x00, 0x00,0x3E,0x07,0x80,0x07,0x00,0x07,0x00, 0x00,0x1C,0x1E,0x00,0x07,0x00,0x07,0x00, 0x00,0x18,0x1C,0x00,0x07,0x00,0x07,0x00, 0x00,0x00,0x00,0x1F,0x0E,0x0E,0x07,0x00, 0x00,0x00,0x00,0x1F,0xFE,0x0F,0xFE,0x00, 0x00,0x00,0x00,0x01,0xF8,0x03,0xFC,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, }; char zhi[]={ 0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00, 0x00,0x08,0x01,0x80,0x00,0x0C,0x00,0x80, 0x00,0x04,0x00,0x80,0x00,0x04,0x00,0x80, 0x00,0xC7,0x00,0x80,0x00,0xCF,0xA1,0x80, 0x00,0xFE,0x11,0x80,0x00,0xFC,0x19,0x80,
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
TH1=0x3c;
TL1=0x2b;
TIM2++;
if(TIM2==10)
{
TIM2=ቤተ መጻሕፍቲ ባይዱ;
FLAG2=!FLAG2;
}
}
互相学习可以加QQ:842592293
TIM=SIGN;
}
void INT()
{
EA=1;
TMOD=0X01;
TR0=1;
ET0=1;
TH0=0X3C;
TL0=0X2B;
}
void UINT()
{
EA=0;
TMOD=0X00;
TR0=0;
ET0=0;
TH0=0X00;
TL0=0X00;
}
void T1_INIT()
{
EA=1;
TMOD=0X10;
TR1=1;
ET1=1;
TH1=(65535-50000)%255;
TL1=(65535-50000)/255;
}
void UT1_INIT()
{
EA=0;
TMOD=0X00;
TR1=0;
ET1=0;
TH1=0;
TL1=0;
}
void erro()
{
hong_D=0;
lv_D=1;
huang_D=1;
delay(5);
night();
UT1_INIT();
INT();
}
}
void main()
{
init();
delay(3000);
FLAG=1;
INT();
while(1)
{
if(TIM>3)
{
lv_D=FLAG;
huang_D=1;
hong_D=!FLAG;
lv_B=!FLAG;
huang_B=1;
while(KEY3==0);
delay(5);
if(TIM<4)
{
TIM=SIGN;
}
SIGN=TIM;
while(1)
{
if(KEY1==0)
{
delay(5);
while(KEY1==0)
{
seg1(TIM);
seg2(TIM-3);
}
delay(5);
TIM++;
if(TIM>99)
{
TIM=4;
sbit DIG2=P2^7;
sbit EN2=P2^2;
sbit DIG3=P2^4;
sbit DIG4=P2^5;
sbit KEY1=P3^0;
sbit KEY2=P3^1;
sbit KEY3=P3^2;
sbit KEY4=P3^3;
sbit KEY5=P2^0;
sbit KEY6=P2^1;
void keyscan();
INT();
break;
}
seg1(TIM);
seg2(TIM-3);
}
}
if(KEY5==0)
{
delay(5);
UINT();
while(KEY5==0);
delay(5);
erro();
INT();
}
if(KEY6==0)
{
delay(5);
UINT();
T1_INIT();
while(KEY6==0);
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
const uchar SMG[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6F};
uchar NUM,SIGN=10,TIM,TIM2;
delay(1);
DIG2=1;
P0=0X00;
}
void seg2(uint x)
{
DIG3=0;
P0=SMG[x/10];
delay(1);
DIG3=1;
P0=0X00;
DIG4=0;
P0=SMG[x%10];
delay(1);
DIG4=1;
P0=0X00;
}
void init()
{
P1=0X00;
bit FLAG,FLAG2;
sbit hong_D=P1^0;
sbit lv_D=P1^1;
sbit huang_D=P1^2;
sbit hong_B=P1^3;
sbit lv_B=P1^4;
sbit huang_B=P1^5;
sbit BEEP=P3^4;
sbit EN1=P2^3;
sbit DIG1=P2^6;
hong_B=0;
lv_B=1;
huang_B=1;
BEEP=0;
P0=0x00;
while(KEY5!=0);
delay(5);
while(KEY5==0);
BEEP=1;
delay(5);
}
void night()
{
hong_D=1;
lv_D=1;
hong_B=1;
lv_B=1;
while(1)
}
SIGN=TIM;
}
if(KEY2==0)
{
delay(5);
while(KEY2==0)
{
seg1(TIM);
seg2(TIM-3);
}
delay(5);
TIM--;
if(TIM<4)
{
TIM=99;
}
SIGN=TIM;
}
if(KEY4==0)
{
delay(5);
while(KEY1==0);
delay(5);
hong_B=FLAG;
if(FLAG)
{
seg1(TIM);
seg2(TIM-3);
}
else
{
seg1(TIM-3);
seg2(TIM);
}
}
if(TIM<4)
{
lv_D=1;
huang_D=FLAG;
hong_D=!FLAG;
lv_B=1;
huang_B=!FLAG;
hong_B=FLAG;
seg1(TIM);
uint M,N;
void delay(uint n)
{
uint x,y;
for(x=n;x>0;x--)
for(y=110;y>0;y--);
}
void seg1(uint x)
{
DIG1=0;
P0=SMG[x/10];
delay(1);
DIG1=1;
P0=0X00;
DIG2=0;
P0=SMG[x%10];
seg2(TIM);
}
keyscan();
}
}
void time0() interrupt 1
{
TL0=0XB0;
TH0=0X3C;
NUM++;
if(NUM==20)
{
NUM=0;
TIM--;
if(TIM==0)
{
TIM=SIGN;
FLAG=!FLAG;
}
}
}
void time1() interrupt 3
{
if(FLAG2)
{
huang_D=0;
huang_B=0;
}
else
{
huang_D=1;
huang_B=1;
}
if(KEY6==0)
{
delay(5);
while(KEY6==0);
delay(5);
break;
}
}
}
void keyscan()
{
if(KEY3==0)
{
delay(5);
UINT();
相关文档
最新文档