24.2 INSTRUCTION TYPES
There are basic categories of instructions,
The reader should be aware that some functions are positive edge triggered (i.e. they only work the scan is active). while most are active any time the input is active. Some examples of edge triggered and non-edge triggered functions are listed below,
24.2.1 Basic Data Handling
Some handy functions found in PLC-5's (similar functions are available in other PLC's)
24.2.1.1 - Move Functions
There are two types of move functions,
MVM(value,mask,destination) - moves a value to a memory location, but with a mask to select specific bits.
The following function moves data values between memory locations. The following example moves a floating point number from floating point memory 7 to 23
The following example moves a floating point number from floating point memory F8:7 to integer memory N7:23
The following example puts an integer value 123 in integer memory N7:23
A more complex example of the move functions follows,
24.2.1.2 - Mathematical Functions
These functions use values in memory, and store the results back in memory (Note: these functions do not use variables like normal programming languages.)
Math functions are quite similar. The following example adds the integer and floating point number and puts the results in `F8:36'.
Basic PLC-5 math functions include,
As an exercise, try the calculation below with ladder logic,
Some intermediate math functions include,
Examples of some of these functions are given below.
For practice implement the following function,
Some functions are well suited to statistics.
Examples of these functions are given below.
There are also functions for basic data conversion.
Examples of these functions are given below.
24.2.2 Logical Functions
24.2.2.1 - Comparison of Values
These functions act like input contacts. The equivalent to these functions are if-then statements in traditional programming languages.
Basic comparison functions in a PLC-5 include,
The comparison function below compares values at locations A and B. If they are not equal, the output is true. The use of the other comparison functions is identical.
More advanced comparison functions in a PLC-5 include,
Examples of these functions are shown below.
24.2.2.2 - Binary Functions
These functions allow Boolean operations on numbers and values in the PLC memory.
Binary functions are also available for,
Examples of the functions are,
24.2.3 Boolean Operations
In most discrete systems the inputs and outputs (I/O) are either on or off. This is a binary state that will be represented with,
Because there are many inputs and outputs, these can be grouped (for convenience) into binary numbers.
Consider an application of binary numbers. There are three motors M1, M2 and M3
The most common Binary operations are,
24.2.4 Binary Mathematics
These include standard logic forms such as,
Negative numbers are a particular problem with binary numbers. As a result there are two common numbering systems use,
- signed binary - the most significant bit (MSB) of the binary number is used to indicate positive/negative
- 2s compliment - negative numbers are represented by complimenting the binary number and then adding 1.
Signed binary numbers are easy to understand, but much harder to work with when doing calculations.
An example of 2s compliments are given below,
When adding 2s compliment numbers, additional operations are not needed to deal with negative numbers. Consider the examples below,
24.2.5 BCD (Binary Coded Decimal)
Each digit is encoded in 4 bits
This numbering system makes poor use of the digits, but is easier to convert to/from base 10 numbers. For the two bytes above the maximum numbers possible are from 0-9999 in BCD, but 0-64285 in binary.
Convert the BCD number below to a decimal number,
Convert the following binary number to a BCD number,
24.2.6 Advanced Data Handling
24.2.6.1 - Multiple Data Value Functions
We can also deal with large `chunks' of memory at once. These will not be covered, but are available in texts. Some functions include,
These functions are similar to single value functions, but they also include some matrix operations. For a PLC-5 a matrix, or block of memory is also known as an array.
FAL(control,length,mode,destination,expression) - will perform basic math operations to multiple values.
These functions are done on a PLC-5 using file commands. Typical operations include
Examples of these functions are shown below.
a useful function not implemented on PLC-5 processors is a memory exchange.
24.2.7 Complex Functions
24.2.7.1 - Shift Registers
The values can be shifted left or right with the following functions.
These use bit memory blocks of variable length.
An example of a shift register is given below. In this case it is taking the value of bit B3:1/0 and putting it in the control word bit R6:2/UL. It then shifts the bits once to the right, B3:1/0 = B3:1/1 then B3:1/1 = B3:1/2 then B3:1/2 = B3:1/3 then B3:1/3 = B3:1/4. Then the input bit is put into the most significant bit B3:1/4 = I:000/00.
There are other types of shift registers not implemented in PLC-5s.
24.2.7.2 - Stacks
We can also use stack type commands. These allow values to be stored in a `pile'. This allows us to write programs that will accumulate values that can be used later, or in sequence.
The basic concept of a FIFO stack is that the first element in is the first element out.
The PLC-5 commands are FFL to load the stack, and FFU to unload it.
The example below shows two instructions to load and unload the stack. The first time FFL is activated it will grab all of the bits from the input card I:001 and store them on the stack, at N7:0. The next value would be at N7:1, and so on until the stack length is met. When FFU is used the value at N7:0 will be moved to set all of the bits on the output card O:003 and the values on the stack will be shifted up so that the value previously in N7:1 is now in N7:0, etc. (note: the source and destination do not need to be inputs and outputs)
A Last-In-First-Out stack can also be used with the LFL/LFU functions.
24.2.7.3 - Sequencers
Basically, sequencers are a method for using predetermined patterns to drive a process
These were originally based on motor driven rotating cams that made and broke switches. When a number of these cams were put together, they would be equivalent to a binary number, and could control multiple system variables.
A sequencer can keep a set of values in memory and move these to memory locations (such as an output card) when directed.
These are well suited to state diagrams/processes with a single flow of execution (like traffic lights)
An example of a sequencer is given below for traffic light control. The light patterns are stored in memory (entered manually by the programmer). These are then moved out to the output card as the function is activated. The mask (003F = 0000000000111111) is used so that only the 6 LSB are changed.
24.2.8 Program Control Structures
These change the flow of execution of the ladder logic.
24.2.8.1 - Branching and Looping
These functions allow control found in languages like Fortran
MCR blocks have been used earlier, but they are worth mentioning again.
Block of ladder logic can be bypassed using a jump statment.
Subroutines allow reusable programs to be written and called as needed. They are different from jump statements because they are not part of the main program (they are other program files), and arguments can be passed and returned.
For next loops can also be done to repeat blocks of ladder logic inside a single scan. Care must be used for this instruction so that the ladder logic does not get caught in an infinite, or long loop - if this happens the PLC will experience a fault and halt.
Ladder logic programs always have an end statement, but it is often taken for granted and ignored. Most modern software automatically inserts this. Some PLCs will experience faults if this is not present.
There is also a temporary end (TND) that for a single ladder scan will skip the remaining portion of a program.
A one shot contact can be used to turn on a ladder run for a single scan. When the run has a positive rising edge the oneshot will turn on the run for a single scan. Bit `B3:0' is used here to track to rung status.
24.2.8.2 - Immediate I/O Instructions
This approach avoids problems caused by logic setting and resetting outputs before done.
If we have a problem we may want to update an output immediately, and not wait for the PLC to complete its scan of the ladder logic. To do this we use immediate inputs and outputs.
24.2.8.3 - Fault Detection and Interrupts
The PLC can be set up to run programs automatically. This is normally done for a few reasons,
Two types of errors will occur - terminal (critical) and warnings (non-critical). A critical failure will normally stop the PLC.
In some applications faults and failures must be dealt with in logic if possible, if not the system must be shut down.
There are some memory locations that store indications of warning and fatal errors that have occurred. The routine in program file [S:29] needs to be able to detect and clear the fault.
To set a timed interrupt we will set values in the status memory as indicated below. The program in file [S:31] will be run every [S:30]ms.
To cause an interrupt when a bit changes the following bits can be set.
24.2.9 Block Transfer Functions
Certain PLC cards only have a single address (eg. O:001 or I:001) but multiple data values need to be read or written to it. To do this the block transfer functions are used.
These will be used in the labs for analog input/output cards.
These functions will take more than a single scan, and so once activated they will require a delay until they finish.
To use the write functions we set up a block of memory, the function shows this starting at N9:0, and it is 10 words long (this is determined by the special purpose card). The block transfer function also needs a control block of memory, this is BT10:1
To read values we use a similar method. In the example below 9 values will be read from the card and be placed in memory locations from N9:4 to N9:11.