21. Advanced Ladder Logic Functions
• Advanced PLC functions go beyond basic ladder logic, and allow capabilities like full programming languages.
• Note: When executing most ladder logic the inputs are read at the beginning of the scan, and the output are set at the end. When using functions the values are changed imediately.
• To use advanced data functions in a PLC, we must first understand the structure of the data in the PLC memory.
• There are two types of memory used in a PLC-5.
Program Files: these are a collection of 1000 slots to store up to 1000 programs. The main program will be stored in program file 2. SFC programs must be in file 1, and file 0 is used for program and password information. All other program files from 3 to 999 can be used for ‘subroutines’.
Data Files: This is where the variable data is stored that the PLC programs operate on. This is quite complicated, so a detailed explanation follows.
21.1.1 Data Files
• In brief PLC memory works like the memories in a pocket calculator. The values below are for a PLC-5, although most Allen-Bradley PLCs have a similar structure.
• These memory locations are typically word oriented (16 bits, or 2 bytes). This includes the bit memory. But the T4, C5, R6 data files are all three words long.
• All values are stored and used as integers (except when specified, eg. floating point). When integers are stored in binary format 2’s complements are used to allow negative numbers. BCD values are also used.
• There are a number of ways the PLC memory can be addressed,
bit: individual bits in memory can be accessed: this is like addressing a single output as a data bit
word/integer: 16 bits can be manipulated as a group
data value: an actual data value can be provided
file level: an array of data values can be manipulated and operated on as a group
indirect: another memory location can be used in the description of a location.
expression: a text string that describes a complex operation
• For the user assigned data files from 9 to 999 different data types can be assigned. These can be one of the data types already discussed, or another data type.
BT: block transfer
F: floating point
N: integer (signed, unsigned, 2s compliment, BCD)
PD: PID controller
SC: SFC status
ST: ASCII string
188.8.131.52 - Inputs and Outputs
• Recall that the inputs and outputs use octal for specific bits. This means that the sequence of output bits is 00, 01, 02, 03, 04, 05, 06, 07, 10, 11, 12, 13, 14, 15, 16, 17
184.108.40.206 - User Bit Memory
• Bit data file B3 is well suited to use of single bits. the data is stored as words and this allows two different ways to access the same bit.
B3:0/0 = B3/0
B3:0/10 = B3/10
B3:1/0 = B3/16
B3:1/5 = B3/21
B3:2/0 = B3/32
• The integer file N7 stores words in 2’s complement form. This allows values from -32768 to 32767. These values can be addressed as whole words, and individual bits can also be changed.
• The floating point file F8 will store floating point numbers that can only be used by floating point functions. The structure of these numbers does not allow bit access.
220.127.116.11 - Timer Counter Memory
• Timer T4 values are addressed using the number of the timers, and an associated data type. For example the accumulator value of timer 3 is T4:3.ACC or T4:3/ACC.
EN: timer enabled bit (bit 15)
TT: timer timing bit (bit 14)
DN: timer done bit (bit 13)
PRE: preset word
ACC: accumulated time word
• Counter C5 values are addressed using the number of the counters, and an associated data type. For example the accumulator value of counter 3 is C5:3.ACC or C5:3/ACC.
CU: count up bit (bit 15)
CD: count down bit (bit 14)
DN: counter done bit (bit 13)
OV: overflow bit (bit 12)
UN: underflow bit (bit 11)
PRE: preset word
ACC: accumulated count word
• The values for the timers and counters can be accesses directly.
Problem 21.1 Design some simple ladder logic to turn on a light for the first 10 seconds after a door is opened.
18.104.22.168 - PLC Status Bits (for PLC-5s and Micrologix)
• Some of the more commonly useful status bits in data file S2 are given below. Full listings are given in the manuals.
S2:0/0 carry in math operation
S2:0/1 overflow in math operation
S2:0/2 zero in math operation
S2:0/3 sign in math operation
S2:1/15 first scan of program file
S2:8 the scan time (ms)
S2:28 watchdog setpoint
S2:29 fault routine file umber
S2:30 STI (selectable timed interrupt) setpoint
S2:31 STI file number
S2:46-S2:54,S2:55-S2:56 PII (Programmable Input Interrupt) settings
S2:55 STI last scan time (ms)
S2:77 communication scan time (ms)
22.214.171.124 - User Function Control Memory
• Control file R6 is used by various functions to track progress. Values that are available are, listed below. The use of these bits is specific to the function using the control location.
EN: enable bit (bit 15)
EU: enable unload (bit 14)
DN: done bit (bit 13)
EM: empty bit (bit 12)
ER: error bit (bit 11)
UL: unload bit (bit 10)
IN: inhibit bit (bit 9)
FD: found bit (bit 8)
LEN: length word
POS: position word
• Different bits will use these memory locations differently. It will be rare for any instruction to use all of these memory bits and words.
126.96.36.199 - Integer Memory
• This memory can hold values from -32768 to +32767: These values cannot be exceeded.
• Decimal fractions are not allowed.
• The values are stored as 2’s compliment.
• These values are normally stored in N7:xx by default, but new blocks of integer memory are often created in other locations.
188.8.131.52 - Floating Point Memory
• This can hold real values with fraction in the range of +/-1.1754944e-38 to +/-3.4028237e38.
• These values are always limited to 7 digits of accuracy.
• Floating point memory is stored in F8:xx by default.
21.2 Instruction Types
• There are basic categories of instructions,
Basic (discussed before)
Basic Data Handling
Advanced Data Handling
high speed counters
ASCII string functions
• 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,
TON, TRO, TOF, ADD, MUL, etc.
21.2.1 Basic Data Handling
• Some handy functions found in PLC-5’s (similar functions are available in other PLC’s)
184.108.40.206 - Move Functions
• There are two types of move functions,
MOV(value,destination): moves a value to a memory location
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,
220.127.116.11 - 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,
ADD(value,value,destination): add two values
NEG(value,destination): reverse sign from positive/negative
CLR(value): clear the memory location
• Consider the example below,
Problem 21.2 Try the calculation below with ladder logic,
• Some intermediate math functions include,
CPT(destination,expression): does a calculation
ACS(value,destination): inverse cosine
ASN(value,destination): inverse sine
ATN(value,destination): inverse tangent
XPY(value,value,destination): X to the power of Y
LN(value,destination): natural log
LOG(value,destination): base 10 log
SQR(value,destination): square root
• Examples of some of these functions are given below.
Problem 21.3 For practice implement the following function,
• Some functions are well suited to statistics.
AVE(start value,destination,control,length): average of values
STD(start value,destination,control,length): standard deviation of values
SRT(start value,control,length): sort a list of values
• Examples of these functions are given below.
• There are also functions for basic data conversion.
TOD(value,destination): convert from BCD to binary
FRD(value,destination): convert from binary to BCD
DEG(value,destination): convert from radians to degrees
RAD(value,destination): convert from degrees to radians
• Examples of these functions are given below.
21.2.2 Logical Functions
18.104.22.168 - 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,
CMP(expression): compares two values for equality
NEQ(value,value): not equal
LES(value,value): less than
LEQ(value,value): less than or equal
GRT(value,value): greater than
GEQ(value,value): greater than or equal
• 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,
MEQ(value,mask,threshold): compare for equality using a mask
LIM(low limit,value,high limit): check for a value between limits
• Examples of these functions are shown below.
22.214.171.124 - Binary Functions
• These functions allow Boolean operations on numbers and values in the PLC memory.
• Binary functions are also available for,
AND(value,value,destination): Binary and function
OR(value,value,destination): Binary or function
NOT(value,value,destination): Binary not function
XOR(value,value,destination): Binary exclusive or function
• Examples of the functions are,
21.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,
1 = on
0 = off
• 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
100 = Motor 1 is the only one on
111 = All three motors are on
in total there are 2n or 23 possible combinations of motors on.
• The most common Binary operations are,
21.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,
21.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.
Problem 21.4 Convert the BCD number below to a decimal number,
Problem 21.5 • Convert the following binary number to a BCD number,
21.2.6 Advanced Data Handling
126.96.36.199 - 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,
move/copy memory blocks
add/subtract/multiply/divide/and/or/eor/not/etc blocks of memory
• 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.
• The basic functions are,
FAL(control,length,mode,destination,expression): will perform basic math operations to multiple values.
FSC(control,length,mode,expression): will do a comparison to multiple values
COP(start value,destination,length): copies a block of values
FLL(value,destination,length): copies a single value to a block of memory
• These functions are done on a PLC-5 using file commands. Typical operations include
file to file: copy an array of memory from one location to another.
element to file: one value is copied to a block of memory
file to element: can convert between data types
file add: add arrays
file subtract: subtract arrays
file multiply: multiply arrays
file divide: divide an array by a value
convert to/from BCD
AND/OR/XOR/NOT: perform binary functions.
• Examples of these functions are shown below.
• a useful function not implemented on PLC-5 processors is a memory exchange.
21.2.7 Complex Functions
188.8.131.52 - Shift Registers
• The values can be shifted left or right with the following functions.
BSL: shifts left from the LSB to the MSB. The LSB must be supplied
BSR: similar to the BSL, except the bit is input to the MSB and shifted to the LSB
• 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.
184.108.40.206 - 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.
220.127.116.11 - 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)
• The commands are,
SQO(start,mask,source,destination,control,length): sequencer output from table to memory address
SQI(start,mask,source,control,length): sequencer input from memory address to table
SQL(start,source,control,length): sequencer load to set up the sequencer parameters
• 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.
21.2.8 Program Control Structures
• These change the flow of execution of the ladder logic.
18.104.22.168 - Branching and Looping
• These functions allow control found in languages like Fortran
IF-THEN is like MCR (Master Control Reset)
GOTO is like JMP (Jump)
SUBROUTINES is like Program Files
• 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.
22.214.171.124 - 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.
126.96.36.199 - Fault Detection and Interrupts
• The PLC can be set up to run programs automatically. This is normally done for a few reasons,
to deal with errors that occur (eg. divide by zero)
to run a program at a regular timed interval (eg. SPC calculations)
to respond when a long instruction is complete (eg. analog input)
when a certain input changed (eg. panic button)
• 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.
S:29: program file number to run when a fault occurs
• 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.
S:30: timed delay between program execution: an integer number of ms
S:31: the program number to be run
• To cause an interrupt when a bit changes the following bits can be set.
S:46: the program file to run when the input bit changes
S:47: the rack and group number (eg. if in the main rack it is 000)
S:48: mask for the input address (eg. 0000000000000100 watches 02)
S:49: for positive edge triggered =1 for negative edge triggered = 0
S:50: the number of counts before the interrupt occurs 1 = always up to 32767
21.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.
21.3 Design Techniques
21.3.1 State Diagrams
• We can implement state diagrams seen in earlier sections using many of the advanced function discussed in this section.
• Most PLCs allow multiple programs that may be used as subroutines. We could implement a block logic method using subroutine programs.
• Consider the state diagram below and implement it in ladder logic. You should anticipate what will happen if both A and C are pushed at the same time.
21.4 Design Cases
• If-then can be implemented different ways, as a simple jump, or as a subroutine call.
• For-next can be implemented as shown below, but recall that PLC programs do not execute one line at a time.
• A For/Next function is also available in the PLC.
• A do-while can be done as a simple variation of this.
• Consider a conveyor where parts enter on one end. they will be checked to be in a left or right orientation with a vision system. If neither left nor right is found, he part will be placed in a reject bin. The conveyor layout is shown below.
21.5 Function Reference
• The following function descriptions are for both the micrologix (500/1000) and PLC-5 processor families.
• Note that floating point operations are not available on the micrologix.
• Some of the flags referred to functions are:
S2:0/0 carry in math operation
S2:0/1 overflow in math operation
S2:0/2 zero in math operation
S2:0/3 sign in math operation
• Counter memory instructions can share the same memory location, so some redundant bits are mentioned here.
PROCESS CONTROL AND MESSAGES
Problem 21.6 A switch will turn a counter on when engaged. This counter can be reset by a second switch. The value in the counter should be multiplied by 5, and then displayed as a binary output using (201-208)
Problem 21.7 2. Develop Ladder Logic for a car door/seat belt safety system. When the car door is open, or the seatbelt is not done up, the ignition power must not be applied. In addition the key must be able to switch ignition power.
1. List of Inputs
2. Draw Ladder
Problem 21.8 3. TRUE / FALSE -- PLC outputs can be set with Bytes instead of bits.
Answer 21.8 true
Problem 21.9 4. Create a ladder logic program that will start when input ‘A’ is turned on and calculate the series below. The value of ‘n’ will start at 1 and with each scan of the ladder logic ‘n’ will increase until n=100. While the sequence is being incremented, any change in ‘A’ will be ignored.
Problem 21.10 5. A thumb wheel input card acquires a four digit BCD count. A sensor detects parts dropping down a chute. When the count matches the BCD value the chute is closed, and a light is turned on until a reset button is pushed. A start button must be pushed to start the part feeding. Develop the ladder logic for this controller. Use a structured design technique such as a state diagram.
Problem 21.11 Design and write ladder logic for a simple traffic light controller that has a single fixed sequence of 16 seconds for both green lights and 4 second for both yellow lights. Use either stacks or sequencers.
Problem 21.12 A PLC is to be used to control a carillon (a bell tower). Each bell corresponds to a musical note and each has a pneumatic actuator that will ring it. The table below defines the tune to be programmed. Write a program that will run the tune once each time a start button is pushed. A stop button will stop the song.
Problem 21.13 The following program uses indirect addressing. Indicate what the new values in memory will be when button A is pushed after the first and second instructions.