|
Microprocessor
Control
Microprocessors represent the pinnacle
of digital system design. They are used to control almost all automated
processes. A microprocessor can be used to
perform almost any function. It is small, relatively cheap and reliable. More
importantly replacing it with a similar system built from discreet components
would involve a very difficult and complex design task, an enormously complex,
very large and
unreliable electronic system, and a lot of maintenance.
As with all digital systems the
trend with microprocessor design is towards increasing complexity with greater
speed of operation at an ever reducing physical size and cost.
A microprocessor is controlling the
PC you are using at the moment, but they are also at the heart of PLC's
(Programmable Logic Controllers) and more recently PIC's (Programmable
Interface Controllers) which are both widely used in industrial automation and
control.
Basic Structure
and operation of microprocessor systems
The two main elements of a microprocessor
system are the CPU or Central Processing Unit and its Memory. The
interconnections are known as buses because they contain a large number
of parallel connecting wires. The three sets of buses connecting
these two blocks are:
 | The
data bus |
 | The address bus
|
 | The control bus
|
The data bus carries data being processed
in both directions. The single direction address bus carries memory
addresses. The control bus is used to make sure everything works in the
correct sequence by sending and receiving timing signals. |
 |
|
There must also be some means of
communicating between the CPU and the outside world. This is achieved
using peripheral devices such as keyboards, mice and VDU's which either
send (input) or receive (output) information via the input/output port.
Sometimes this information
is digitally encoded and has to be decoded before the CPU can handle it.
At other times it may be generated by an analogue device which needs to
be converted into a digital signal (or vice-versa) using A-D (analogue
to digital) or D-A (digital to analogue) converters before use. |
All microprocessor systems have some memory.
It may be permanent and non-volatile (Read Only Memory or ROM) or temporary,
volatile memory (Random Access Memory or RAM).
 |
RAM or random access memory is a
flexible read/write memory. Most PLC's and some PIC's will have some built
in RAM which is used to store programs being written by the user. When we
say that RAM is volatile we mean that it cannot be used to store data whilst
the PLC or PIC is turned off unless the RAM is battery backed (has a battery
permanently attached to hold the data in memory). CMOS RAM is usually used
in a battery backed system because it uses so little power and operates of
wider voltage ranges than traditional TTL circuitry.
|
 | DRAM or dynamic
random access memory effectively consists of capacitors which must be kept
at a constant level of charge if they are to stored information. For this
purpose refresh facilities regularly top up the charge on each cell. DRAM is
much smaller than equivalent static RAM. |
 |
ROM or read only memory is programmed
during manufacture. We say it is non-volatile because its data is built in
and is not lost if power is removed. ROM is often used to hold the operating
system and other fixed data required by the microprocessor system.
|
 |
EPROM or erasable programmable read
only memory is a type of ROM which can be programmed by electrical pulses
and then erased by exposure to ultra-violet light through a quartz lens
fitted into the top of each device. Once programmed the lens is covered by
an opaque plastic or paper sticker. In this state the EPROM provides
non-volatile ROM.
|
 |
EEPROM or electrically erasable
programmable read only memory is similar to EPROM but is erased as well as
programmed using electrical pulses. It has the flexibility of battery backed
CMOS RAM but does not require the battery back up. It will store data indefinitely
until reprogrammed. EEPROM is used in most modern PLC's and PIC's primarily
for this reason but writing data into an EEPROM does take longer than into
RAM. For most applications however this is not a problem. |
The current developments in solid state technology and a
move towards ever increasing miniaturisation means that in
modern PLC's and PIC's most if not all the memory requirements are provided on a single
chip.
THE CPU
This is the microprocessors 'brain' - what it can do depends to a
large extent on the set of instructions (called the instruction set) which it is
designed to work with.
Typically a CPU consists of 3 main parts:
 | The ALU
|
 | The Registers
|
 | The Control Unit
