0% found this document useful (0 votes)
2 views27 pages

CPE323 Microprocessor Module 5 Input Output Interfacing

This document covers input-output interfacing in microprocessors, detailing methods of communication between the microprocessor and external devices, including serial and parallel communication. It explains the concepts of isolated and memory-mapped I/O, their advantages and disadvantages, as well as the steps for interfacing I/O devices. Additionally, it introduces the PIC microcontroller as a compact alternative to traditional microprocessor circuits.

Uploaded by

hexelsrana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views27 pages

CPE323 Microprocessor Module 5 Input Output Interfacing

This document covers input-output interfacing in microprocessors, detailing methods of communication between the microprocessor and external devices, including serial and parallel communication. It explains the concepts of isolated and memory-mapped I/O, their advantages and disadvantages, as well as the steps for interfacing I/O devices. Additionally, it introduces the PIC microcontroller as a compact alternative to traditional microprocessor circuits.

Uploaded by

hexelsrana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CPE323

Microprocessor
Module No. 5
Topic Input-Output Interfacing
Period Week no.__14 and 15__: Date _ May 29 to June 10, 2023_

INPUT-OUTPUT INTERFACING

A microprocessor is great at solving problems, but if it can’t communicate with the outside
world, it is of little worth. Though the input and output instructions were discussed from the previous
lesson, it still need a further discussion in actually using them. This chapter outlines some of the basic
methods of communications, both serial and parallel, between humans or machines and the
microprocessor.
In this lesson, we first introduce the basic I/O interface and discuss about two different
methods of interfacing I/O to the microprocessor. Then proceed to provide detail on PIC
microcontroller, which is a one single package composed of traditional microprocessor circuit. To
study applications, input and output device interfacing are also be tackled.

Objective/Intended Learning Outcomes


 Students should be able to understand the input and output interfaces and its methods.
 Students should be able to know the different input and output devices for interfacing.
 Students should be able to be familiar with PIC microcontroller.

Discussion/Content
5. INPUT-OUTPUT INTERFACING
I/O INTERFACE
Interface is the path for communication between two components. An I/O interface is any interface that
transfers data between the CPU/memory and the rest of the world. The main purpose of the I/O interfaces
is to transmit and receive data to and from the microprocessor.
Input-Output Interface is used as an method which helps in transferring of information between the
internal storage devices i.e. memory and the external peripheral device. A peripheral device is that which
provide input and output for the computer, it is also called Input-Output devices.
The input/output system of the microprocessor allows peripherals to provide data or receive results of
processing the data. This is done using I/O ports with the use of I/O instructions.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 1


I/O INSTRUCTIONS
The instruction set contains one type of instruction that transfers information to an I/O device (OUT)
and another to read information from an I/O device (IN). Instructions (INS and OUTS, found on all
versions except the 8086/8088) are also provided to transfer strings of data between the memory and an
I/O device. Figure 1 lists all versions of each instruction found in the microprocessor’s instruction set.

Figure 1. Input/Output instructions.

Instructions that transfer data between an I/O device and the microprocessor’s accumulator (AL, AX, or
EAX) are called IN and OUT.

The I/O address is stored in register DX as a 16-bit I/O address or in the byte (p8) immediately following
the opcode as an 8-bit I/O address. Intel calls the 8-bit form (p8) a fixed address because it is stored with
the instruction, usually in a ROM.

The 16-bit I/O address in DX is called a variable address because it is stored in a DX, and then used to
address the I/O device. Other instructions that use DX to address I/O are the INS and OUTS instructions.
I/O ports are 8 bits in width so whenever a 16-bit port is accessed two consecutive 8-bit ports are actually
addressed. A 32-bit I/O port is actually four 8-bit ports.

For example, port 100H is accessed as a word, then 100H and 101H are actually accessed. Port 100H
contains the least significant part of the data and port 101H the most significant part.

Whenever data are transferred by using the IN or OUT instructions, the I/O address, often called a port
number (or simply port), appears on the address bus.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 2


The external I/O interface decodes the port number in the same manner that it decodes a memory address.
The 8-bit fixed port number (p8) appears on address bus connections A7–A0 with bits A15–A8 equal to
000000002. The address connections above A15 are undefined for an I/O instruction. The 16-bit variable
port number DX) appears on address connections A15–A0. This means that the first 256 I/O port
addresses (00H–FFH) are accessed by both the fixed and variable I/O instructions, but any I/O address
from 0100H to FFFFH is only accessed by the variable I/O address. In many dedicated systems, only the
rightmost 8 bits of the address are decoded, thus reducing the amount of circuitry required for decoding.

In a PC computer, all 16 address bus bits are decoded with locations 0000H–03FFH, which are the I/O
addresses used for I/O inside the PC on the ISA (industry standard architecture) bus.

The INS and OUTS instructions address an I/O device by using the DX register, but do not transfer data
between the accumulator and the I/O device as do the IN and OUT instructions. Instead, these
instructions transfer data between memory and the I/O device. The memory address is located by ES:DI
for the INS instruction and by DS:SI for the OUTS instruction. As with other string instructions, the
contents of the pointers are incremented or decremented, as dictated by the state of the direction flag (DF).
Both INS and OUTS can be prefixed with the REP prefix, allowing more than one byte, word, or
doubleword to be transferred between I/O and memory.

The Pentium 4 and Core2 operating in the 64-bit mode have the same I/O instructions. There are no 64-
bit I/O instructions in the 64-bit mode. The main reason is that most I/O is still 8 bits and likely will
remain so for an indefinite time.

ISOLATED AND MEMORY-MAPPED I/O


There are two different methods of interfacing I/O to the microprocessor:
 Isolated I/O
