LECTURE
¹ 7
PROGRAMMABLE LOGIC DEVICES
(PLDS). INTRODUCTION TO MICROPROCESSOR PROGRAMMING
7.1. NUMBER SYSTEM
Number system is
a way of representing the value of any number with respect to a base value.
Number System can be classified on the basis of its «base». Each number
has a unique representation in a number system. Different number systems have
different representation of the same number. In general Binary, Octal, Decimal
and Hexadecimal Number systems are used in microprocessor programming.
Decimal number system to any number
system
Any number in
decimal system can be changed to any other number system by continuously
dividing it by base of the required number system and then writing remainders
after each step in reverse order.
Let us take an
example of converting a decimal number 235 to its binary equivalent. Following
table shows the conversion process as stated above.
Table 7.1.1
Binary representation of (235)10
2 |
235 |
1 |
2 |
117 |
1 |
2 |
58 |
0 |
2 |
29 |
1 |
2 |
14 |
0 |
2 |
7 |
1 |
2 |
3 |
1 |
2 |
1 |
1 |
Hence Binary equivalent of (235)10
is (11101011)2.
Hexadecimal system
This system is
quite extensively used in microprocessor programming. It facilitates much
shorter representation of number in comparison with that obtained by using the
binary number system. Hexadecimal system has a base of 16 and it is easy to
write and remember the numbers and alphabets viz. 0 to 9 and A to F. Table 7.1.2 shows numerals
and alphabets used in hexadecimal system for representation of a number.
Table
7.1.2
Numerals and alphabets used in hexadecimal system
Decimal |
Binary |
Hexadecimal |
0 |
0000 |
0 |
1 |
0001 |
1 |
2 |
0010 |
2 |
3 |
0011 |
3 |
4 |
0100 |
4 |
5 |
0101 |
5 |
6 |
0110 |
6 |
7 |
0111 |
7 |
8 |
1000 |
8 |
9 |
1001 |
9 |
10 |
1010 |
A |
11 |
1011 |
B |
12 |
1100 |
C |
13 |
1101 |
D |
14 |
1110 |
E |
15 |
1111 |
F |
Example: Let us
convert the number (235)10 to hexadecimal equivalent. Table 7.1.2
shows the conversion of this decimal number.
Binary coded decimal (BCD)
BCD code
expresses each digit of a decimal system by its nibble equivalent. It uses 4
bit binary strings to represent the digits 0 to 9. Figure 7.1.1 shows the
representation of number 523 as 010100100011 using BCD system. Due its longer
representation scheme, it is now rarely used in micro-electronics programming.
Fig. 7.1.1. BCD
representation system
Example: (235)10 can be
represented by using PCD as 001000110101.
7.2. LOW LEVEL PROGRAMMING LANGUAGE
Microprocessors
recognize the binary numbers and they operate in binary numbers. Each
microprocessor has its own binary words, meanings and languages. Each machine
has its own set of instructions based on the design of its CPU. Binary language
is called as machine language. English-like words are used to represent the
binary instructions of a machine. This is called as assembly language programs.
The general purpose languages such as BASIC, FORTRAN, C, etc. are called as
high-level languages. The machine language and assembly language are however
specific to a microprocessor, thereforethese are termed as low-level languages.
Assembly language
In assembly
language, a word length is of about eight bits. It is called as a byte.
Assembly language can have 256 combinations of bytes. Thus the language has 256
words. There can be a various patterns of bytes. With the help of electronic
logic gates, these patterns give a specific meaning to each combination of
bytes. These are called as an «instruction». Instructions
are made up of one word or several words.
The set of instructions designed
into the machine makes up the machine language that is specific to each
microprocessor based system viz. micro-computer. Thus we can say, «Machine language
is the binary medium of communication through a designed set of instructions
specific to each computer», «Assembly level
language is a medium of communication with a computer in which programs are
written in mnemonics. An assembly language is specific to a given computer».
Assembly language programming
Assembly
language programming is generally written by using hexadecimal codes. Programs
can be written by using special keyboard equipped with using hex keys. Programs
also have instructions to translate these keys into their equivalent binary
patterns. The data and instructions are stored in prescribed locations in
memory. An operation code is written which accomplishes the intended task(s).
These tasks are carried out on «operand(s)» by the
operation code. 8085 is a typical general purpose microprocessor and has 8-bit
word length. Now, let us learn its architecture, working and programming.
Internal Architecture of 8085 Microprocessor.
Register Array
8085
Microprocessor consists of six registers, one accumulator and a flag register.
The typical architecture is shown in figure 7.2.1.
There are six general-purpose registers B, C, D, E, H, and L, each having
capacity to store 8 bit data. They are combined as BC, DE, HL to perform 16 bit
operations. In addition to this Register array, two 16 bit registers viz. stack
register and program counter are provided. As discussed in the earlier lecture,
the «program
counter» is
employed to sequence the execution of instructions. It always points to the
memory address from which the next byte is to be fetched. Stack Pointer points
to the memory location in R/W (Read and/or write) memory. It is also termed as
a «stack».
Fig. 7.2.1. Architecture of 8085 microprocessor
7.3.
ACCUMULATOR
The accumulator
is 8-bit register (can store 8 bit data). It is a part of arithmetic/logic unit
(ALU). In general, after performing logical or arithmetical operations, result
is stored in accumulator. Accumulator is also identified as Register A.
Flags
ALU of 8085 have
five flip flops whose states (set/reset) are determined by the result data of
other registers and accumulator. They are called as Zero, Carry, Sign, Parity
and Auxiliary-Carry flags:
·
Zero Flag (Z): When an arithmetic operation results in zero, the flip-flop
called the Zero flag - which is set to one.
·
Carry flag (CY): After an addition of two numbers, if the sum in the
accumulator is larger than eight bits, then the flip-flop uses to indicate a
carry called the Carry flag – which is set to one.
·
S-Sign (S): It is set to 1, if bit D7 of the result = 1; otherwise
reset. D7 is the first digit of a binary number.
S |
Z |
|
AC |
|
P |
|
CY |
·
P-Parity (P): If the result has an even number of 1s, the flag is set to
1; for an odd number of 1s the flag is reset.
·
AC-Auxiliary Carry (AC): In an arithmetic operation, when a carry is
generated by digit D3 and passed to digit D4, the AC flag is set. Generally
this flag is used internally for Binary Coded Decimals (BCD).
·
Figure 7.2.1 shows a 8-bit flag register, adjacent to the accumulator.
It is not used as a register. Out of eight bit-positions, five positions are
used to store the outputs of five flip-flops. These flags play an important
role in decision-making process of the microprocessor.
Instruction Register/Decoder
Before execution
of an instruction, it is sent to the Instruction Register. Instruction register
stores current instruction of any program. Decoder takes the instruction from
memory, decodes it and then passes it to the next stage.
Memory Address Register
Memory Address
Register (MAR) holds the address of next instruction to be executed.
Control Generator
In
microprocessor, the Control Generator generates a signal that executes the
operations in accordance to the decoded instructions. In fact it creates a
signal (information) which have details about connections between different
blocks of the microprocessor so that data reaches to the respective place.
Register Selector
Register
selector is basically a logical controller which directs switching between
different registers of microprocessor.
General Purpose Registers
Microprocessor
has few extra registers which can be used to store additional data during a
program.
Programming in 8085
As mentioned in
above section, a simple and very effective substitution to binary codes could
be use of standard English words to complete any task. For example addition of
two numbers can be represented by ADD. Such codes are referred as mnemonic
codes and
that language is called assembly language. Most of the early processers
including 8085, are programmed using mnemonics. However, assembly language
codes should be converted into binary one so that microprocessor can identify
the instructions given to it. This operation is done by Assembler. In assembly
language, instructions are composed of two segments which are as follows:
·
Operation (Op)
Code: It depends on which operation is to be performed. For
example for OR operation, we have Op Code «OR».
·
Operands:
Operand is the object on which the required operation is to be done. Generally
operations are done on data stored in registers.
Classification of Instructions:
·
Data Transfer;
·
Arithmetic;
·
Logical;
·
Program Control.
Data Transfer
1. Load:
It reads content from specified memory location and copies it to specified
register location in CPU.
2. Store:
It copies content of a specified register into specified memory location.
Arithmetic:
1. Add:
It adds contents of a specified memory location to the data in some register;
2. Decrement:
It subtracts 1 from contents of specified location;
3. Compare:
It tells whether contents of a register are greater than, less than or same as
content of specified memory location.
Logical:
1. AND: Instruction carries out Logical AND operation
with the contents of specified memory location and data in some register.
Numbers are ANDed bit by bit.
2. OR:
Instruction carries out Logical OR operation with the contents of specified
memory location and data in some register. Numbers are ORed bit by bit.
3. Logical
Shift: Logical shift instruction involves moving a pattern of bits in the
register one place to left or right by moving a zero in the end of number.
Program Control:
1. Jump:
This instruction changes the sequence in which program steps are carried out.
Normally program counter causes the program to be carried out sequentially in
strict numerical sequence. However, JUMP causes program counter to some other
specified location in the program.
2.
Branch: This is a conditional instruction which might «branch» if «zero» results or «branch» if «plus» results of an
operation. Branch also followed if right conditions occur in the decision
making process.
3.
Halt: This instruction stops all further microprocessor activity.
7.4. PROGRAMMABLE LOGIC
CONTROLLERS
Any computer
having input and output interfaces can be used to control external devices. However most of
the computers are not industrially hardened. Input / Output devices of general-purpose
microcomputers are not engineered to handle line-voltages and currents above
transistor-transistor logic (TTL) levels. Also they are not designed to
with-stand the temperature, humidity, and vibration on shop floors. These
drawbacks of a general purpose computer have been rectified by developing a
Programmable Logic Controller (PLC) with built-in isolation into their inputs
and outputs.
«The programmable
logic controller is defined as a digital electronic device that uses a
programmable memory to store instructions and to implement functions such as
logic, sequencing, timing, counting and arithmetic words to control machines
and processes».
PLCs are
generally used for incorporating automation in open loop systems where
processes are to be performed in a sequential manner. PLCs are used for
automation of assembly lines in industries. They are generally designed for
multiple input multiple output (MIMO) systems. In PLCs, instructions are saved
in nonvolatile memory. Some of the advantages of PLCs are:
·
Cost effective;
·
Flexibility and ability to use similar system for other processes;
·
Programming interface is easier in comparison to other processers;
·
Resistant to impact and vibration;
·
Resistant towards electrical and mechanical noise;
·
Ability to work at high temperatures;
·
Now let us study the structure and functioning of a PLC.
Figure 7.4.1 shows the
basic elements of a PLC. It is basically a microprocessor based control system.
Microprocessor communicates with the outside world with input/output devives
via a circuitry. This circuitry protects the microprocessor and other elements
of PLC from the high voltages and currents coming to the PLC. Microprocessor
does its basic functions of taking decisions according to the instructions
written in the programs which are stored in the memory. PLC scans a set of
sensor inputs rapidly and repeatedly. Then it evaluates their logic
relationships to defined outputs according to a logic program. At last it sets
the outputs according to the programmed logic. Figure 7.4.2 shows an
industrial PLC with input and output ports.
Programmable Logic Controller: Structure and Functioning
Fig. 7.4.1. Block diagram of a PLC
Fig. 7.4.2. An Industrial PLC
Programming a PLC:
PLCs are
programmed through concept of ladder logic. In general there exists a graphical
user interface (GUI) to program a PLC that makes it different from other
processers. Ladder logic comprises of two columns. Left column shows input
devices like switches, sensors while in output column is at right side which
shows actuators like cylinders, motors.
This instruction
is called as «examine
on» or «normally opened» as input
functions or storage bits. If the corresponding memory bit is a «1» then the
respective «rung» will
continuously be executed and the corresponding outputs will be energized. Rung
is one of the multiple horizontal programming lines in a ladder logic diagram.
NOTE: Other
factors may also affect rung simultaneously.
If the
corresponding bit is «0» then the rung
will not be executed continuously and outputs will be de-energized. If this
instruction is used as input bit, its status should be according to the status
of the real world input devices connected to the input table by identical
addresses.
Timer
This is also
called as «TON». Figure 7.4.3 shows the
schematic of a Timer. It is used to turn an output ON or OFF after the timer
has been ON for preset time interval. This output instruction begins timing when
rung goes true. It waits the specified amount of time (As specified in Preset),
keeps track of accumulated intervals which have occurred (ACCUM), and sets DN
(Done) bit when ACCUM time equals preset time.
As long as rung
condition remains true, Timer adjusts its accumulated value to each evaluation
until it reaches the preset value. The accumulated value is reset when rung
condition go false, regardless of whether timer has timed out. «TIME BASE» is an amount of time
after which accumulator increases its value by 1.
Fig. 7.4.3. Schematic
of a Timer
7.5. TO RUN THE CONVEYER BELT WITH THE HELP OF SWITCHES
As discussed in
earlier sections, PLCs are controlled through Ladder Logic. In input section of
the ladder, name of the input device must be mentioned on the top of symbol,
followed by primary input port. Secondary input port is mentioned just below
symbol. In similar way, output symbol should be mentioned with name and output
ports as shown in figure 7.5.1.
Fig. 7.5.1. A
rung to run the conveyor belt
To control
movement of pneumatic devices in an industry with PLCs. Figure 7.5.2. shows a
program code to control the motion of pneumatic cylinder with a switch.
Fig. 7.5.2. Program
code to actaute a pneumatic cylinder
In industrial
applications, it is required to use various sensors to control the operations
of systems and proceses using PLCs. Figure 7.5.2 shows a typical program to
operate an electric motor and a pnematic cylinder with the help of some sensors
such pneumatic proximity switch. To control a mechatronics system we need to
combine various mechanical and electrical input and output devices and to
operate them in a sequential manner. Consider a prototype of
industrial assembly line with 3 stations as shown in Figure 7.4.2.
At first station
ST1, the sensor identifies an object (finished product) on the conveyer belt
and sends a signal to the controller. Controller processes this information and
actuates the electric motor to run the conveyer belt.
Second Station
ST2: It is allotted for the inspection of the finished product or object. At
ST2, conveyer belt stops. In case any fault diagnosed by the inspection system,
the product will be taken away by the pneumatic actuators placed at Station 3,
ST3.