|
The ALU or Arithmetic Logic
Unit performs all the arithmetic or logical functions.
The Registers are shift
registers where data to be processed can be fed in and temporarily stored. The
number and purpose of the registers varies from one microprocessor to another
but one of these shift registers is used as a program counter to provide the
addresses fed onto the address bus. Another register is used as the accumulator
which contains the data being processed at any one time.
The Control unit is linked to a clock which generates pulses at a frequency
measured in megahertz. Its purpose is to control and synchronize the way data is
processed.
The Instruction register holds each
instruction while it is decoded by the Instruction decoder which then
sends a signal to the control circuitry to enable the instruction to be
executed.
As an example let's look at just one
machine cycle. The first cycle in a program.
The content of program counter initially
0000 0000 is placed on the address bus.
The instruction in this first address is
'read out of memory' and placed on the data bus
The instruction is held in the
instruction register whilst it is decoded into a signal that goes to the
control circuitry. This then carries out the instruction.
Let us suppose the instruction was to
load a number stored in memory into the accumulator.
The program counter increments to 0000
0001 the contents of this address are placed on the data bus and the
control circuitry loads this into the accumulator.
|
|
If we wanted to add the data stored in the
accumulator to another number stored at a different address, the program must
give the necessary instructions to shift the first number from the accumulator
into one of the internal registers and to then read the second number and place
this in the accumulator.
An instruction to then add the contents of the
accumulator to the contents of the internal register can be sent to the ALU and
the result can then be stored in the accumulator for transfer to the output
unit.
Number Systems
Writing down groups of binary digits such as 1001
1110 can be cumbersome and confusing we often convert binary numbers into
decimal (base10), Octal (base 8), hexadecimal (base 16) or binary-coded decimal
numbers.
Each of these number systems is described below.
Binary
The binary number system uses two counting digits
(1 and 0). We write a binary number with its most significant bit (MSB)
furthest to the left and its least significant bit (LSB) furthest to the
right so that it is organised in ascending powers of 2 starting with the least
significant bit.
the diagram below should explain the conversion
from binary to decimal quite clearly.
| Bit 7 (MSB) |
|
|
|
|
|
|
Bit 1 (LSB) |
| 1 |
0 |
1 |
1 |
0 |
1 |
0 |
1 |
| 128 |
64 |
32 |
16 |
8 |
4 |
2 |
0 |
| 27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
Binary 10110101 =
(1x128)+(0x64)+(1x32)+(1x16)+(0x8)+(1x4)+(0x2)+(1x1)
Octal
The octal (base 8) number system uses a set of eight digits (0
to 7). Octal numbers are organised in ascending powers of 8. For example:
The octal number 321 = (3x82)+(2x81)+(1x80)
= (3x64)+(2x16)+(1x1) = 209 in decimal
To convert a binary number to octal you need to
split it into groups of three bits. This is because the octal numbers 0 to 7
represent the 3-bit binary numbers 000 to 111.
| Octal |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
| Binary |
111 |
110 |
101 |
100 |
011 |
010 |
001 |
000 |
The diagram below demonstrates how to convert an eight bit
binary number to octal and then decimal:
8 bit binary
number
11111101
Split into groups of three
bits 011
111 101
Octal
number
3 7 5
Decimal
number
3x82 + 7x81 + 5x80 = 192 + 56 +
5 = 253
Hexadecimal
The hexadecimal number system (base16) uses a set of sixteen
digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F). The letters A to F
represent the numbers 10 to 15. Hexadecimal numbers are organised in ascending
powers of 16.
Converting the number 321 used in the previous example from
hexadecimal to decimal = (3x162) + (2x161) + (1x160)
= (3x256) + (2x16) + (1x1) = 768 + 32 + 1 = 801.
To convert a binary number to hexadecimal you need to split
the number into groups of four bits. This is because the hexadecimal numbers 0
to F represent four bit numbers ranging from 0000 to 1111.
|
Hexadecimal |
F |
E |
D |
C |
B |
A |
9 |
8 |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
| Binary |
1111 |
1110 |
1101 |
1100 |
1011 |
1010 |
1001 |
1000 |
0111 |
0110 |
0101 |
0100 |
0011 |
0010 |
0001 |
0000 |
The diagram below demonstrates how to convert an eight bit
binary number to hexadecimal and then decimal:
8 bit binary
number
10101100
Split into groups of four bits
1010 1100
Hexadecimal
number
A C
Decimal
number
A(10)x161 + C(12)x160 = 10x16 + 12x1 = 172
Binary-Coded Decimal
Binary-coded decimal (BCD) numbering, codes decimal numbers
into 4-bit binary numbers. The digits 0 to 9 are represented by the numbers 0000
to 1001.
|
BCD |
9 |
8 |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
| Binary |
1001 |
1000 |
0111 |
0110 |
0101 |
0100 |
0011 |
0010 |
0001 |
0000 |
The diagram below demonstrates how to convert a decimal number
to BCD
Decimal
number
954
Binary Coded
Decimal
1001 0101 0100
Programming
All the systems that you have built or
investigated using discreet components have their function decided by the
connections you make between the various components. They are known as dedicated
systems because they are built to do just one thing. For example you will have
seen how D type flip-flops can be used to build a four bit binary counter, a
latch or a shift register, depending on how you connect them up. Once built they
will only perform one of these tasks. We say they have been hard wired. you can
only change their function by re-wiring them.
A programmable system such as a microprocessor
however, can have its function changed without changing any of its connections. To communicate with a microprocessor we must give
it instructions (called a program) so that it knows what it must do and how to
do it. The program tells the CPU the order in which it has to perform the
various operations provided by its instruction set. Different tasks require
these operations to be performed in different ways. Each task will have a
different program written for it.
At its most basic level programming can be done
in machine code. This is the series of 0's and 1's which a microprocessor has to
work with. Writing programs in this way however is tedious, difficult and
inevitably leads to errors which make the program fail.
To make programming more intelligible program
languages have been developed which make the task of writing a program less
difficult.
There are two main types:
 | Low level or assembly language. |
 | High level languages |