In the isolated I/O scheme, the IN, INS, OUT, and OUTS instructions transfer data between the
microprocessor’s accumulator or memory and the I/O device.

The most common I/O transfer technique used in the Intel microprocessor-based system is
isolated I/O. The term isolated describes how the I/O locations are isolated from the memory
system in a separate I/O address space. Figure 2 illustrates both the isolated and memory-mapped
address spaces for any Intel 80X86 or Pentium–Core2 microprocessor.

Figure 2. Isolated I/O for the memory and I/O maps for the 8086/8088 microprocessors.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 3


The addresses for isolated I/O devices, called ports, are separate from the memory. Using isolated
I/O a microcomputer system, the I/O devices are treated separate from memory. Because the
ports are separate, the user can expand the memory to its full size without using any of memory
space for I/O devices.

The part of the I/O address space from address 0000H through 00FFH is referred to as Page 0.
It supports byte and word I/O ports, has 64K independent byte-wide I/O ports and 32K
independent aligned word-wide I/O ports. There are various communication devices like the
keyboard, mouse, printer, etc. So, we need to interface the keyboard and other devices with the
microprocessor by using latches and buffers.

Advantages of Isolated I/O


1. Complete memory address space available for use by memory
2. Special instructions have been provided in the instruction set of the 8086 to perform
isolated I/O operation. This instructions tailored to maximize performance.

Disadvantage of Isolated I/O


1. All inputs/outputs must take place between an I/O port and accumulator (AL or AX)
register.
2. The data transferred between I/O and the microprocessor must be accessed by the IN,
INS, OUT, and OUTS instructions.

