文章目录
- 前言
- 一、功能概述
- 二、单输出电磁阀程序编写
- 1.创建自定义数据类型
- 2.创建FB功能块“单输出电磁阀”
- 3.编写程序
- 三、双输出电磁阀程序编写
- 1.创建自定义数据类型
- 2.创建FB功能块“双输出电磁阀”
- 3.编写程序
前言
本文分享一个自己编写的电磁阀控制逻辑块。
一、功能概述
- 手动状态、自动状态、机旁状态、强制状态、检修状态自由切换;
- 具有开阀超时、关阀超时报警功能;
- 手动状态下,按开阀按钮开阀,按关阀按钮关阀;
- 自动状态下,根据程序联锁要求自动开阀关阀;
- 强制状态下,跳过允许开阀关阀条件,直接开阀或关阀;
- 检修状态下,自动关阀,且按下开阀按钮不允许开阀。
二、单输出电磁阀程序编写
1.创建自定义数据类型
2.创建FB功能块“单输出电磁阀”
建立如下引脚变量:
3.编写程序
代码如下:
1.赋值
#HMI.Ready_DI := #Ready_DI;
#HMI.Locally_DI := #Locally_DI;
#HMI.Open_DI := #Open_DI;
#HMI.Close_DI := #Close_DI;
#Open_DO := #HMI.Open_DO;
2.时间转换、启动超时判断、故障复位
#HMI.#Fault_ALL := #Fault OR #HMI.#Open_Time_Out OR #HMI.#Close_Time_Out OR #HMI.#Limit_Fault; //故障赋值
#HMI.#Lcl_DO := #Locally_DI AND #HMI.#HMI_Lcl; //有就地信号,且按下机旁允许按钮,才会显示机旁允许状态#Open_Time := DINT_TO_TIME(REAL_TO_DINT(#HMI.#Open_T_Set * 1000.0)); //输入1表示1S
#Close_Time := DINT_TO_TIME(REAL_TO_DINT(#HMI.#Close_T_Set * 1000.0));#Open_IEC(IN := #Open_DO AND NOT #HMI.#HMI_Close AND NOT #Open_DI AND NOT #HMI.#Limit_Fault,PT := #Open_Time); //开阀延时
#Close_IEC(IN := NOT #Open_DO AND NOT #HMI.#HMI_Open AND NOT #Close_DI AND NOT #HMI.#Limit_Fault,PT := #Close_Time); //关阀延时
#Limit_IEC(IN := #Open_DI AND #Close_DI,PT := #Limit_Time); //限位超时IF #Open_IEC.Q THEN#HMI.#Open_Time_Out := 1;#HMI.#Open_DO := 0;
END_IF; //开阀超时输出
IF #Close_IEC.Q THEN#HMI.#Close_Time_Out := 1;#HMI.#Open_DO := 0;
END_IF; //关阀超时输出
IF #Limit_IEC.Q THEN#HMI.#Limit_Fault := 1;#HMI.#Open_DO := 0;
END_IF; //限位故障输出IF #HMI.#HMI_Rst THEN#HMI.#Fault_ALL := 0;#HMI.#Limit_Fault := 0; //复位限位故障输出#HMI.#Open_Time_Out := 0; //复位开阀超时输出#HMI.#Close_Time_Out := 0; //复位关阀超时输出
END_IF;
3.检测信号上升沿
#P_HMI_Lcl(CLK := #HMI.#HMI_Lcl);
#P_HMI_EmgManual(CLK := #HMI.#HMI_EmgManual);
#P_HMI_Pause(CLK := #HMI.#HMI_Pause);
4.手动状态显示
IF #HMI.#HMI_Manual = 1 AND NOT #HMI.#HMI_Auto AND NOT #HMI.#HMI_EmgManual AND NOT #HMI.#HMI_Pause AND NOT #HMI.#Lcl_DO THEN#HMI.#Manual_DO := 1;#HMI.#Auto_DO := 0;#HMI.#EmgManual_DO := 0;
END_IF;
//默认手动状态
IF NOT #HMI.#Auto_DO AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO AND NOT #HMI.#EmgManual_DO THEN#HMI.#Manual_DO := 1;#HMI.#Auto_DO := 0;#HMI.#EmgManual_DO := 0;
END_IF;
5.自动状态显示
IF #HMI.#HMI_Auto = 1 AND NOT #HMI.#HMI_Manual AND NOT #HMI.#HMI_EmgManual AND NOT #HMI.#HMI_Pause AND NOT #HMI.#Lcl_DO THEN#HMI.#Manual_DO := 0;#HMI.#Auto_DO := 1;#HMI.#EmgManual_DO := 0;
END_IF;
6.强制状态显示
IF #HMI.#HMI_EmgManual = 1 AND NOT #HMI.#HMI_Manual AND NOT #HMI.#HMI_Auto AND NOT #HMI.#HMI_Pause AND NOT #HMI.#Lcl_DO THEN#HMI.#Manual_DO := 0;#HMI.#Auto_DO := 0;#HMI.#EmgManual_DO := 1;#HMI.#HMI_Pause := 0;
END_IF;
IF #HMI.#HMI_EmgManual = 0 THEN#HMI.#EmgManual_DO := 0;
END_IF;
7.休止状态显示
IF #HMI.#HMI_Pause = 1 AND NOT #HMI.#HMI_Manual AND NOT #HMI.#HMI_Auto AND NOT #HMI.#HMI_EmgManual AND NOT #HMI.#Lcl_DO THEN#HMI.#Manual_DO := 0;#HMI.#Auto_DO := 0;#HMI.#EmgManual_DO := 0;#HMI.#Pause_DO := 1;#HMI.#HMI_EmgManual := 0;
END_IF;
IF #HMI.#HMI_Pause = 0 THEN#HMI.#Pause_DO := 0;
END_IF;
8.手动状态下开阀、关阀
IF #Ready_DI AND #HMI.#HMI_Open AND #HMI.#Manual_DO AND NOT #HMI.#HMI_Close AND NOT #HMI.#Limit_Fault AND NOT #HMI.#Open_Time_Out AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO AND NOT #Fault THEN#HMI.#Open_DO := 1;
END_IF;
IF #Ready_DI AND #HMI.#HMI_Close AND #HMI.#Manual_DO AND NOT #HMI.#HMI_Open AND NOT #HMI.#Limit_Fault AND NOT #HMI.#Close_Time_Out AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO AND NOT #Fault THEN#HMI.#Open_DO := 0;
END_IF;
9.自动状态下开阀、关阀
IF #Ready_DI AND #Auto_Open AND #HMI.#Auto_DO AND NOT #HMI.#HMI_Close AND NOT #HMI.#Limit_Fault AND NOT #HMI.#Open_Time_Out AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO AND NOT #Fault THEN#HMI.#Open_DO := 1;
END_IF;
IF #Ready_DI AND #Auto_Close AND #HMI.#Auto_DO AND NOT #HMI.#HMI_Open AND NOT #HMI.#Limit_Fault AND NOT #HMI.#Close_Time_Out AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO AND NOT #Fault THEN#HMI.#Open_DO := 0;
END_IF;
10.强制状态下开阀、关阀
IF #HMI.#EmgManual_DO AND #HMI.#HMI_Open AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO AND NOT #HMI.#Limit_Fault AND NOT #HMI.#Open_Time_Out AND NOT #Fault THEN#HMI.#Open_DO := 1;
END_IF;
IF #HMI.#EmgManual_DO AND #HMI.#HMI_Close AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO AND NOT #HMI.#Limit_Fault AND NOT #HMI.#Close_Time_Out AND NOT #Fault THEN#HMI.#Open_DO := 0;
END_IF;
11.休止状态下关阀
IF #HMI.#Pause_DO AND NOT #HMI.#HMI_Lcl AND NOT #HMI.#Lcl_DO THEN#HMI.#Open_DO := 0;
END_IF;
12.按钮互锁
IF #HMI.#HMI_Lcl AND #P_HMI_EmgManual.Q THEN#HMI.#HMI_EmgManual := 0;
END_IF;
IF #HMI.#HMI_Lcl AND #P_HMI_Pause.Q THEN#HMI.#HMI_Pause := 0;
END_IF;IF #HMI.#HMI_EmgManual AND #P_HMI_Lcl.Q THEN#HMI.#HMI_Lcl := 0;
END_IF;
IF #HMI.#HMI_EmgManual AND #P_HMI_Pause.Q THEN#HMI.#HMI_Pause := 0;
END_IF;IF #HMI.#HMI_Pause AND #P_HMI_Lcl.Q THEN#HMI.#HMI_Lcl := 0;
END_IF;
IF #HMI.#HMI_Pause AND #P_HMI_EmgManual.Q THEN#HMI.#HMI_EmgManual := 0;
END_IF;
13.按钮自复位
IF NOT "AlwaysFALSE" THEN#HMI.#HMI_Open := 0;#HMI.#HMI_Close := 0;#HMI.#HMI_Manual := 0;#HMI.#HMI_Auto := 0;#HMI.#HMI_Rst := 0;
END_IF;
三、双输出电磁阀程序编写
1.创建自定义数据类型
同单输出电磁阀。
2.创建FB功能块“双输出电磁阀”
建立如下引脚变量:
3.编写程序
代码如下:
1.赋值
#HMI.Ready_DI := #Ready_DI;
#HMI.Locally_DI := #Locally_DI;
#HMI.Open_DI := #Open_DI;
#HMI.Close_DI := #Close_DI;
#Open_DO := #HMI.Open_DO;
#Close_DO := #HMI.Close_DO;
2.时间转换、启动超时判断、故障复位
#HMI.#Fault_ALL := #Fault OR #HMI.#Open_Time_Out OR #HMI.#Close_Time_Out OR #HMI.#Limit_Fault; //故障赋值
#HMI.#Lcl_DO := #Locally_DI AND #HMI.#HMI_Lcl; //有就地信号,且按下机旁允许按钮,才会显示机旁允许状态#Open_Time := DINT_TO_TIME(REAL_TO_DINT(#HMI.#Open_T_Set * 1000.0)); //输入1表示1S
#Close_Time := DINT_TO_TIME(REAL_TO_DINT(#HMI.#Close_T_Set * 1000.0));#Open_IEC(IN := #HMI.#Open_DO AND NOT #HMI.#HMI_Close AND NOT #HMI.#Open_DI AND NOT #HMI.#Limit_Fault,PT := #Open_Time); //开阀延时
#Close_IEC(IN := #HMI.#Close_DO AND NOT #HMI.#HMI_Open AND NOT #HMI.#Close_DI AND NOT #HMI.#Limit_Fault,PT := #Close_Time); //关阀延时
#Limit_IEC(IN:=#HMI.#Open_DI AND #HMI.#Close_DI,PT:=#Limit_Time); //限位超时IF #Open_IEC.Q THEN#HMI.#Open_Time_Out := 1;#HMI.#Open_DO := 0;
END_IF; //开阀超时输出
IF #Close_IEC.Q THEN#HMI.#Close_Time_Out := 1;#HMI.#Close_DO := 0;
END_IF; //关阀超时输出
IF #Limit_IEC.Q THEN#HMI.#Limit_Fault := 1;#HMI.#Open_DO := 0;#HMI.#Close_DO := 0;
END_IF; //限位故障输出IF #HMI.#HMI_Rst THEN#HMI.#Fault_ALL := 0;#HMI.#Limit_Fault := 0; //复位限位故障输出#HMI.#Open_Time_Out := 0; //复位开阀超时输出#HMI.#Close_Time_Out := 0; //复位关阀超时输出
END_IF;
3.检测信号上升沿
#P_HMI_Lcl(CLK := #HMI.#HMI_Lcl);
#P_HMI_EmgManual(CLK := #HMI.#HMI_EmgManual);
#P_HMI_Pause(CLK := #HMI.#HMI_Pause);
4.手动状态显示
IF #HMI.#HMI_Manual = 1 AND NOT #HMI.#HMI_Auto AND NOT #HMI.#HMI_EmgManual AND NOT #HMI.#HMI_Pause AND NOT #HMI.#Lcl_DO THEN#HMI.#Manual_DO := 1;#HMI.#Auto_DO := 0;#HMI.#EmgManual_DO := 0;
END_IF;
//默认手动状态
IF NOT #HMI.#Auto_DO AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO AND NOT #HMI.#EmgManual_DO THEN#HMI.#Manual_DO := 1;#HMI.#Auto_DO := 0;#HMI.#EmgManual_DO := 0;
END_IF;
5.自动状态显示
IF #HMI.#HMI_Auto = 1 AND NOT #HMI.#HMI_Manual AND NOT #HMI.#HMI_EmgManual AND NOT #HMI.#HMI_Pause AND NOT #HMI.#Lcl_DO THEN#HMI.#Manual_DO := 0;#HMI.#Auto_DO := 1;#HMI.#EmgManual_DO := 0;
END_IF;
6.强制状态显示
IF #HMI.#HMI_EmgManual = 1 AND NOT #HMI.#HMI_Manual AND NOT #HMI.#HMI_Auto AND NOT #HMI.#HMI_Pause AND NOT #HMI.#Lcl_DO THEN#HMI.#Manual_DO := 0;#HMI.#Auto_DO := 0;#HMI.#EmgManual_DO := 1;#HMI.#HMI_Pause := 0;
END_IF;
IF #HMI.#HMI_EmgManual = 0 THEN#HMI.#EmgManual_DO := 0;
END_IF;
7.休止状态显示
IF #HMI.#HMI_Pause = 1 AND NOT #HMI.#HMI_Manual AND NOT #HMI.#HMI_Auto AND NOT #HMI.#HMI_EmgManual AND NOT #HMI.#Lcl_DO THEN#HMI.#Manual_DO := 0;#HMI.#Auto_DO := 0;#HMI.#EmgManual_DO := 0;#HMI.#Pause_DO := 1;#HMI.#HMI_EmgManual := 0;
END_IF;
IF #HMI.#HMI_Pause = 0 THEN#HMI.#Pause_DO := 0;
END_IF;
8.手动状态下开阀、关阀
IF #Ready_DI AND #HMI.#HMI_Open AND #HMI.#Manual_DO AND NOT #HMI.#HMI_Close AND NOT #HMI.#Limit_Fault AND NOT #HMI.#Open_Time_Out AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO AND NOT #Fault THEN#HMI.#Open_DO := 1;#HMI.#Close_DO := 0;
END_IF;
IF #Ready_DI AND #HMI.#HMI_Close AND #HMI.#Manual_DO AND NOT #HMI.#HMI_Open AND NOT #HMI.#Limit_Fault AND NOT #HMI.#Close_Time_Out AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO AND NOT #Fault THEN#HMI.#Open_DO := 0;#HMI.#Close_DO := 1;
END_IF;
9.自动状态下开阀、关阀
IF #Ready_DI AND #Auto_Open AND #HMI.#Auto_DO AND NOT #HMI.#HMI_Close AND NOT #HMI.#Limit_Fault AND NOT #HMI.#Open_Time_Out AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO AND NOT #Fault THEN#HMI.#Open_DO := 1;#HMI.#Close_DO := 0;
END_IF;
IF #Ready_DI AND #Auto_Close AND #HMI.#Auto_DO AND NOT #HMI.#HMI_Open AND NOT #HMI.#Limit_Fault AND NOT #HMI.#Close_Time_Out AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO AND NOT #Fault THEN#HMI.#Open_DO := 0;#HMI.#Close_DO := 1;
END_IF;
10.强制状态下开阀、关阀
IF #HMI.#EmgManual_DO AND #HMI.#HMI_Open AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO AND NOT #HMI.#Limit_Fault AND NOT #HMI.#Open_Time_Out AND NOT #Fault THEN#HMI.#Open_DO := 1;#HMI.#Close_DO := 0;
END_IF;
IF #HMI.#EmgManual_DO AND #HMI.#HMI_Close AND NOT #HMI.#Pause_DO AND NOT #HMI.#Lcl_DO AND NOT #HMI.#Limit_Fault AND NOT #HMI.#Close_Time_Out AND NOT #Fault THEN#HMI.#Open_DO := 0;#HMI.#Close_DO := 1;
END_IF;
11.休止状态下关阀
IF #HMI.#Pause_DO AND NOT #HMI.#HMI_Lcl AND NOT #HMI.#Lcl_DO THEN#HMI.#Open_DO := 0;#HMI.#Close_DO := 1;
END_IF;
12.开关到位后,开阀关阀不再输出
IF #HMI.#Open_DI THEN#HMI.#Open_DO := 0;
END_IF;
IF #HMI.#Close_DI THEN#HMI.#Close_DO := 0;
END_IF;
13.按钮互锁
IF #HMI.#HMI_Lcl AND #P_HMI_EmgManual.Q THEN#HMI.#HMI_EmgManual := 0;
END_IF;
IF #HMI.#HMI_Lcl AND #P_HMI_Pause.Q THEN#HMI.#HMI_Pause := 0;
END_IF;IF #HMI.#HMI_EmgManual AND #P_HMI_Lcl.Q THEN#HMI.#HMI_Lcl := 0;
END_IF;
IF #HMI.#HMI_EmgManual AND #P_HMI_Pause.Q THEN#HMI.#HMI_Pause := 0;
END_IF;IF #HMI.#HMI_Pause AND #P_HMI_Lcl.Q THEN#HMI.#HMI_Lcl := 0;
END_IF;
IF #HMI.#HMI_Pause AND #P_HMI_EmgManual.Q THEN#HMI.#HMI_EmgManual := 0;
END_IF;
14.按钮自复位
IF NOT "AlwaysFALSE" THEN#HMI.#HMI_Start := 0;#HMI.#HMI_Stop := 0;#HMI.#HMI_Manual := 0;#HMI.#HMI_Auto := 0;#HMI.#HMI_Rst := 0;
END_IF;