Low level or assembly languages
Programs for these are written in mnemonics (memory aids).
They are provided in an instruction set which must be adhered to. The mnemonic
for 'loading the contents of a memory address into the accumulator' might be LDA
and the binary code for the operation 1001 1110. This is more conveniently
written in hexadecimal code (which is 9E). Conversion of these inputs into
machine code is done by a program usually stored in ROM called an assembler.
High level languages
Programs for these are written using commands which are much
easier to understand and remember since they use terms such as Load, Add ,
Compare, Build etc. The one big drawback is that they need more memory space
and computer time because each statement has to be decoded into a number of
machine code instructions. A compiler or an interpreter (similar to the
assembler program) does the translation into machine code. BASIC, LOGO and
PASCAL are all examples of high level languages.
| Programmable
Logic Controllers (PLC's) |
| PLC's are used to
automate machinery in assembly lines. A PLC typically contains the
Microprocessor, Memory and Input/Output ports in a single robust unit.
The Inputs and outputs terminate to screw terminals so that input and
output devices can be wired directly to each terminal. These input and
output connections are called ports. The ports are allocated numbers so
that they can be uniquely identified.
With the majority of PLC's writing a program is
similar to drawing a switching circuit. The switching circuit is drawn
in a ladder diagram format and is known as relay ladder
logic programming.
Ladder logic is a high level language, it is not necessarily
difficult but ladder logic
is quite different from more common programming languages such as BASIC or FORTRAN.
Ladder logic uses conditional statements, subroutines and FOR
NEXT loops but there are some very significant differences.
Normally when constructing a basic or other high level
program things happen in
order. Even though the program may contain conditional statements, subroutines and FOR
NEXT loops each command line is executed sequentially until you reach the end of
the program. This is not so in ladder logic. A number of things happen
at the same time. Ladder diagrams have to conform to a number of rules. |

|
 | Circuits are arranged as a series of horizontal lines
containing both inputs (referred to as contacts) and outputs (referred to as
coils). |
 | Inputs must always precede outputs and are in the form of
normally open
and normally closed
contacts. |
 | There must always be at least one output on each line. The
symbol for an output is  |
 | The final rung on the ladder is always the End statement  |
The term ladder logic is used
In Ladder logic programming each instruction is placed in an
individual memory location (address). The CPU contains a program register which
points to the next instruction to be fetched from memory. When an instruction is
received by the CPU it is placed in the instruction register for decoding.
After decoding this may result in other instructions being
read from memory or an output being controlled by the CPU.
Inputs are usually prefixed by an X, outputs are usually
prefixed by Y's.
A ladder program can be translated into a program consisting
of instructions and data. Some simple examples are shown below.
|
Address |
Instruction |
Data |
| 0 |
LOAD |
IN1 |
| 1 |
AND |
IN2 |
| 2 |
OUT |
CR1 |
| 3 |
END |
|

|
Address |
Instruction |
Data |
| 0 |
LOAD |
IN1 |
| 1 |
OR |
IN2 |
| 2 |
OUT |
CR1 |
| 3 |
END |
|
Ladder circuits consisting of only inputs and
outputs are limited to just switching operations. To allow PLC's to handle more
advanced control tasks manufacturers incorporate a set of special functions.
These special functions vary from manufacturer to manufacturer but include
features such as counters, timers and latches.
Programmable Logic Controllers are rather bulky
and expensive for use in school a much better solution is to develop your own
controller based on PIC's
Programmable Interface
Controllers
| A programmable interface controller or PIC,
combines a microprocessor, ROM
They can be obtained in 8, 18 and 28 pin
configurations which provide a variety of output and digital and
analogue inputs.
The chips use reprogrammable 'flash memory' which can
written and rewritten to with ease.
Building a working controller involves simply
connecting the chip to power, interfacing input and output components
and adding a capacitor, resonator and a reset switch.
The most commonly used PIC is the 16F84 shown. This is
an 18 pin device which has 8 outputs and 5 inputs.
|

|
| Connecting Power to the
16F84 PIC
The pin out diagram for the 16F84 is shown on the
right. The 16F84 requires a 6V DC supply. This can be provided by 4 x AA
cells.
A 4MHz ceramic resonator must also be connected as
shown below. The 16F84 provides an internal clock pulse. The resonator is used
to regulate the speed of the clock pulse (4MHz).
Pin 4 (reset) must be connected via a 4k7 resistor to
+V.
A reset facility can also be added by adding a push to make between
pin 4 and 0V. |

|
|

|
|
Interfacing Input and
Output Devices
Providing care is taken over matching
voltage and current levels, digital sensors or transducers can be
directly connected to the input and output ports.
The most common digital sensors are
switches.

| Micro-Switches, Reed Switches, Tilt Switches
and Push Switches can all be directly connected to any input pin
as shown right. The 10k resistor prevents a short circuit and
the 1k resistor protects the input pin. In these cases the
digital input will move from logic 0 to logic 1 when the switch
is pressed. |
 |
Although the 16F84 does not have analogue
input pins an analogue sensor can be used if the sensor is connected via
a potential divider and transistor as shown.

A phototransistor can be used to switch
the input directly 
In both these cases the input will move
from logic 1 to logic 0 when the switching level is reached.
 |
An LED can be driven directly from any output |
| As can a Seven Segment display.
Taking the required outputs high in the
correct sequence will display the numbers 0 - 9 |
 |
|

|
A Piezo sounder can be connected directly to
produce a range of sounds.
The sounds are produced by pulsing the
required output with a variety of frequencies generated by the
programmed instructions. |
Higher current devices which cannot be
driven directly will require a simple transistor switching circuit. A
device which is frequently used is the BCX 38B darlington driver.
In some situations it will be impossible
to match voltage and current levels of the PIC and the input and output
devices you want to use. In this instance you will need to use two
separate power sources and drive the output transducer from a
transistor.
 |
Although the PIC and the output device have
different supply voltages the 0V rail must be common to both.
This is essential for the correct operation of
the circuit. |
| Stepper motors are motors which do
not spin freely but turn in steps of 7.5 0, so that
48 steps will cause the motor to turn through 3600 or
one complete revolution. The two types of stepper motor
available are known as unipolar and bipolar types. The unipolar
is the type explained below.
A suitable motor can be obtained from - Rapid
Electronics - code 37-0500
Unipolar stepper motors have four coils which
must be switched on and off in the correct sequence to make the
motor turn. The table below shows the correct sequence.
| Step |
Coil 1 |
Coil 2 |
Coil 3 |
Coil 4 |
| 1 |
1 |
0 |
1 |
0 |
| 2 |
1 |
0 |
0 |
1 |
| 3 |
0 |
1 |
0 |
1 |
| 4 |
0 |
1 |
1 |
0 |
| 5 |
1 |
0 |
1 |
0 |
|
The ULN 2003A darlington driver IC is used to
drive the stepper motor as show below. |
|

|
 |
This project board uses PCB mounted push
switches as inputs and LED's as outputs. Both IC's should be mounted in
sockets. The PCB mounted switches could be wired to other sensors and
there is the facility for 2 pin pcb connectors to be connected in
parallel to the output LED's to connect other output devices. A 7805
regulator and Darlington driver array is used to allow most output
devices to be driven safely. Double Click the PCB
to open the PCB Wizard file.
|
Most modern microcontrollers use what is
called 'flash' programming using
EEPROM (electrically erasable programmable read only memory). This means
that the device should be capable of being re-programmed over 10,000
times.
This is important because you will rarely
get the microcontroller program correct first time and you will
inevitably want to change it. If you do make a mistake, the PIC can
simply be re-programmed with new code.
The program will stay in the PIC's memory until it is
re-programmed.
Microcontrollers use instructions
provided in binary (a sequence of
0's and 1's) often called machine code.
This 'code' means very little to anyone but a skilled programmer.
In industry microcontrollers are
programmed using a 'compiler' or 'assembler'. This is a programming
language which is slightly easier to understand but unless you take
A-Level computing you will be unlikely to be able to use or understand
it.
For most applications in school, the
complexity of these languages makes their use impossible (or at least
unrealistic) for most applications.
In KS4 you may be taught a programming
language called BASIC, which the
majority of students cope with quite well. At KS3 however, even learning
BASIC is unrealistic in the time we have.
For most students a much simpler
and easier to understand programming technique is to use a programme
editor such as the one provided with Crocodile Technology.
Most programme editors use a flowchart
approach to design programs. The editing software then converts the
flowchart into BASIC or Assembly language before the microcontroller is
programmed. |
PIC based
PIC Logicator for instance requires a programmer
into which
you insert the microcontroller for programming.
The PICAXE system uses special microcontrollers with a
small programme called a bootstrap programme permanently written
into its memory which allows you to programme the microcontroller while
it is installed in your project.
The Chip
factory system is a low cost PIC programming unit that works without
a PC. It uses its own basic programming language, and is portable.
PIC Logicator and Chip factory both support a range of
28 and 18 pin microcontrollers. The PICAXE system provides support for
their own 8, 18 and 28 pin chips.
All this makes it very difficult to to explain
accurately how every microcontroller could be used. |
A number of newer Microcontrollers have
recently been released. They are much cheaper than the older devices,
enabling additional features at a lower cost. For instance the 18 pin
16F627 is much cheaper than the old 16F84A, has two analogue inputs, and
two extra input pins as it also has an internal resonator.
The types most commonly used are:
 | 8 PIN - 12F629 (2 input, 4 output)
|
 | 18 PIN - 16F627 (4 input, 2 analogue, 8 output, 1
sound)
 | 18 PIN - 16F84A (4 input, 8 output, 1 sound)
 | 18 PIN - 16F818 (2 input, 2 analogue, 8 output, 1
sound)
 | 28 PIN - 16F872 (8 input, 4 analogue, 8 output, 1
sound) |
| | |
|
|