Separate control signals for the I/O space are developed (using M/IO ̅̅̅ and W/R ̅), which indicate
̅̅̅̅̅̅̅) or an I/O write (IOWC
an I/O read (IORC ̅̅̅̅̅̅̅̅) operation. These signals indicate that an I/O port
address, which appears on the address bus, is used to select the I/O device.

In the personal computer, isolated I/O ports are used for controlling peripheral devices. An 8-bit
port address is used to access devices located on the system board, such as the timer and keyboard
interface, while a 16-bit port is used to access serial and parallel ports as well as video and disk
drive systems.

8086 has special instructions IN and OUT to transfer data through the input/output ports in I/O
mapped I/O system. The IN instruction copies data from a port to the Accumulator. If an 8-bit
port is read data will go to AL and if 16-bit port is read the data will go to AX. The OUT instruction
copies a byte from AL or a word from AX to the specified port. The M/IO signal is always low
when 8086 is executing these instructions. In this address of I/O device is 8-bit or 16-bit. It is 8-
bit for Direct addressing and 16-bit for Indirect addressing.

 Memory-Mapped I/O.
In the memory-mapped I/O scheme, any instruction that references memory can accomplish the
transfer. It is a part of the memory address space is dedicated to I/O devices. The PC does not
use memory-mapped I/O.

Unlike isolated I/O, memory-mapped I/O does not use the IN, INS, OUT, or OUTS instructions.
Instead, it uses any instruction that transfers data between the microprocessor and memory. A
memory-mapped I/O device is treated as a memory location in the memory map.

In this type of I/O interfacing, the 8086 uses 20 address lines to identify an I/O device. The I/O
device is connected as if it is a memory device. The 8086 uses same control signals and instructions
to access I/O as those of memory, here RD and WR signals are activated indicating memory bus
cycle.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 4


Figure 3. Memory-mapped I/O for the memory and I/O maps
for the 8086/8088 microprocessors.

Advantages of Memory-Mapped I/O


1. Any memory transfer instruction can be used to access the I/O device.
̅̅̅̅̅̅̅) and (IOWC
2. The (IORC ̅̅̅̅̅̅̅̅) signals have no function in a memory-mapped I/O system and
may reduce the amount of circuitry required for decoding.
3. Instructions that affect data in memory (MOV, ADD, AND, etc.) can be used to perform
I/O operations.
4. I/O transfers can take place between I/O port and any of the registers.

Disadvantage of Memory-Mapped I/O


1. A portion of the memory system is used as the I/O map. This reduces the amount of
memory available to applications.
2. Memory instructions perform slower.
3. Part of the memory address space cannot be used to implement memory.

When we are executing any instruction, we need the microprocessor to access the memory for
reading instruction codes and the data stored in the memory. For this, both the memory and the
microprocessor requires some signals to read from and write to registers. The interfacing process
includes some key factors to match with the memory requirements and microprocessor signals.
The interfacing circuit therefore should be designed in such a way that it matches the memory
signal requirements with the signals of the microprocessor.

Differences between Isolated I/O and Memory Mapped I/O


ISOLATED I/O MEMORY MAPPED I/O
Memory mapped I/O uses memory from the
Isolated I/O uses separate memory space.
main memory.
Limited instructions can be used. Those are IN, Any instruction which references to memory can
OUT, INS, OUTS. be used. (MOV, AND XCHG, SUB, etc.).
Faster because I/O instructions is specifically Slower because memory instructions execute
designed to run faster than memory instructions. slower than the special I/O instructions.
The memory address space is not affected. Part of the memory address space is lost.
The addresses for Isolated I/O devices are called Memory mapped I/O devices are treated as
ports. memory locations on the memory map.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 5


WAYS OF COMMUNICATION
There are two ways of communication in which the microprocessor can connect with the outside world.
 Serial Communication Interface − In this type of communication, the interface gets a single
byte of data from the microprocessor and sends it bit by bit to the other system serially and vice-
a-versa.

 Parallel Communication Interface − In this type of communication, the interface gets a byte of
data from the microprocessor and sends it bit by bit to the other systems in simultaneous (or)
parallel fashion and vice-a-versa.

HANDSHAKING
Many I/O devices accept or release information at a much slower rate than the microprocessor. Another
method of I/O control, called handshaking or polling, synchronizes the I/O device with the
microprocessor. An example of a device that requires handshaking is a parallel printer that prints a few
hundred characters per second (CPS). It is obvious that the microprocessor can send more than a few
hundred CPS to the printer, so a way to slow the microprocessor down to match speeds with the printer
must be developed.

INTERFACING I/O PORTS

I/O ports or input/output ports are the devices through which the microprocessor communicates with
other devices or external data sources/destinations. Input activity, as one may expect, is the activity that
enables the microprocessor to read data from external devices, for example keyboard, joysticks, mouser
etc. the devices are known as input devices as they feed data into a microprocessor system.

Output activity transfers data from the microprocessor top the external devices, for example CRT display,
7-segment displays, printer, etc., the devices that accept the data from a microprocessor system are called
output devices.

Steps in Interfacing an I/O Device


The following steps are performed to interface a general I/O device with a CPU:
1. Connect the data bus of the microprocessor system with the data bus of the I/O port.
2. Derive a device address pulse by decoding the required address of the device and use it as the
chip select of the device.
3. Use a suitable control signal, i.e., IORD and /or IOWR to carry out device operations, i.e. connect
IORD to RD input of the device if it is an input devise, otherwise connect IOWR to WR input
of the device. In some cases, the RD or WR control signals are combined with the device address
pulse to generate the device select pulse.

INPUT PORT
The input device is connected to the microprocessor through buffer. The simplest form of an input port
is a buffer as shown in the Figure 4. This buffer is a tri-state buffer and its output is available only when
enable signal is active. When microprocessor wants to read data from the input device (keyboard), the
control signals from the microprocessor activates the buffer by asserting enable input of the buffer. Once
the buffer is enabled, data from the device is available on the data bus. Microprocessor reads this data by
initiating read command.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 6


Figure 4. Buffer

OUTPUT PORT
It is used to send the data to the output device such as display from the microprocessor. The simplest form
of the output port is a latch.

The output device is connected to the microprocessor through latch as shown in the Figure 5. When
microprocessor wants to send data to the output device it puts the data on the data bus and activates the
clock signal of the latch, latching the data from the data bus at the output of latch. It is then available at
the output of latch for the output device.

Figure 5. Latch

PIC MICROCONTROLLER
A PIC microcontroller is a single integrated circuit small enough to fit in the palm of a hand. ‘Traditional’
microprocessor circuits contain four or five separate integrated circuits - the microprocessor (CPU) itself,
an EPROM program memory chip, some RAM memory and an input/output interface. With PIC
microcontrollers all these functions are included within one single package, making them cost effective and
easy to use.

PIC microcontrollers can be used as the ‘brain’ to control a large variety of products. In order to control
devices, it is necessary to interface (or ‘connect’) them to the PIC microcontroller.

PIC is a family of microcontrollers made by Microchip Technology, originally developed by General


Instrument's Microelectronics Division. The name PIC initially referred to Peripheral Interface
Controller and is currently expanded as Programmable Intelligent Computer.
The PIC was originally intended to be used with the General Instrument CP1600, the first commercially
available single-chip 16-bit microprocessor.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 7


PIC MICROCONTROLLER
A PIC microcontroller is a single integrated circuit small enough to fit in the palm of a hand. ‘Traditional’
microprocessor circuits contain four or five separate integrated circuits - the microprocessor (CPU) itself,
an EPROM program memory chip, some RAM memory and an input/output interface. With PIC
microcontrollers all these functions are included within one single package, making them cost effective and
easy to use.

PIC microcontrollers can be used as the ‘brain’ to control a large variety of products. In order to control
devices, it is necessary to interface (or ‘connect’) them to the PIC microcontroller.

PIC is a family of microcontrollers made by Microchip Technology, originally developed by General


Instrument's Microelectronics Division. The name PIC initially referred to Peripheral Interface
Controller and is currently expanded as Programmable Intelligent Computer.
The PIC was originally intended to be used with the General Instrument CP1600, the first commercially
available single-chip 16-bit microprocessor.

Figure 6. PIC16F84 PIC Microcontroller

INTERFACING TO THE PIC MICROCONTROLLER


There are many different input and output devices to interface with the PIC microcontroller.

BASIC code examples are provided for users of the Basic Stamp or PICAXE systems. Explanations of
BASIC commands are provided in the Commands section (available separately).

The interfacing circuits can also be used with any PIC microcontrollers such as the PIC16F84, although
these microcontrollers may require programming in assembler code.

This interfacing is split into four subsections:


• Introduction to ‘standard’ interfacing circuits
• Output Device Interfacing
• Input Device Interfacing
• Advanced Component Interfacing

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 8


Note on the BASIC Code Samples
Simple BASIC code examples are provided within each subsection. The samples are not ‘complete’
programs but sections of code that can be included within a main program when using that particular
component. When using these code samples it must be remembered that:
1. Each pin should be set up as an input or output before using the code (stamp users only).
2. If the hardware pins are changed from those given in the circuit diagrams it will be necessary to
modify the pin numbers in the code.
3. Any let dirs = or let pins = commands will adjust all 8 pins, in the port.
4. Try to keep variables independent of each other. If a sub-procedure uses a variable, do not use the
same variable anywhere else in the code. If the same variable must be used again, make sure there
is no way it can clash with any other part of the code. This is the most common way of adding
‘hard-to-find’ bugs into software code.

Note on Component Selection


For convenience and ease of understanding, a single device has been adopted when using standard
interfacing components such as transistors and MOSFETS. For instance, the ‘standard’ transistor selected
is the darlington device BCX38B. This does not mean that this device is the only transistor that can be
used in all the transistor circuits, as it is not, but it is chosen because it is suitable for the majority of project
work applications. All components listed are common devices that can be purchased from almost all
electronics distributors.

INTRODUCTION TO ‘STANDARD’ INTERFACING CIRCUITS


 Standard Circuits 1 – The Transistor Interfacing Circuit
Many output devices will require a transistor
switching circuit. In most cases a Darlington pair
formed from two transistors is ideal.

However, this circuit requires that two separate


transistors are used. It is possible to buy a device that
contains the two transistors in a single package. This
transistor is called the BCX38B and can switch
currents up to 800mA.

Note that it is usual to connect a back emf


suppression diode across the output device. This is
essential with devices such as relays, solenoids and
motors which create a back emf when power is
switched off. The diode type 1N4001 is the device
recommended.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 9


 Standard Circuits 2 – Using a Darlington Driver IC
If a number of output devices are being controlled, it
may be necessary to use a number of output
transistors. In this case it will often be more
convenient to use a ULN2003 Darlington driver IC.
This is simply a 16 pin ‘chip’ that contains 7
darlington transistors similar in value to the BCX38B.
The ‘chip’ also contains internal back emf
suppression diodes and so no external 1N4001 diodes
are required.

A device called the ULN2803 Darlington Driver IC


is also available. This is identical to the ULN2003
except that it is an 18 pin device and contains 8
darlington pairs instead of 7. If it is necessary to pass
relatively high currents through a device, it can be
useful to ‘pair up’ drivers as shown with this circuit.

A ULN2803 darlington driver is supplied prefitted to


the PICAXE interface board.

 Standard Circuits 3 – The Relay Interfacing Circuit


A relay can be used to switch higher power devices
such as motors and solenoids. If desired, the relay can
be powered by a separate power supply, so, for
instance, 12V solenoids can be controlled by the
microcontroller. Note the use of a back emf
suppression diode across the relay contacts. This is to
prevent damage to the transistor when the relay
switches off. Diode type 1N4001 is suitable for this
diode.

 Standard Circuits 4 – The Power MOSFET Interfacing Circuit


Power MOSFETs can be used instead of darlington
transistor pairs to switch medium power devices. The
standard MOSFET circuit is shown below. The
device IRF530 is a suitable power MOSFET to use in
this circuit.

Note that it is usual to connect a back emf


suppression diode across the output device. This is
essential with devices such as relays, solenoids and
motors which create a back emf when power is
switched off. The diode type 1N4001 is the device
recommended.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 10


OUTPUT DEVICE INTERFACING
 Output Device 1 – Light Emitting Diode (LEDs)
The PIC Microcontroller can sink (“absorb”) or
source (“give out”) a small amount of current,
which means that an LED can be connected
directly to the output pin. A series resistor (value
330R) is also required to limit the current.

Bi-colour LEDs often contain both green and red LEDs connected in ‘inverse parallel’. This means
if current flows one way through the device the LED lights green, and if current flows the other
way the LED lights red. Therefore, by using the sink/source capabilities of the PIC
Microcontroller it is possible to light the LED in both colours.

 Output Device 2 – Signal Lamp


To interface a signal lamp the standard transistor interfacing circuit is used. Note that if a different
power supply is used for the signal lamp, the 0V rails of each power supply must be connected to
provide a common reference. If a battery is used as the power supply, it is worth remembering
that LEDs draw much less current than lamps. Therefore, if a simple ‘indicator’ is required, a LED
will be a better solution than a lamp as the batteries will last far longer.

To switch on Lamp - high 1


To switch off Lamp - low 1

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 11


 Output Device 3 – Buzzer
To interface a buzzer the standard transistor interfacing circuit is used. Note that if a different
power supply is used for the buzzer, the 0V rails of each power supply must be connected to
provide a common reference.

If a battery is used as the power supply, it is worth remembering that piezo sounders draw much
less current than buzzers. Buzzers also just have one ‘tone’, whereas a piezo sounder is able to
create sounds of many different tones.

To switch on buzzer - high 1


To switch off buzzer - low 1

 Output Device 4 – Piezo Sounder & Speaker


A piezo sounder or speaker can be used to produce many different sounds, whereas a buzzer can
only produce a single tone. Buzzers produce a noise when power is applied, but a piezo or speaker
requires a pulsed signal to generate the noise. Fortunately, this is very easy to generate from the
microcontroller by using the BASIC ‘sound’ command.

To produce a note of pitch 100, length 50 on pin 1


sound 1, (100,50)

To produce a varying noise using variable b1


for b1 = 1 to 100
sound 1, (b1,25)
next b1

 Output Device 5 – Solar & DC “Toy” Motors


Many projects require the use of a cheap dc motor to create rotational movement. There are a
number of ways motors can be interfaced to the microcontroller.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 12


This circuit uses a darlington transistor to switch the motor on and off. This circuit will work with
‘solar’ motors but may not function correctly with cheap dc ‘toy’ motors. This is because this type
of motor introduces a lot of electrical ‘noise’ on to the power rails. This noise can affect the
microcontroller, and in some cases can completely stop the control program functioning.

Electrical noise can be reduced by soldering


suppression capacitors across the motor contacts,
as shown. Use a 220nF polyester (non-polarised)
capacitor.

In order to switch medium power motors, a power


MOSFET is used instead of a darlington transistor.
The MOSFET circuit is shown. The device IRF530
is a suitable power MOSFET to use in this circuit.

On many occasions it may be necessary to


control two motors. A convenient and cheap
approach would be to use a motor driver IC
such as the L293D. This IC will allow control
of two dc motors, using four data lines from
the microcontroller. Naturally, if only one
motor is to be controlled then only two output
lines are used.

Both inputs low - motor halt


First output high, second output low - motor forward
First output low, second output high - motor reverse
Both inputs high - motor halt

Changing the states of the input pins has the effect of altering the direction of current flow through
the motor, as shown below.

Note that the L293D will become warm with continuous use. A heatsink bonded onto the top of
the chip will help keep it cool.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 13


One way to prevent electrical noise affecting the microcontroller is to use separate power supplies
for the ‘control’ electronics and the motor. For example, a PP3 battery may be chosen to power
the microcontroller and 4xAA cells to power the motors. Naturally it will be necessary to ‘link’ the
two circuits so that the motor can be controlled. A relay is an ideal component to do this.

The above circuit will only switch the motor on and off. If the motor is required to run in both
directions (forwards and reverse), two relays can be used as shown.

 Output Device 6 – Unipolar Stepper Motor


Stepper motors are very accurate motors that are commonly used in computer disk drives, printers
and clocks. Unlike dc motors, which spin round freely when power is applied, stepper motors
require that their power supply be continuously pulsed in specific patterns. For each pulse, the
stepper motor moves around one ‘step’, often 7.5 degrees (giving 48 steps in a full revolution).
There are two main types of stepper motors - Unipolar and Bipolar. Unipolar motors usually have
four coils which are switched on and off in a particular sequence. Bipolar motors have two coils
in which the current flow is reversed in a similar sequence. Use of bipolar motors is covered in the
next section.
Each of the four coils in a unipolar stepper motor must be switched on and off in a certain order
to make the motor turn. Many microprocessor systems use four output lines to control the stepper
motor, each output line controlling the power to one of the coils.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 14


As the stepper motor operates at 12V, the standard transistor circuit is required to switch each
coil. As the coils create a back emf when switched off, a suppression diode on each coil is also
required. The table below show the four different steps required to make the motor turn.

Look carefully at the table, and notice that a pattern is visible. Coil 2 is always the opposite (or
logical NOT) of coil 1. The same applies for coils 3 and 4. It is therefore possible to cut down the
number of microcontroller pins required to just two by the use of two additional NOT gates.

Fortunately, the darlington driver IC ULN2003 can be used to provide both the NOT and
darlington driver circuits. It also contains the back emf suppression diodes, so no external diodes
are required. The complete circuit is shown below.

N.B. colours of stepper motor leads may vary

Before programming, there is another pattern to


notice in the stepping sequence. Look at the table,
which just shows coil 1 and coil 3.

Notice the change from step 1 to step 2, just coil 3


changes. Then look at the next change - just coil 1
changes. In fact, the two coils take it ‘in turns’ to
change from high to low and back again. This high-
low-high changing can be described as ‘toggling’
state. This makes the programming very simple by
using the BASIC toggle command.

steps:
toggle 1 ‘ Toggle pin 1
pause 200 ‘ Wait 200 ms
toggle 2 ‘ Toggle pin 2
pause 200 ‘ Wait 200ms
goto steps ‘ Loop

Note: If stepper motor ‘wobbles’, try adjusting wire polarity

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 15


 Output Device 7 – Bipolar Stepper Motor
Stepper motors are very accurate motors that are commonly used in computer disk drives, printers
and clocks. Unlike dc motors, which spin round freely when power is applied, stepper motors
require that their power supply be continuously pulsed in specific patterns. For each pulse, the
stepper motor moves around one ‘step’, often 7.5 degrees (giving 48 steps in a full revolution).

There are two main types of stepper motors - Unipolar and Bipolar. Unipolar motors usually have
four coils which are switched on and off in a particular sequence. Bipolar motors have two coils
in which the current flow is reversed in a similar sequence. Use of unipolar motors is covered in
the previous pages.

The bipolar stepper motor has two coils that must be controlled so that the current flows in
different directions through the coils in a certain order. The changing magnetic fields that these
coils create cause the rotor of the motor to move around in steps. The circuit that is normally used
to control one of the coils is shown below. Notice how there are four ‘control’ transistors, that are
switched on in ‘pairs’. Therefore, with two coils there are four control transistor pairs (Q1-Q4)
which must be switched on and off in a certain sequence.

Notice that as the coils create a back emf when switched off 8 suppression diodes (4 on each coil)
are also required.

The table below show the four different steps


required to make the motor turn.

Fortunately, the motor driver L293D has been


specifically designed to provide this transistor
switching circuit. The L293D contains all 8
transistors and diodes within one 16 pin
package.

Four pins from the microcontroller


are connected to the four transistor
‘pairs’ via IC pins 2, 7, 10 and 15.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 16


This sample procedure makes the motor spin 100 steps to the left and then 100 steps to the right
by using two sub-procedures. lstep causes the motor to move one step to the left, rstep causes
the motor to move one step to the right. Variable b1 is used to store the step position and so
should not be used elsewhere in the program.

main: for b3 = 0 to 99 ‘ start a for...next loop


gosub lstep ‘ call left step sub-procedure
next b3 ‘ next loop
for b3 = 0 to 99 ‘ start a for...next loop
gosub rstep ‘ call left step sub-procedure
next b3 ‘ next loop

lstep: let b1 = b1 + 1 ‘ add 1 to variable b1


goto step ‘ goto the lookup table
rstep: let b1 = b1 - 1 ‘ subtract 1 from variable b1
step: let b1 = b1 & 2 ‘ mask lower two bits of b1
lookup b1,(%1010,%1001,%0101,%0110),b2 ‘ lookup code into b2
let pins = b2 ‘ output b2 onto control lines
return

 Output Device 8 – Radio Control Servo


Servos are used in most radio controlled cars and planes to control the
steering mechanism. They are accurate devices that always rotate the
same amount for a given signal, and so are ideal for use in many
automated machines.

A typical servo has just three connection wires, normally red, black and
white (or yellow). The red wire is the 5V supply, the black wire is the 0V
supply, and the white or yellow) wire is for the positioning signal.

