|
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};//數(shù)碼管共陽極顯示碼
//定義引腳
#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中斷次數(shù),秒數(shù)
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'};//存放狀態(tài)即#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)//如果接收有數(shù)據(jù),處理接收的數(shù)據(jù)
{ //*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();//清除接收緩沖區(qū)
state[2]=DXgreen/10+'0';state[3]=DXgreen%10+'0'; state[4]=NBgreen/10+'0';//再發(fā)送一次狀態(tài)數(shù)據(jù)
state[5]=NBgreen%10+'0';state[6]=yellow+'0'; state[7]=control+'0';
sendstring(state);//狀態(tài)有改變時再發(fā)送一次狀態(tài)數(shù)據(jù)
}
}
void main()
{
UARTinit();
//T0中斷設(shè)置
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';//運行前發(fā)送一次狀態(tài)數(shù)據(jù)
state[5]=NBgreen%10+'0';state[6]=yellow+'0'; state[7]=control+'0';
sendstring(state); //運行前發(fā)送一次狀態(tài)數(shù)據(jù)
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;//查看有沒有接收的數(shù)據(jù)并處理
}
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;//查看有沒有接收的數(shù)據(jù)并處理
} 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;//查看有沒有接收的數(shù)據(jù)并處理
}
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;//查看有沒有接收的數(shù)據(jù)并處理
}second=0;
}
//下面兩行為PC控制
if(control==1) LIGHT=0x21;//PC發(fā)送控制為1,讓東西通行
if(control==2) LIGHT=0x0c; //PC發(fā)送控制為2,讓南北通行
}
}
|
評分
-
查看全部評分
|