自动控制交通灯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源代码

智能交通灯源代码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 语言实现交通灯(红绿灯)源程序交通灯,红黄绿灯交替亮,怎样实现呢?其实就是根据单片机定时器及倒计时的程序修改。

源程序如下:#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语言程序

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语言程序

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++); }。

  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();
相关文档
最新文档