The positioning signal is a pulse between 0.75


and 2.25 milliseconds (ms) long, repeated about
every 18ms (so there are roughly 50 pulses per
second). With a 0.75ms pulse the servo moves to
one end of its range, and with a 2.25ms pulse the
servo moves to the other. Therefore, with a
1.5ms pulse, the servo will move to the central
position. If the pulses are stopped the servo will
move freely to any position.

Unfortunately, servos require a large current (up to 1A) and also introduce a large amount of noise
on to the power rail. Therefore, in most cases the servo should be powered from a separate power
supply, as shown below. Remember that when using two power supplies the two 0V rails must be
joined to provide a common reference point.
loop: servo 4,75 ‘ move servo to one end
pause 2000 ‘ wait 2 seconds
servo 4,150 ‘ move servo to centre
pause 2000 ‘ wait 2 seconds
servo 4,225 ‘ move servo to other end
pause 2000 ‘ wait 2 seconds
goto loop ‘ loop back to start

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 17


 Output Device 9 – Counter Module
The Counter Module is a numeric LCD display module that can be used to show a ‘counter’ value.
To increment the counter a pulse (between 1 and 1.5V) must be applied to the counter pad 3. As
the PIC microcontroller operates at 5V a potential divider formed from resistors must be used to
reduce the PIC microcontroller output signal to 1.5V. As the counter uses its own, internal, 1.5V
battery, the two 0V rails must also be connected.

