日常很多算法,比如数值积分、数值微分、 PID指令都需要在固定的步长里调用,中断程序提供了一个这样的基础步长,最小步长。我们可以利用这样的基础步长完成步长的变换。
下面给大家介绍下在博途PLC里如何自动获取中断程序的中断时间值。
一、中断时间读取FC
利用这样的FC,我们可以自动读取对应中断程序的中断时间值,将时间值存储到全局变量表,之后其它数值算法都可以调用,不用一个个分别更改,提高效率降低出错概率。
2、1200PLC-SCL完整代码
FUNCTION "LCon_ReadCycleTimeS71200" : Real
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
VAR_INPUT
OBnumber : Int; // OB number; cyclic interupt
END_VAR
VAR_TEMP
tmpRetInt : Int;
tmpPhase : UDInt;
tmpStatus : Word;
tmpCycleTime : UDInt; // Cycle time in Microseconds
END_VAR
VAR CONSTANT
UNIT_ID : DWord := 16#1001_0000;
END_VAR
BEGIN
//------------------------------------------------------------------------------------------------------------------------
//SIEMENS DF FA PMA APC
//------------------------------------------------------------------------------------------------------------------------
//name : ReadCycleTimeS71200
//functionality : Cycle time identification for S7-1200 CPU
//assignment : Any
//library : LCon
//------------------------------------------------------------------------------------------------------------------------
//change log table:
//version date expert in charge changes applied
//V1.0 10.11.2016 APC ERL Creation
//V1.1 17.11.2016 APC ERL adapted to styleguide
//=========================================================================================================================
#LCon_ReadCycleTimeS71200 := 0.0;
#tmpRetInt := QRY_CINT(OB_NR := #OBnumber, CYCLE => #tmpCycleTime, PHASE => #tmpPhase, STATUS => #tmpStatus);
IF (#tmpStatus = 16#0014) THEN
IF #tmpRetInt = 0 THEN
#LCon_ReadCycleTimeS71200 := (UDINT_TO_REAL(#tmpCycleTime)) / 1000; //Conversion from microseconds to milliseconds
END_IF;
END_IF;
END_FUNCTION
3、1500PLC-SCL完整代码
FUNCTION "LCon_ReadCycleTimeS71500" : Real
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
VAR_INPUT
OBnumber : Int; // OB number; cyclic interupt or synchronous operation
END_VAR
VAR_TEMP
tmpRetInt : Int;
tmpCycleTime : UDInt; // Cycle time in Microseconds
tmpPhase : UDInt;
tmpStatus : Word;
tmpStartUpSi {InstructionName := 'SI_Startup'; LibVersion := '1.0'} : SI_Startup;
tmpTopSi {InstructionName := 'SI_SynchCycle'; LibVersion := '1.0'} : SI_SynchCycle;
tmpSiNone {InstructionName := 'SI_none'; LibVersion := '1.0'} : SI_none;
END_VAR
VAR CONSTANT
UNIT_ID : DWord := 16#1001_0000;
END_VAR
BEGIN
//------------------------------------------------------------------------------------------------------------------------
//SIEMENS DF FA PMA APC / Mumbai,India | Erlangen, Germany
//------------------------------------------------------------------------------------------------------------------------
//name : ReadCycleTimeS71500
//functionality : Cycle time identification for S7-1500 CPU
//assignment : Any
//library : LCon
//------------------------------------------------------------------------------------------------------------------------
//change log table:
//version date expert in charge changes applied
//V1.0 02.07.2012 APC GMC group Creation
//V2.0 10.11.2016 APC ERL extended to read cycle time from synchronous cycle OBs
//V2.1 17.11.2016 APC ERL adapted to styleguide
//=========================================================================================================================
#LCon_ReadCycleTimeS71500 := 0.0;
#tmpRetInt := RD_SINFO(TOP_SI => #tmpSiNone, START_UP_SI => #tmpStartUpSi);
IF #tmpSiNone.OB_Class = 30 THEN
#tmpRetInt := QRY_CINT(OB_NR := #OBnumber, CYCLE => #tmpCycleTime, PHASE => #tmpPhase, STATUS => #tmpStatus);
IF (#tmpStatus = 16#0014) THEN
IF #tmpRetInt = 0 THEN
#LCon_ReadCycleTimeS71500 := (UDINT_TO_REAL(#tmpCycleTime)) / 1000; //Conversion from microseconds to milliseconds
END_IF;
END_IF;
ELSIF #tmpSiNone.OB_Class = 61 THEN
#tmpRetInt := RD_SINFO(TOP_SI => #tmpTopSi, START_UP_SI => #tmpStartUpSi);
IF #tmpTopSi.OB_Nr = #OBnumber THEN
IF #tmpRetInt = 0 THEN
#LCon_ReadCycleTimeS71500 := DINT_TO_REAL(LTIME_TO_DINT(#tmpTopSi.SyncCycleTime)) / 1000000; // convert to milliseconds in REAL-Format
END_IF;
END_IF;
ELSE
#LCon_ReadCycleTimeS71500 := 0.0;
END_IF;
END_FUNCTION