|
pc端控制交通灯,供大家学习
代码如下
#include <reg51.h>
#include <uart.h>
unsigned char table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};//数码管共阳极显示码
//定义引脚
#define NBSEG P0
#define DXSEG P2
#define LIGHT P1
sbit NBWEI1=P3^4;
sbit NBWEI2=P3^5;
sbit DXWEI1=P3^6;
sbit DXWEI2=P3^7;
//定义变量
unsigned char t0count=0,second=0; //t0中断次数,秒数
unsigned char DXgreen=30;yellow=4;NBgreen=20;//绿灯和黄灯时间
unsigned char control=0;//0为自动运行,1为手动或PC控制东西通行,2为南北通行
unsigned char state[8]={'#','S',0,0,0,0,0,'\0'};//存放状态即#S东西绿灯时间,南北绿灯时间,黄灯时间和控制方式
//延时程序
void delayt()
{
unsigned char i,j;
for(i=0;i<50;i++)for(j=0;j<50;j++);
}
//T0中断程序
void T0int() interrupt 1
{
TH0=(65536-50000)/256;TL0=(65536-50000)%256; //再计时50毫秒
t0count++;
if(t0count==20){t0count=0;second++;}
}
void revset()
{
if(revover==1)//如果接收有数据,处理接收的数据
{ //*S35205,'35'为东本绿灯35秒,‘20’为南北绿灯20秒,5为黄灯5秒
//*C1,1控制东西通行,*C2南北通行,*C0自动运行。
if(revbuf[1]=='S') {DXgreen=(revbuf[2]-'0')*10+revbuf[3]-'0';NBgreen=(revbuf[4]-'0')*10+revbuf[5]-'0';yellow=revbuf[6]-'0';}
if(revbuf[1]=='C')control=revbuf[2]-'0';
clearrevbuf();//清除接收缓冲区
state[2]=DXgreen/10+'0';state[3]=DXgreen%10+'0'; state[4]=NBgreen/10+'0';//再发送一次状态数据
state[5]=NBgreen%10+'0';state[6]=yellow+'0'; state[7]=control+'0';
sendstring(state);//状态有改变时再发送一次状态数据
}
}
void main()
{
UARTinit();
//T0中断设置
PS=1;
EA=1;ET0=1; //允许中断
TMOD=0x21;//方式1,16位计时
TH0=(65536-50000)/256;TL0=(65536-50000)%256; //计时50毫秒
TR0=1;//启动计时器T0
state[2]=DXgreen/10+'0';state[3]=DXgreen%10+'0'; state[4]=NBgreen/10+'0';//运行前发送一次状态数据
state[5]=NBgreen%10+'0';state[6]=yellow+'0'; state[7]=control+'0';
sendstring(state); //运行前发送一次状态数据
while(1)
{
revset();second=0; DXSEG=0xff;NBSEG=0xff;//清屏
if(control==0){//自动运行
//东西绿灯亮30秒,黄灯亮4秒, 红灯亮24秒
//南北红灯亮34秒, 绿灯亮20秒,黄灯亮4秒
LIGHT=0x21;
while(second<DXgreen){DXSEG=0xff;NBSEG=0xff;//清屏
DXWEI2=1;DXWEI1=0;DXSEG=table[second%10];delayt();//显示东西个位
NBWEI1=0;NBWEI2=1;NBSEG=table[second%10];delayt(); //显示南北个位
DXSEG=0xff;NBSEG=0xff; //清屏
DXWEI2=0;DXWEI1=1;DXSEG=table[second/10];delayt(); //显示东西十位
NBWEI1=1;NBWEI2=0;NBSEG=table[second/10];delayt(); //显示南北十位
revset();if(control>0)break;//查看有没有接收的数据并处理
}
LIGHT=0x11;
while(second<(DXgreen+yellow)){DXSEG=0xff;NBSEG=0xff;//清屏
DXWEI2=1;DXWEI1=0;DXSEG=table[second%10];delayt();
NBWEI1=0;NBWEI2=1;NBSEG=table[second%10];delayt();
DXSEG=0xff;NBSEG=0xff;
DXWEI2=0;DXWEI1=1;DXSEG=table[0]; delayt();
NBWEI1=1;NBWEI2=0;NBSEG=table[second/10];delayt();
revset();if(control>0)break;//查看有没有接收的数据并处理
} second=0;
LIGHT=0x0c;
while(second<NBgreen){DXSEG=0xff;NBSEG=0xff;
DXWEI2=1;DXWEI1=0;DXSEG=table[second%10];delayt();
NBWEI1=0;NBWEI2=1;NBSEG=table[second%10];delayt();
DXSEG=0xff;NBSEG=0xff;
DXWEI2=0;DXWEI1=1;DXSEG=table[second/10];delayt();
NBWEI1=1;NBWEI2=0;NBSEG=table[second/10];delayt();
revset();if(control>0)break;//查看有没有接收的数据并处理
}
LIGHT=0x0a;
while(second<(NBgreen+yellow)){DXSEG=0xff;NBSEG=0xff;
DXWEI2=1;DXWEI1=0;DXSEG=table[second%10];delayt();
NBWEI1=0;NBWEI2=1;NBSEG=table[second%10];delayt();
DXSEG=0xff;NBSEG=0xff;
DXWEI2=0;DXWEI1=1;DXSEG=table[second/10];delayt();
NBWEI1=1;NBWEI2=0;NBSEG=table[0];delayt();
revset();if(control>0)break;//查看有没有接收的数据并处理
}second=0;
}
//下面两行为PC控制
if(control==1) LIGHT=0x21;//PC发送控制为1,让东西通行
if(control==2) LIGHT=0x0c; //PC发送控制为2,让南北通行
}
}
|
评分
-
查看全部评分
|