To increment counter: pulsout 1,100


To reset the counter, a second potential divider is added and connected to pin 2.

 Output Device 10 – Seven Segment Display


A seven segment display contains seven LED ‘bars’ that can be lit up in
different combinations to show the ten digits 0 to 9. In theory each ‘bar’
could be connected to one microcontroller output pin, but this would
use up 7 of the 8 available pins!

A better solution is to use a dedicated integrated circuit, such as the CMOS 4511B to control the
seven segment display. This IC controls the seven segment display according to the binary ‘code’
on the four data lines. This system uses four pins rather than 7.

IMPORTANT NOTE - Seven segment displays


are available in two types, called ‘common cathode’
and ‘common anode’. The following circuits will
only work with a ‘common cathode’ type display.
Use the manufacturer’s datasheet to determine the
pinout arrangement of the LED bars.

This code example counts through the digits 0 to 9.


main: for b1 = 0 to 9 ‘ Set up a for...next loop using variable b1
let pins=b1 ‘ Output b1 onto the four data lines
pause 1000 ‘ Pause 1 second
next b1 ‘ Next
goto main ‘ Loop back to start

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 18


Another possible solution is to use the CMOS 4026B to control the seven segment display. This
system uses just two pins to control the display. The reset pin is used to reset the display to 0, the
clock pin is then used to increment the digit up from 0. This means to display the digit ‘4’ it is
necessary to reset and then pulse the clock line 4 times. In reality this means that the display shows
the digits 0-1-2-3-4, but, as they are clocked extremely rapidly, the human eye cannot see the
changes, and so the number ‘4’ seems to appear immediately!

