怎么编写关于红绿灯程序代码(红绿灯的程序编辑)
c51单片机c语言交通灯的程序
Proteus仿真原理图:
程序如下:
#include reg51.h
#define uchar unsigned char
#define uint? unsigned int
uchar 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 b;//定时器中断次数
sbit? k1=P1^6;//定义5组开关
sbit? k2=P1^7;
sbit? k3=P2^7;
sbit? k4=P3^0;
sbit? k5=P3^1;
sbit Yellow_nb=P2^5;?????? //南北黄灯标志
sbit Yellow_dx=P2^2;?????? //东西黄灯标志
sbit Green_nb=P2^4;
sbit Green_dx=P2^1;
sbit Buzz=P3^7;
bit Buzzer_Indicate;
bit time=0;//灯状态循环标志
bit?? set=1;//调时方向切换键标志
uchar code table[11]={???? ? //共阴极字型码
0x3f,? //--0
0x06,? //--1
0x5b,? //--2
0x4f,? //--3
0x66,? //--4
0x6d,? //--5
0x7d,? //--6
0x07,? //--7
0x7f,? //--8
0x6f,? //--9
0x00?? //--NULL
};
//函数的声明部分
void delay(int ms);//延时子程序
void key();//按键扫描子程序
void key_to1();//键处理子程序
void key_to2();
void key_to3();
void display();//显示子程序
void logo();?? //开机LOGO
void Buzzer();
//主程序
void main()
{
TMOD=0X01;
TH0=0XD8;
TL0=0XF0;
EA=1;
ET0=1;
TR0=1;
EX0=1;
EX1=1;
logo();
P2=0Xc3;// 开始默认状态,东西绿灯,南北黄灯
sec_nb=sec_dx+5;
while(1)
{?????
key(); //调用按键扫描程序
display(); //调用显示程序
Buzzer();
}? ?
}
//函数的定义部分
void key()??? //按键扫描子程序
{
if(k1!=1)
{
delay(10);
if(k1!=1)
{
while(k1!=1)
{
key_to1();
for(n=0;n40;n++)
{ display();}
}
}
}
if(k2!=1)
{
delay(10);
if(k2!=1)
{
while(k2!=1)
{
key_to2();
for(n=0;n40;n++)
{ display();}
}
}
}
if(k3!=1)
{
TR0=1;?? //启动定时器
Buzzer_Indicate=0;
sec_nb=set_timenb;? //从中断回复,仍显示设置过的数值
sec_dx=set_timedx;
if(time==0)
{ P2=0X99;sec_nb=sec_dx+5; }
else { P2=0xC3;sec_dx=sec_nb+5; }
}?????
if(k4!=1)
{
delay(5);
if(k4!=1)
{
while(k4!=1);
set=!set;
}
}
if(k5!=1)
{
delay(5);
if(k5!=1)
{
while(k5!=1)
key_to3();
}
}
}
void display() //显示子程序
{
buf[1]=sec_dx/10; //第1位 东西秒十位
buf[2]=sec_dx%10; //第2位 东西秒个位
buf[3]=sec_nb/10; //第3位 南北秒十位
buf[0]=sec_nb%10; //第4位 南北秒个位
P1=0xff;????????? ??????????????????? ??// 初始灯为灭的
P0=0x00;
P1=0xfe;?????????? ???????????????? ??//片选LCD1
P0=table[buf[1]];
delay(1);
P1=0xff;
P0=0x00;
P1=0xfd;???????????? ???????????? ?//片选LCD2
P0=table[buf[2]];
delay(1);
P1=0xff;
P0=0x00;
P1=0Xfb;??????????? ? //片选LCD3
P0=table[buf[3]];
delay(1);
P1=0xff;
P0=0x00;
P1=0Xf7;
P0=table[buf[0]];???????????? ? //片选LCD4
delay(1);
}
void time0(void) interrupt 1 using 1? //定时中断子程序
{
b++;
if(b==19)????? ????????????????? // 定时器中断次数
{????? b=0;
sec_dx--;
sec_nb--;
if(sec_nb=5time==0)? //东西黄灯闪
{? Green_dx=0;Yellow_dx=!Yellow_dx;}???????????? ??
if(sec_dx=5time==1)? //南北黄灯闪
{? Green_nb=0;Yellow_nb=!Yellow_nb;}????????????
if(sec_dx==0sec_nb==5)
sec_dx=5;
if(sec_nb==0sec_dx==5)
sec_nb=5;
if(time==0sec_nb==0)
{ P2=0x99;time=!time;sec_nb=set_timenb;sec_dx=set_timenb+5;}
if(time==1sec_dx==0)
{P2=0Xc3;time=!time;sec_dx=set_timedx;sec_nb=set_timedx+5;}
}
}
void key_to1()??? //键盘处理子程序之+
{
TR0=0;?? ?????? //关定时器
if(set==0)
set_timenb++;??? //南北加1S
else
set_timedx++;??? //东西加1S
if(set_timenb==100)
set_timenb=1;
if(??? set_timedx==100)
set_timedx=1;?? //加到100置1
sec_nb=set_timenb ;?? ?//设置的数值赋给东西南北
sec_dx=set_timedx;
}
void key_to2()??? ?? //键盘处理子程序之-
{
TR0=0;???????? //关定时器
if(set==0)
set_timenb--;? //南北减1S
else
set_timedx--;? //东西减1S
if(set_timenb==0)
set_timenb=99;
if(??? set_timedx==0 )
set_timedx=99;?? //减到1重置99
sec_nb=set_timenb ;?? ?//设置的数值赋给东西南北
sec_dx=set_timedx;
}
void key_to3()?? //键盘处理之紧急车通行
{
TR0=0;
P2=0Xc9;
sec_dx=00;
sec_nb=00;
Buzzer_Indicate=1;
}
void int0(void) interrupt 0 using 1 ?//只允许东西通行
{
TR0=0;
P2=0Xc3;
Buzzer_Indicate=0;
sec_dx=00;
sec_nb=00;
}
void int1(void) interrupt 2 using 1 ? ?? ?//只允许南北通行
{
TR0=0;
P2=0X99;
Buzzer_Indicate=0;
sec_nb=00;
sec_dx=00;
}
void logo()//开机的Logo? "- - - -"
{ for(n=0;n50;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=0;
}
void delay(int ms)??????????? //延时子程序
{
uint j,k;
for(j=0;jms;j++)
for(k=0;k124;k++);
}
vb写一个简单红绿灯程序代码,希望能注释和思路写清楚一点,我菜鸟!谢谢
'ZT是当前状态记录,Qzt是前状态记录,SJ是时间记录
Dim ZT As Integer, Qzt As Integer, Sj As Integer
Private Sub Command1_Click()
If Command1.Caption = "开始" Then
Command1.Caption = "20停止"
Timer1.Enabled = True
Shape3.FillColor = HFF
ZT = 3
Qzt = 3
Else
Command1.Caption = "开始"
Timer1.Enabled = False
Shape1.FillColor = H4000
Shape2.FillColor = H8080
Shape3.FillColor = H40
ZT = 0
End If
End Sub
Private Sub Form_Load()
Shape1.FillStyle = 0 '绿灯
Shape2.FillStyle = 0 '黄灯
Shape3.FillStyle = 0 '红灯
Shape1.FillColor = H4000 '暗绿
Shape2.FillColor = H8080 '暗黄
Shape3.FillColor = H40 '暗红
Shape1.Shape = 3 '圆形
Shape2.Shape = 3
Shape3.Shape = 3
Command1.Caption = "开始"
Timer1.Interval = 1000 '一秒一次加入倒计时
Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
Sj = Sj + 1
If ZT = 1 Or ZT = 3 Then Command1.Caption = (20 - Sj) "停止"
If ZT = 2 Then Command1.Caption = (5 - Sj) "停止"
If Sj = 20 And (ZT = 1 Or ZT = 3) Then
Shape1.FillColor = H4000
Shape2.FillColor = H8080
Shape3.FillColor = H40
Select Case ZT
Case 1
Shape2.FillColor = HFFFF
ZT = 2
Qzt = 1
Case 3
Shape2.FillColor = HFFFF
ZT = 2
Qzt = 3
End Select
Sj = 0
End If
If Sj = 5 And ZT = 2 Then
Shape1.FillColor = H4000
Shape2.FillColor = H8080
Shape3.FillColor = H40
If Qzt = 3 Then
Shape1.FillColor = HFF00
ZT = 1
Else
Shape3.FillColor = HFF
ZT = 3
End If
Sj = 0
End If
End Sub
红绿灯的plc简单编程是什么?
PLC的编程不同于电脑中软件编程,它需要对具体的输入/输出端口信号进行处理和操作(或者说对一个字节的每一个 bit 进行处理和操作)。
南北方向:
南北绿(8s),东西红(8s)
南北黄(2.1s),东西红(2.1s)
南北红(10.1s),东西绿(8s),东西黄(2.1s)
东西方向:
东西红(10.1s),南北绿(8s),南北黄(2.1s)
东西绿(8s),南北红(8s)
东西黄(2.1s),南北红(2.1s)
中央处理单元
中央处理器(CPU)是PLC的控制中枢,也是PLC的核心部件,其性能决定了PLC的性能。中央处理器由控制器、运算器和寄存器组成,这些电路都集中在一块芯片上,通过地址总线、控制总线与存储器的输入/输出接口电路相连。中央处理器的作用是处理和运行用户程序,进行逻辑和数学运算,控制整个系统使之协调。
用VB编写“红绿灯”程序
Dim s As Integer
Dim n As Integer
'单击“红灯”按钮
Private Sub Command1_Click()
Shape6.Visible = True : Shape7.Visible = False : Shape8.Visible = False
End Sub
'单击“黄灯”按钮
Private Sub Command2_Click()
Shape6.Visible = False : Shape7.Visible = True : Shape8.Visible = False
End Sub
'单击“绿灯”按钮
Private Sub Command3_Click()
Shape6.Visible = False : Shape7.Visible = False : Shape8.Visible = True
End Sub
'单击“自动”按钮
Private Sub Command4_Click()
Timer1.Enabled = True
Command1.Enabled = False
Command2.Enabled = False
Command3.Enabled = False
n = 0 : s = 0
End Sub
'单击“手动”按钮
Private Sub Command5_Click()
Timer1.Enabled = False
Label1.Caption = ""
Shape6.Visible = False
Shape7.Visible = False
Shape8.Visible = False
Command1.Enabled = True
Command2.Enabled = True
Command3.Enabled = True
End Sub
'时钟控件启动
Private Sub Timer1_Timer()
Select Case n
Case 0
Shape6.Visible = True
Shape7.Visible = False
Shape8.Visible = False
Label1.Caption = 3 - s
s = s + 1
If s 3 Then
n = 1
s = 0
End If
Case 1
Shape6.Visible = False
Shape7.Visible = True
Shape8.Visible = False
n = 2
Case 2
Shape6.Visible = False
Shape7.Visible = False
Shape8.Visible = True
Label1.Caption = 3 - s
s = s + 1
If s 3 Then
n = 0 : s = 0
End If
End Select
End Sub
JS中制作红绿灯的代码?
只能修改页面统一代码中的颜色设置: a:hover { color: #FFFFFF; text-decoration: none; } a:visited { color: #fff; text-decoration: none; }
关于单片机简单的红绿灯程序问题
代码修改如下,经keil编译,proteus仿真结果为 P1.0亮10秒,然后P1.1亮2秒,然后P1.2亮10秒,最后P1.0亮10秒,,,
#include?reg51.h
sbit?R=P1^0;//HONG
sbit?W=P1^1;//HUANG
sbit?L=P1^2;//LV
unsigned?int?a,b=0;
//unsigned?char?code?c[]={0xfe,0xfc,0xfb};//1110,1100,1011
unsigned?char?code?c[]={0xfe,0xfD,0xfb,0xfd};//1110,1101,1011,1101//红?黄?绿?黄//修正黄灯的值,增加一个状态
void?timer10s?()?interrupt?1
{
?if?(a2000)
????{
????????TH0=0xec;//5ms
????????TL0=0x78;
????????a++;
??//??????b=0;//这里不能对b赋值
????????TR0=1;
????????TR1=0;//【增加】
????}
????if?(a=2000)
????{
????????TH0=0xec;
????????TL0=0x78;
????????a=0;
????????b++;//b=1;
????????if(b==4)?b=0;//【增加】
????????TR1=1;
????????TR0=0;//【增加】
????}
}
void?timer2s?()interrupt?3
{
???
???if?(a400)
????{
????????TH1=0xec;
????????TL1=0x78;
????????a++;
????????TR1=1;
????????TR0=0;
????}
????if?(a=400)
????{
????????TH1=0xec;
????????TL1=0x78;
????????a=0;
????????b++;//b=2;
????????//
????????if(b==4)?b=0;//【增加】
????????TR0=1;
????????TR1=0;//【增加】
????}
}
void?main?()
{
????EA=1;
????ET0=1;
????ET1=1;
????TMOD=0X11;
????TH0=0xec;//5MS
????TL0=0x78;
????TH1=0xec;
????TL1=0x78;
????TR0=1;
????TR1=0;
????P1=c[0];
????b=0;////【增加】
????while?(1)
????{
????????switch?(b)
????????{
????????????case?0x00:P1=c[0];break;
????????????case?0x01:P1=c[1];break;
????????????case?0x02:P1=c[2];break;
????????????case?0x03:P1=c[3];break;//增加一个状态
????????}
????}
}
以上代码可以优化,如下:
#include?reg51.h
sbit?R=P1^0;//HONG
sbit?W=P1^1;//HUANG
sbit?L=P1^2;//LV
unsigned?int?a,b=0;
unsigned?char?code?c[]={0xfe,0xfD,0xfb,0xfd};
void?timer10s?()?interrupt?1
{
????TH0=0xec;//5ms
????TL0=0x78;
????a++;
????if(TR1==1)?TR1=0;
????if?(a=2000)
????{
????????a=0;
????????b++;
????????if(b==4)?b=0;
????????TR1=1;
????????TR0=0;
????}
}
void?timer2s?()interrupt?3
{
????TH1=0xec;
????TL1=0x78;
????a++;?
????if(TR0==1)?TR0=0;
????if?(a=400)
????{
????????a=0;
????????b++;
????????if(b==4)?b=0;
????????TR0=1;
????????TR1=0;
????}
}
void?main?()
{
????EA=1;
????ET0=1;
????ET1=1;
????TMOD=0X11;
????TH0=0xec;//5MS
????TL0=0x78;
????TH1=0xec;
????TL1=0x78;
????TR0=1;
????TR1=0;
????a=0;//
????b=0;//
????while?(1)
????{
????????switch?(b)
????????{
????????????case?0x00:P1=c[0];break;
????????????case?0x01:P1=c[1];break;
????????????case?0x02:P1=c[2];break;
????????????case?0x03:P1=c[3];break;
????????}
????}
}