TwinCAT3 编写红绿灯程序
程序员文章站
2022-06-09 16:33:41
...
TwinCAT3 编写红绿灯程序
1.制作红绿灯,实现十字路口红绿灯基本功能,红绿灯显示,倒计时显示。
主要用到:定时器,界面制作。
2.下面展示 红绿灯程序
。
PROGRAM MAIN
VAR
red_green1:red_green; // 实例化功能块1
END_VAR
//主程序部分
red_green();
//red_green 功能块变量申明
FUNCTION_BLOCK red_green
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR
wait:TON;
flash1:TON;
flash2:TON;
count:INT;
count_down:TIME;
S_N_red:BOOL;
S_N_yellow:BOOL;
S_N_green:BOOL;
E_W_red:BOOL;
E_W_yellow:BOOL;
E_W_green:BOOL;
str1:STRING;
i: INT;
S_N_redshow: INT;
E_W_greenshow: INT;
E_W_yellowshow:INT;
E_W_redshow:INT;
S_N_greenshow:INT;
S_N_yellowshow:INT;
END_VAR
// red_green控制程序
CASE count OF
0: //init
count:=count+1;
1: //red
str1:='hello word';
S_N_red:= TRUE; //南北红
E_W_green:=TRUE; //东西绿
S_N_yellow:= FALSE;
S_N_green:= FALSE;
E_W_red:=FALSE;
E_W_yellow:=FALSE;
wait(IN:=TRUE , PT:=T#20S , Q=> , ET=> );
S_N_redshow := 25 - TIME_TO_INT(wait.ET)/1000; //转换从秒变为毫秒,南北红显示
E_W_greenshow:=20 - TIME_TO_INT(wait.ET)/1000; //东西绿倒计时显示
IF wait.Q THEN
wait(IN:=FALSE);
count:=count+1;
END_IF
2: //red and yellow
S_N_red:= TRUE; //南北红
E_W_green:=FALSE; //东西绿 灭
//E_W_yellow:=TRUE; //东西黄 亮
flash1(IN:=NOT flash2.Q , PT:=T#250MS , Q=> , ET=> );
flash2(IN:=flash1.Q , PT:=T#250MS , Q=> , ET=> );
E_W_yellow := flash1.Q;
E_W_red:=FALSE;
S_N_yellow:= FALSE;
S_N_green:=FALSE;
wait(IN:=TRUE , PT:=T#5S , Q=> , ET=> );
S_N_redshow := 5 - TIME_TO_INT(wait.ET)/1000;
E_W_yellowshow:=5-TIME_TO_INT(wait.ET)/1000;
IF wait.Q THEN
wait(IN:=FALSE);
count:=count+1;
END_IF
3: //Green
E_W_red:=TRUE; //东西红
S_N_green:= TRUE; //南北绿
S_N_red:=FALSE;
S_N_yellow:=FALSE;
E_W_yellow:=FALSE;
E_W_green:=FALSE;
wait(IN:=TRUE , PT:=T#20S , Q=> , ET=> );
E_W_redshow:=25-TIME_TO_INT(wait.ET)/1000;
S_N_greenshow:=20-TIME_TO_INT(wait.ET)/1000;
IF wait.Q THEN
wait(IN:=FALSE);
count:=count+1;
END_IF
4:
E_W_red:=TRUE; //东西红
flash1(IN:=NOT flash2.Q , PT:=T#250MS , Q=> , ET=> );
flash2(IN:=flash1.Q , PT:=T#250MS , Q=> , ET=> );
S_N_yellow := flash1.Q;
S_N_green:= FALSE; //南北绿 灭
S_N_red:=FALSE;
E_W_yellow:=FALSE;
E_W_green:=FALSE;
wait(IN:=TRUE , PT:=T#5S , Q=> , ET=> );
E_W_redshow:=5-TIME_TO_INT(wait.ET)/1000;
S_N_yellowshow:=5-TIME_TO_INT(wait.ET)/1000;
IF wait.Q THEN
wait(IN:=FALSE);
count:=count+1;
END_IF
5:
count:=0;
END_CASE
3.每个红绿灯计时器显示用后缀为show的变量,用Rectangle窗口来显示计时,(SN、EW为南北,东西缩写)。
4.插入一张图片置底层,电脑上虚拟端口仿真,最终实现红绿灯闪烁。
上一篇: 关于电路中常见地的区分
下一篇: 运放注意要点