This code example uses sub-procedure ‘clock’ to display the digit ‘4’, which is stored in the variable
b1.

This is the sub-procedure:


clock: pulsout 0,10 ‘ reset display to 0
if b1 = 0 goto endclk ‘ if b1 = 0 then return
for b3 = 1 to b1 ‘ start a for...next loop
pulsout 1,10 ‘ pulse clock line
next b3 ‘ next loop
endclk: return ‘ return from sub-procedure

This is the main code:


main: let b1 = 4 ‘ give variable b1 the value 4
gosub clock ‘ call sub-procedure
pause 1000 ‘ wait 1 second
goto main ‘ loop

This system can be expanded to two digits by adding a second 4026B IC and a second seven
segment display, as shown in the diagram below. No changes to the code are required, just give
the variable b1 a value between 0 and 99 and the number will be displayed on the two displays
when sub-procedure ‘clock’ is called.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 19


 Output Device 11 – Solenoid & Solenoid Valves
A solenoid consists of a steel plunger inside an electric coil which
is wrapped around a tube. When the coil is energised a magnetic
field is created, and this draws the plunger into the tube. When the
coil is de-energised a spring pushes the plunger back out of the
tube.

To control a solenoid the standard MOSFET circuit is used.

The isonic solenoid valve can be used to control air flow through a pneumatic system. Isonic valves
are ideal for battery operated products as operate at a low voltage and draw much less current than
traditional solenoid valves. The standard transistor switching circuit can be used to drive the isonic
valve.

To switch the solenoid on - high 1


To switch the solenoid off - low 1

 Output Device 12 – Smart Wire & Smart Springs


Shape Memory Alloy wire or springs are ‘smart’ materials that can be used to create mechanical
actuation (movement). When an electric current is passed through the wire it heats up and so
contracts with a large pulling force. When the current is removed the wire cools and so expands
again (a ‘traditional’ steel spring is sometimes used to pull the smart wire/spring taut as it cools).

Smart wire or springs draw a relatively large current, and so the standard FET interfacing circuit
should be used to interface to the microcontroller.

