基于单片机的交通灯设计c程序.docx
《基于单片机的交通灯设计c程序.docx》由会员分享,可在线阅读,更多相关《基于单片机的交通灯设计c程序.docx(19页珍藏版)》请在冰豆网上搜索。
基于单片机的交通灯设计c程序
#include
sbitRED_E=P1^0;
sbitYELLOW_E=P1^1;
sbitGREEN_E=P1^2;
sbitRED_N=P1^3;
sbitYELLOW_N=P1^4;
sbitGREEN_N=P1^5;
sbits1=P2^4;
sbits2=P2^5;
sbits3=P2^6;
sbits4=P2^7;
sbittype=P1^6;
sbitk1=P2^0;
sbitk2=P2^1;
sbitk3=P2^2;
sbitk4=P2^3;
#defineucharunsignedchar//宏定义
#defineuintunsignedint
voidcpuInit();
voidDisplay();
voidkey_analyse();
voidsmec(unsignedcharx);
voidDisplay2();
voidt0_time();
voidkv_set_E();
voidkv_set_N();
voidpc_EN();
//voidkv_set_pc();
voidset_pc();
ucharR_E,Y_E,G_E,R_N,Y_N,G_N;
ucharFlash_Count=0,Operation_Type=1,key,Set_flag,flag=0,x=0,x1=0,x2=0,x3=0,m1,m2,flag_pc=0,temp;
ucharled_seg_code[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
inta=0;
uchartime;
intt,t2,t3,t4;
voidcpuInit()//初始化
{
TMOD=0x21;//定时器0按方式1工作
//PCON=0x7f;
SCON=0x70;
ET0=1;
EA=1;
TH0=0x3C;//定时50ms(11.0592M)
TL0=0x0B0;
TH1=0x0f3;
TL1=0x0f3;
ES=1;
TR1=1;
P1=0x0ff;//关LED
s1=1;s2=1;s3=1;s4=1;
P0=0x00;
P2=0x0ff;
R_E=14;
Y_E=4;
G_E=15;
R_N=19;
Y_N=4;
G_N=10;
}
voidDisplay()//普通模式下显示
{
//uchartime;
//intt,t2;
uchari;
//intj;
m1=P0;
m2=P0;
switch(Operation_Type)
{
case1:
RED_E=1;YELLOW_E=1;GREEN_E=0;
RED_N=0;YELLOW_N=1;GREEN_N=1;
if(x==0)
{
i=G_E;
x=1;
time=R_N;
t=time/10;
t2=(int)time%10;
t3=i/10;
t4=(int)i%10;
}
P0=led_seg_code[t2];
s2=0;
s2=1;
P0=led_seg_code[t4];
s4=0;
s4=1;
P0=led_seg_code[t];
s1=0;
s1=1;
P0=led_seg_code[t3];
s3=0;
s3=1;
if(flag==1)
{
flag=0;
i--;
time--;
t=time/10;
t2=(int)time%10;
t3=i/10;
t4=(int)i%10;
P0=led_seg_code[t2];
s2=0;
s2=1;
P0=led_seg_code[t4];
s4=0;
s4=1;
P0=led_seg_code[t];
s1=0;
s1=1;
P0=led_seg_code[t3];
s3=0;
s3=1;
if(i==0)
{
x=0;
Operation_Type=2;
}
}
break;
case2:
GREEN_E=1;
YELLOW_E=0;
P0=led_seg_code[t2];
s2=0;
s4=0;
s2=1;
s4=1;
P0=led_seg_code[t];
s1=0;
s3=0;
s1=1;
s3=1;
if(flag==1)
{
flag=0;
x1++;
time--;
t=time/10;
t2=(int)time%10;
P0=led_seg_code[t2];
s2=0;
s4=0;
s2=1;
s4=1;
P0=led_seg_code[t];
s1=0;
s3=0;
s1=1;
s3=1;
}
if(x1==4)
{
x1=0;
Operation_Type=3;
}
break;
case3:
RED_E=0;YELLOW_E=1;GREEN_E=1;
RED_N=1;YELLOW_N=1;GREEN_N=0;
if(x2==0)
{
i=G_N;
x2=1;
time=R_E;
t=time/10;
t2=(int)time%10;
t3=i/10;
t4=(int)i%10;
}
P0=led_seg_code[t2];
s4=0;
s4=1;
P0=led_seg_code[t4];
s2=0;
s2=1;
P0=led_seg_code[t];
s3=0;
s3=1;
P0=led_seg_code[t3];
s1=0;
s1=1;
if(flag==1)
{
flag=0;
i--;
time--;
t=time/10;
t2=(int)time%10;
t3=i/10;
t4=(int)i%10;
P0=led_seg_code[t2];
s4=0;
s4=1;
P0=led_seg_code[t4];
s2=0;
s2=1;
P0=led_seg_code[t];
s3=0;
s3=1;
P0=led_seg_code[t3];
s1=0;
s1=1;
if(i==0)
{
x2=0;
Operation_Type=4;
}
}
break;
case4:
GREEN_N=1;
YELLOW_N=0;
P0=led_seg_code[t2];
s2=0;
s4=0;
s2=1;
s4=1;
P0=led_seg_code[t];
s1=0;
s3=0;
s1=1;
s3=1;
if(flag==1)
{
flag=0;
x3++;
time--;
t=time/10;
t2=(int)time%10;
P0=led_seg_code[t2];
s2=0;
s4=0;
s2=1;
s4=1;
P0=led_seg_code[t];
s1=0;
s3=0;
s1=1;
s3=1;
}
if(x3==4)
{
x3=0;
Operation_Type=1;
}
break;
}
}
//voiddelay1ms()//1ms延时
//{
//inti,j;
//for(i=2;i>0;i--)
//for(j=248;j>0;j--);
//}
voidkey_analyse()
{
unsignedcharkey_value;
key_value=P2;
if((key_value&0x0f)!
=0x0f)//有键按下
{
smec(150);
key_value=key_value&0x0f;
key=key_value;
switch(key)
{
case0x0e:
key=0;kv_set_E();break;//进入东西功能键
case0x07:
key=0;kv_set_N();break;//进入南北功能键
//case0x0d:
key=0;kv_set_pc();break;//进入PC机修改
//case0x06:
key=0;kv_down_E();break;//东西减
//case0x09:
key=0;kv_up_N();break;//南北加
//case0x05:
key=0;kv_down_N();break;//南北减
//case0x07:
key=0;Kv_Enter();break;//确定键
default:
break;
}
}
if(type==0)
{
smec(150);
Display2();
}
}
voidkv_set_E()
{
//uchartime2;
//inttt,tt2;
intn1=0,n2=0;
unsignedcharkey_s,key_d;
key_s=P0;
key_d=P2;
P2=0x0ff;
//time2=G_E;
//t=time/10;
//t2=(int)time%10;
while
(1)//k4为确定键
{
if(k4==0)
{
smec(150);
k4=1;
//P2=0x0ff;
//P0=0x0ff;
break;
}
//time=G_E;
//tt=time2/10;
//tt2=(int)time2%10;
//P0=led_seg_code[tt2];
//s2=0;
//s2=1;
//P0=led_seg_code[tt];
//s1=0;
//s1=1;
if(k2==0)
{
smec(150);
++G_E;//东西加
//time2=G_E;
++R_N;
n1++;
//t=time/10;
//t2=(int)time%10;
s1=0;
P0=led_seg_code[n1];
//s2=0;
//s2=1;
//P0=led_seg_code[t];
//s1=0;
//s1=1;
k2=1;
}
if(k3==0)
{
smec(150);
--G_E;//东西减
//time2=G_E;
--R_N;
n2++;
s1=0;
P0=led_seg_code[n2];
k3=1;
}
}
//P0=key_s;
//P2=key_d;
}
voidkv_set_N()
{
//uchartime2;
//inttt,tt2;
intn1=0,n2=0;
unsignedcharkey_s,key_d;
key_s=P0;
key_d=P2;
P2=0x0ff;
//time2=G_E;
while
(1)//k1为确定键
{
if(k1==0)
{
smec(150);
k1=1;
//P2=0x0ff;
//P0=0x0ff;
break;
}
//tt=time2/10;
//tt2=(int)time2%10;
//P0=led_seg_code[tt2];
//s2=0;
//s2=1;
//P0=led_seg_code[tt];
//s1=0;
//s1=1;
if(k2==0)
{
smec(150);
++G_N;//南北加
++R_E;
//time2=G_N;
n1++;
s1=0;
P0=led_seg_code[n1];
k2=1;
}
if(k3==0)
{
smec(150);
--G_N;//南北减
--R_E;
//time2=G_N;
n2++;
s1=0;
P0=led_seg_code[n2];
k3=1;
}
}
//P0=key_s;
//P2=key_d;
}
//voidkv_set_pc()
//{
//inti=1;
//while(i)
//{
//
//while(RI==0)
//{
//if(k3==0)//k3键退出
//{
//smec(150);
//i=0;
//break;
//}
//}
//RI=0;
//G_E=SBUF;
//}
//}
voidDisplay2()//智能模式下显示
{
inti=10;
s1=1;s2=1;s3=1;s4=1;
while(i)
{
if(flag==1)
{
flag=0;
i--;
}
RED_E=1;YELLOW_E=1;GREEN_E=0;
RED_N=0;YELLOW_N=1;GREEN_N=1;
}
}
voidpc_EN()
{
if(flag_pc==1)
{
flag_pc=0;
G_E=temp;
R_N=G_E+4;
}
}
voidsmec(unsignedcharx)//1MS
{
unsignedintj;
while(x--)
{
for(j=0;j<80;j++);
}
}
//voidUart_sc2(ucharbyte)
//{
//SBUF=byte;
//while(!
RI);
//RI=0;
//G_E=SBUF;
//}
intmain()
{
cpuInit();//初始化
TR0=1;
while
(1)
{
Display();
key_analyse();//查询是否有键按下
pc_EN();
}
}
voidt0_time()interrupt1
{
TH0=0X3C;
TL0=0X0B0;
a++;
if(a==20)
{
a=0;
flag=1;
}
}
voidset_pc()interrupt4
{
RI=0;
temp=SBUF;
flag_pc=1;
}