Ladder Programming
Timers
Several timer commands available
• More information can be found in NJ Series Instruction
Manual
• Timer section of Toolbox
• Function Blocks
º TON
º TOF
º TP
º AccumulationTimer
• Functions
º TIMER
Timers
TON, TOF, TP use TIME data type for preset
• Constant or variable
• Maximum value
º 106,571 days
º 23 hours
º 47 minutes
º 16 seconds
º 854.775807 milliseconds
• Accurate to 100ns
Timers
On Delay Timer
• Function block instance must be defined
• When IN is energized, timer begins to time up from
zero to preset value PT
• When current value reaches preset, output Q active
• Current elapsed time available at ET
• When IN signal is removed
º Output turns off
º Elapsed time reset to zero
Timers
On Delay Timer
• Example
Timers
Off Delay Timer
• Function block instance must be defined
• When input In turns on
º Output Q turns on
º ET of timer is reset to zero
• Timing begins when In turns off
• Timer times up from zero to preset
• Output Q turns off when preset reached
Timers
Off Delay Timer
• Example
Timers
Pulse Timer
• Create a one-shot pulse
• Input In turns on
º Output Q turns on
º Timer begins to time
• When preset value is reached, output Q turns off
• Output is always same length, regardless of input
length
Timers
Pulse Timer
• Example
Timers
Accumulation Timer
• Different than other timers discussed
• Retentive, does not automatically reset
• Must use Reset input to reset elapsed time value
• Can be useful as a stopwatch or to time how long a
condition is true
Timers
Accumulation Timer
• A preset can be defined on PT
• The output Q will turn on if this preset value is
reached
• Each time In turns on, the elapsed time ET is
updated as long as In stays on
• ET will continue to accumulate each time In
turns on
• When In turns off, the value in ET is held
• The value is held until reset with the Reset input
Timers
Accumulation Timer
• Example
Timers
TIME Data Type
• Many operator interfaces and OPC products do not
have support for this IEC data type
• May be required to enter a value in a different format
(such as LINT) and convert it to TIME type
º Nanoseconds
‒ NanoSecToTime
º Milliseconds
‒ Multiply by 1,000,000, NanoSecToTime
º Seconds
‒ SecToTime
Timers
TIME Data Type
• To convert TIME data type to integer, use:
º TimeToSec
º TimeToNanoSec
• Example conversion
Timers
Timer
• This function does NOT use the TIME data type
• Preset is integer representing 100ms increments
º 10 x 100ms = 1s
• Times DOWN from preset to zero
• TimerDat is IN/OUT variable
º MUST be declared as type _sTimer
Timers
Timer
• Example
EXERCISE
Counters
Several counter commands available
• More information can be found in NJ Series Instruction
Manual
• Counter section of Toolbox
• Function Blocks
º CTD
º CTU
º CTUD
º All INT based, max value 32,767
Counters
Counters with higher count capabilities
• CTD_DINT
• CTD_LINT
• CTD_UDINT
• CTD_ULINT
Same 4 options for CTU and CTUD
See table in Lesson 2 for value limits of these data
types
Counters
Count Down
• Counts down from preset value to zero
• CD arrow means rising edge signal
• Load input sets counter to preset value
• PV indicates preset value
º Constant number or variable
• Q turns on when counter reaches zero
• CV is current value of counter
Counters
Count Down
Counters
Count Down
• Counter does NOT count while LOAD is on
• Counter does NOT count below zero
Counters
Count Up
• Counts up from zero to preset value
• CU input causes count up
• Reset input resets counter to zero
• other values are as previously discussed
Counters
Count Up
Counters
Bi-Directional Counter
• Counts both up and down
• Combines functions of CTU and CTD
• Signal names as previously discussed
• QU indicates count equal to preset
• QD indicates count equal to zero
º Counted down to zero or immediately after reset
Counters
Bi-Directional Counter
EXERCISE
Data Movement
Variety of functions in Data Movement section of
Toolbox window
Move bits, digits, words, text strings, and more
MOVE can move any data type
• Including structures, arrays, enumerations
• Source and destination should be same type
Data Movement
MOVE function
• Data can be different data types as long as in the same
group, as shown here
º BYTE, WORD, DWORD, LWORD
º USINT, UINT, UDINT, ULINT, SINT, INT, DINT, LINT,
REAL, LREAL
• Copies data from In to Out
º Input can be a variable or a constant
º Output must be a variable
• Executes every scan while EN is on
Mathematics
Functions for basic math
Take inputs in multiple formats
Result should always be format of largest source
• Using 2 INT results in INT
• INT and DINT results in DINT
• Any input REAL, output is REAL
Mathematics
Changing number of inputs
• Change from 2 up to 5 inputs
• Make sure any added inputs are connected or a
compile error will result
• Available with addition or multiplication
• Inputs can be constants or variables
Mathematics
Math Commands
• ADD or +
• SUB or –
• MUL or *
• DIV or /
Mathematics
Overflow detection
• Addition, subtraction and multiplication only
• If output data is too large for data type, system flag
P_Cy (carry flag) is turned on
º AddOU or +OU
º SubOU or –OU
‒ Cannot be expanded above 2 inputs
º MulOU or *OU
Add or + can also be used to combine text strings
Mathematics
Increment and decrement
• Usable on any integer data type
• Raise or lower a value by 1
Comparison
6 comparison functions
Available by name or by symbol
• > or GT
• < or LT
• = or EQ
• <E or LE
• >E or GE
• <> or NE
Comparison
All except Not Equal can be used on 2 to 5 inputs
• Sequence must be followed as shown below
Comparison
All data does not need to be same data type
• Data types will expand as discussed for math
• Compare INT and REAL is valid
Some comparisons which are NOT allowed:
• Bit strings to integers or real numbers
• Signed integer data and unsigned integer data
• Data must be same type for TIME, DATE, TOD,
DT, or STRING
Comparison
ZoneCmp checks a value against a range
• Inputs for minimum, maximum, and data
• Single output when in range
• Available for all numeric data types
EXERCISE
Data Type Conversion
Some commands require data in a specific format
Series of functions to convert from one type to
another type
• Found in the Data Type Conversion group in
toolbox
• Sometimes multiple commands offer similar
functionality
Data Type Conversion
Table below shows 14 commands to convert to
REAL and 14 commands to convert to LREAL
INT_TO_REAL INT_TO_LREAL
DINT_TO_REAL DINT_TO_LREAL
LINT_TO_REAL LINT_TO_LREAL
SINT_TO_REAL SINT_TO_LREAL
UINT_TO_REAL UINT_TO_LREAL
UDINT_TO_REAL UDINT_TO_LREAL
ULINT_TO_REAL ULINT_TO_LREAL
USINT_TO_REAL USINT_TO_LREAL
LREAL_TO_REAL REAL_TO_LREAL
STRING_TO_REAL STRING_TO_LREAL
BYTE_TO_REAL BYTE_TO_LREAL
WORD_TO_REAL WORD_TO_LREAL
DWORD_TO_REAL DWORD_TO_LREAL
LWORD_TO_REAL LWORD_TO_LREAL
2 commands can do all of the above
• TO_REAL
• TO_LREAL
Data Type Conversion
• Not all data types can store the same data
• Possible that conversion will result in
truncating or rounding off of value
• Review help file for potential issues
• Convert to INT rounds off decimal places
Data Type Conversion
.5 does NOT always round up
Based on digit before decimal point, as shown
below
Other commands
• TRUNC truncates decimal places (no rounding)
• Round rounds the value per the table above
• RoundUp always rounds up to next integer
Scaling
NJ Firmware 1.05 and Sysmac Studio 1.06
ScaleTrans scales a range of numbers to a
different range
• Linear scaling with offset function
• Analog Conversion section of Toolbox
• 2 inputs set range of incoming data
• 2 inputs set engineering units range
• 1 input for offset value
• 1 input for data to be scaled
• REAL or LREAL inputs only
Scaling
Scaling
• If data to input is not REAL or LREAL, use
conversion instructions
• If REAL or LREAL is not desired output, use
conversion instructions after function
• Same function can be achieved for integers by
writing a function following the formula shown
for ScaleTrans
Range Limiting
Keeps a value within a specific range
Limit can be found in the Selection group in the
Toolbox
Looks similar to ZoneCmp, but outputs a numeric
value within the specified range, not just a bit
indicating true or false
Range Limiting
Example
• If number too high, output is upper limit
• If number too low, output is lower limit
• If in range, input value is output