To make the wire / spring contract - high 1


To allow the wire / spring to expand again - low 1

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 20


INPUT DEVICE INTERFACING
 Input Device 1 – Switches
There are a large variety of switches available, but the majority all have two ‘contacts’ which are
either ‘open’ (off) or ‘closed’ (on). The two circuits shown below can be used with almost all
switches.

With this circuit the input pin is low when the switch is open and high when the switch is closed.

Goto ‘jump’ when switch is open: if pin0 = 1 then jump

Goto ‘jump’ when switch is closed: if pin0 = 0 then jump

Switch Bounce
All mechanical switches ‘bounce’ when the switch opens or
closes. This means that the switch contacts ‘bounce’ against
each other before settling. As the PIC microcontroller
operates so quickly it is possible that in some programs the
microcontroller may register 2 or 3 of these ‘bounces’ instead
of just registering one ‘push’.

The simplest way to debounce a circuit is to simply add a time


delay (pause 100) after the if... command. If the section of
code after the push is quite long this time delay will occur
naturally (as the other code commands are carried out) and so
is unnecessary. However, if the code does not have a long
delay, as in the following example, a pause command can be
used instead.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 21


The following two programs show the effect of switch bouncing. The program should light the
LED on pin1 when the switch connected to pin0 has been pressed more than 5 times. However,
the first listing may not work correctly, because the microcontroller may count ‘bounces’ rather
than actual pushes, and so the LED may light prematurely.

init: let b0 = 0 init: let b0 = 0

main: if pin 1 = 1 then add main: if pin 1 = 1 then add


goto main goto main

add: let b0 = b0 + 1 add: pause 100 ‘short delay here


if b0 < 5 then main let b0 = b0 + 1
high 1 if b0 < 5 then main
goto main high 1
goto main

 Input Device 2 – Potentiometer


A potentiometer (or ‘variable resistor’) has a spindle that can be moved to change the resistance
value of the potentiometer. This can be used to measure rotational or linear movement.

The readADC command is used to measure the value of the resistance by carrying out an Analogue
to Digital Conversion. The value of the resistance is given a ‘value’ between 0 and 255 which is
then stored in a variable. After storing the reading in the variable, the if...then command can be
used to perform different functions.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 22


The program below lights three different LEDs (connected to pins 1, 2 and 3), depending on the
analogue sensor reading.

main: readadc 0,b1 ‘ read value on pin0 into variable b1


if b1<75 then light1 ‘ if b1 is less than 75 then light 1
if b1<175 then light2 ‘ if b1 is less than 175 then light 2
goto light3 ‘ if b1 is greater than 175 then light 3

light1: high 1 ‘ switch on LED 1


low 2 ‘ switch off LED 2
low 3 ‘ switch off LED 3
goto main ‘ loop

light2: low 1 ‘ switch off LED 1


high 2 ‘ switch on LED 2
low 3 ‘ switch off LED 3
goto main ‘ loop

light3: low 1 ‘ switch off LED 1


low 2 ‘ switch off LED 2
high 3 ‘ switch on LED 3
goto main ‘ loop

 Input Device 3 – Light Dependant Resistor (LDR)


A Light Dependant Resistor (LDR) is a resistor that changes in value according to the light falling
on it. A commonly used device, the ORP-12, has a high resistance in the dark, and a low resistance
in the light. Connecting the LDR to the microcontroller is very straight forward, but some software
‘calibrating’ is required.

It should be remembered that the LDR response is not linear, and so the readings will not change
in exactly the same way as with a potentiometer. In general there is a larger resistance change at
brighter light levels. This can be compensated for in the software by using a smaller range at darker
light levels. Experiment to find the most appropriate settings for the circuit.

main: readadc 0,b1 ‘ read the value


if b1<50 then light1 ‘ range 0-50 = 50
if b1<100 then light2 ‘ range 50-100 = 50
if b1<145 then light3 ‘ range 100-145 = 45
if b1<175 then light4 ‘ range 145-175 = 30
goto main

NOTE: An analogue calibration board is available that will display exact sensor values. Sensors are connected to the board for
testing purposes. Readings can be taken in different conditions to work out the best value ranges to use in a program. To find out
more about the analogue calibration board, visit the Educational Systems pages at [Link].

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 23


 Input Device 4 – Thermistor
A thermistor is a resistor that changes in value according to its heat. In actual fact all resistors
change in value as they heat up or cool down, but thermistors are manufactured to show a large
resistance change. Connecting the thermistor to the microcontroller is very straight forward, but
some software ‘calibrating’ is required.

It should be remembered that the thermistor response is not linear, and so the readings will not
change in exactly the same way as with a potentiometer. In general there is a larger resistance
change at lower temperatures. This can be compensated for in the software by using a smaller
range at higher temperatures. Experiment to find the most appropriate settings for the circuit.

main: readadc 0,b1 ‘ read the value


if b1<50 then light1 ‘ range 0-50 = 50
if b1<100 then light2 ‘ range 50-100 = 50
if b1<145 then light3 ‘ range 100-145 = 45
if b1<175 then light4 ‘ range 145-175 = 30
goto main

ADVANCED COMPONENT INTERFACING


 Advanced Interfacing 1 – LCD Display
A Liquid Crystal Display is an electronic device that can
be used to show numbers or text. There are two main
types of LCD display, numeric displays (used in watches,
calculators etc) and alphanumeric text displays (often used
in devices such as photocopiers and mobile telephones).
The display is made up of a number of shaped ‘crystals’. In numeric displays these crystals are
shaped into ‘bars’, and in alphanumeric displays the crystals are simply arranged into patterns of
‘dots’. Each crystal has an individual electrical connection so that each crystal can be controlled
independently. When the crystal is ‘off’ (i.e. when no current is passed through the crystal) the
crystal reflect the same amount of light as the background material, and so the crystals cannot be
seen. However, when the crystal has an electric current passed through it, it changes shape and so
absorbs more light. This makes the crystal appear darker to the human eye - and so the shape of
the dot or bar can be seen against the background.
It is important to realize the difference between an LCD display and an LED display. An LED
display (often used in clock radios) is made up of a number of LEDs which actually give off light
(and so can be seen in the dark). An LCD display only reflects light, and so cannot be seen in the
dark.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 24


Connecting The LCD (OPTION 1)
The serial LCD firmware is used to allow serial control of an alphanumeric LCD. This allows
microcontrollers (and microcontroller based systems such as the PICAXE or Basic Stamp) to
visually output user instructions or readings onto a text screen without the need for a host
computer. This is especially useful when working, for example, with analogue sensors, as the
analogue reading can easily be displayed on the LCD module. All LCD commands are transmitted
serially via a single microcontroller pin. A sample instruction, using the serout command is as
follows:
to print the text ‘Hello’ the instruction is simply

serout 7,T2400,(“Hello”)

For more information, see the Serial LCD Firmware datasheet at [Link]

Connecting The LCD (OPTION 2)


The LCD has 6 lines that can be
connected directly to the PIC
microcontroller pins. However it
is a good design practice to add a
low value resistor (e.g. 330R) on
the lines to protect against static
discharges. The 10k
potentiometer connected to pin 3
is used to adjust the contrast of
the display. All unused lines
should be tied to ground as
shown.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 25


A Simple LCD Program
The following program will print out the phrase ‘Hello there!’ on two lines of the LCD display. It
uses three sub-procedures called init, wrins and wrchr. These three subprocedures carry out all
the ‘difficult’ software tasks, and are ‘standard’ sub-procedures that will not have to be changed.
In fact they can be used without understanding how they work, but it is necessary to know what
they do:

init ‘initialises’ the LCD so that it is ready to accept instructions


wrins sends an instruction stored in variable b1 to the LCD module
wrchr sends a character stored in variable b1 to be ‘printed’ on the LCD screen

The three sub-procedures are explained further in the following sections.

EEPROM 0,(“Hellothere!”) ‘ store the text in the EEPROM memory

gosub init ‘ initialise LCD

main: let b1 = 1 ‘ set b1 to ‘clear display’ instruction


gosub wrins ‘ send instruction to LCD

for b3 = 0 to 4 ‘ setup for...next loop (“Hello” - positions 0 to 4)


read b3, b1 ‘ read letter from EEPROM into variable b1
gosub wrchr ‘ send character to LCD
next b3 ‘ next loop

let b1 = 192 ‘ set b1 to ‘start of second line’ position


gosub wrins ‘ send instruction to LCD

for b3 = 5 to 11 ‘ setup for...next loop (“there!”-positions 5 to 11)


read b3, b1 ‘ read letter from EEPROM memory into variable b1
gosub wrchr ‘ send character to LCD
next b3 ‘ next loop

 Advanced Interfacing 2 – Serial Interfacing to a Computer


A Most computers can ‘talk’ to other devices by serial communication. Serial communication uses
a common ‘protocol’ (or code) where characters are converted into numbers and then transmitted
via cables. A computer mouse normally ‘communicates’ serially with a computer, and computer
modems work by turning these numbers into sounds to travel down telephone lines.

As all computers use the same ASCII code for transmitting and receiving characters it is relatively
easy to program the PIC microcontroller to ‘talk’ to any type of computer. All that is needed is a
suitable cable and some very simple electronic circuits.

Connecting to the Computer


The system we will use requires just three wires between the computer and the microcontroller.
The ground wire provides a common reference, the RX wire sends signals from the computer to
the PIC microcontroller, and the TX wire sends signals from the PIC microcontroller to the
computer.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 26


The best way to make a serial cable is to buy a serial ‘extension’ cable and cut it in half. This will
give two cables with a suitable connector at each end. The diagrams below show the various wiring
connections required.

Computer Communication Software


To use this system a communication software package is required for the PC. The examples below
use the Terminal option within the Programming Editor software, but any communications
package can be used.

There are various different protocols that can be used for serial communication, and it is important
that both the computer and the microcontroller use the same setting. The 2400,N,8,1 protocol is
used here, which means baud speed 2400, no parity, 8 data bits and one stop bit. This baud speed
is quite slow by modern standards, but is quite sufficient for the majority of project work tasks.
All ‘handshaking’ (hardware or software) must also be disabled.

References
 The Intel Microprocessors 8th Edition by Barry B. Brey
 Microprocessors and Interfacing 1st Edition by Godse
 Introduction to Microprocessors and Microcontroller 2nd Edition by John Crisp
 Microprocessors and Interfacing Devices by Mr.R Mahendhar Reddy
 Computer Organization and Architecture Designing for Performance by William Stallings
 Computer Organization and Architecture/Introduction to Computer Organization and
Architecture by Dr. Manmohan Sharma
 Computer System Architecture 3rd Edition by M. Morris Mano
 Computer Architecture: A Quantitative Approach 4th Ed by D.A. Patterson, and J.L.
 Computer Architecture Computer Organization and Design 5th Edition 2014
 Computer Organization and Design – The Hardware/Software Interface, David A. Patterson
and John L. Hennessy, [Link], Morgan Kaufmann, Elsevier, 2009.

Created by: CPE323 SECOND SEMESTER USTP INSTRUCTORS 27

You might also like