Interfacing of Devices to Microcontroller
Prof Prabhat Ranjan
(
[email protected])
Dhirubhai Ambani Institute of Information and
Communication Technology, Gandhinagar
Acknowledgment
Based on a document Microcontroller
Interfacing Techniques available from
www.bipom.com
Wikipedia
Overview
Micro-controllers are useful to the extent that
they communicate with other devices, such as
sensors, motors, switches, keypads, displays,
memory and even other micro-controllers
Many interface methods have been developed
over the years to solve the complex problem
of balancing circuit design criteria such as
features, cost, size, weight, power
consumption, reliability, availability,
manufacturability
Overview
Many microcontroller designs typically mix
multiple interfacing methods. In a very
simplistic form, a micro-controller system can
be viewed as a system that reads from
(monitors) inputs, performs processing and
writes to ( controls ) outputs
Digital I/O - On/OFF control and
monitoring
Advantages
Simplest interface
Lowest-cost to
implement (built into
the microcontroller)
High speed
Low programming
overhead
Disadvantages
Only on/off
control/monitoring
Short distance, few
feet maximum
Single device
control/monitoring
Digital Input Example: Reading the status of
buttons or switches
Digital Input : Keypad
Digital Output : LED display
Digital Output : Relay Control
Analog I/O : Voltage-based control
and monitoring
Simple interface
Low cost for low-
resolutions
High speed
Low programming
overhead
High cost for higher
resolutions
Not all uC have
analog I/O built-in
Complicates the
circuit design when
external ADC or
DAC are needed
Short distance, few
feet maximum
Analog I/O types
Voltage Ranges
0 to 2.5 V
0 to 4 V
0 to 5 V
+/- 2.5 V
+/- 4 V
+/- 5 V
Current Ranges
0-20 mA
4-20 mA
Analog Interface
Parallel Bus
High speed
High throughput:
Several bits are
transmitted on one
clock transition
Low cost
Large number of
microcontroller pins
needed for
implementing the
parallel bus
Consists of multiple digital inputs/outputs. Most common types:
4-bit
8-bit ( e.g. Centronics )
16-bit ( e.g. ISA )
32-bit ( e.g. PCI )
Parallel Example : 4 bit LCD
interface
8 Bit LCD Interface
Serial Buses : I
2
C (Inter Integrated
Circuit bus)
2-wire interface with one master and multiple
slaves ( multi-master configurations possible)
Originated by Philips Semiconductor in the
early 80s to connect a microcontroller to
peripheral devices in TV sets
Signals: DATA (SDA), CLOCK (SCL) and
Ground. SDA is always bi-directional; SCL is
bi-directional only in multi-master mode
Maximum allowable capacitance on the lines
is 400 pF. Typical device capacitance is 10 pF
Serial Buses : I
2
C (Inter Integrated
Circuit bus)
To start the communications, the bus master
(typically a microcontroller) places the
address of the device with which it intends to
communicate (the slave) on the bus
All slave devices monitor the bus to determine
if the master device is sending their address
Only the device with the correct address
communicates with the master
By definition, I
2
C is 5V
Multiple slave
devices - with only 3
wires
Low-cost to
implement
Implemented in
hardware or
software
Ease to implement,
many examples
Supports multi-
master configuration
Short distance
Slow speed: 100
KHz although 400
KHz and 1 MHz
slave device exist
These can not
coexist with slower
devices
Limited device
addresses
Start & Stop
An I
2
C master prepares to communicate with a
slave device first by generating a Start
condition on the bus
Start condition is defined as SDA signal going
low while SCL signal is high
Stop condition is defined as SDA going high
while SCL is high
Data Validity
Data can change while the clock is low
Data should remain stable while the clock is
going high
Acknowledge(ACK)
Writing a byte to a serial EEPROM (24C04 )
on the I2C bus:
where Device Address is defined as : P0, P1,
P2 indicate the page number ( 2Kbit pages ).
A0, A1, A2 indicate the device number on the
bus
Reading a byte from a serial EEPROM
(24C04 ) on the I2C bus ( starting from the
current address )
SPI ( Serial Peripheral Interface )
4-wire interface with one master and multiple
slaves. Signals: DATA IN, DATA OUT,
CLOCK, CS( Chip Select )
Originated by Motorola, SPI bus is a relatively
simple synchronous serial interface for
connecting low speed external devices using
minimal number of wires.
A synchronous clock shifts serial data into and
out of the microcontrollers in blocks of 8 bits.
SPI ( Serial Peripheral Interface )
SPI bus is a master/slave interface. Whenever
two devices communicate, one is referred to
as the "master" and the other as the "slave"
device
The master drives the serial clock
SPI is full duplex: Data is simultaneously
transmitted and received
Multiple slave
devices - with only
few wires
Low-cost
HW/SW Implement
Ease to implement,
many examples
Can be high speed
( e.g. 4MHz or
higher if
implemented in
hardware )
Short distance
Data and clock lines
can be shared but
each device requires
a separate Chip
Select signal, limiting
the number of
devices in limited I/O
systems
SPI bus specifies four
logic signals
SCLK Serial Clock
(output from master)
MOSI Master Output,
Slave Input (output from
master)
MISO Master Input,
Slave Output (output
from slave)
SS Slave Select
(active low; output from
master
Sometimes, the following
naming convention is
used:
SCLK Serial Clock
(output from master)
SDI Serial Data In
SDO Serial Data Out
CS Chip Select
(active low; output from
master)
SPI Bus with multiple slaves
Multimedia Card ( MMC ) Interface
using SPI
1-wire
Originated by Dallas Semiconductor ( now
part of MAXIM ) to address a variety of
peripherals, sensors, and memory chips from
a single wire interface ( DATA and Ground ).
One signal wire carries both operating power
and signal. Usually the network is built using a
wire pair where one wire carries the signal
and power and the other wire is ground.
The system is sensitive to the right timing to
operate well
Multiple slave devices-
with only 2 wires
Low-cost
Implemented in HW/SW
Ease to implement,
many examples
Relatively long
distance. Theoretically
300 meters but this is
limited in practice due
to noise and cable
capacitance
Slow speed
1-wire slave devices
typically has to come
from one source:
Dallas
Semiconductor
RS232
Asynchronous communications
Popular interface with many examples
Many compatible legacy devices
Relatively long distance, 50 feet maximum for
low baud rates although longer distances
work in practice, with low baud rates and error
correction
Immune to noise due to +/-5 Volts or higher
voltage levels for logic 0 and 1
Implemented in hardware or software
Ease to implement, many examples
More suitable for system to system
communications, not so much for chip to chip
or chip to sensor
Low speed for long distance, 115200 baud
can be achieved with small microcontrollers
using short distances
Requires transceiver chips which add to
system cost ( TTL/CMOS level RS232 can be
used without transceiver chips ).
Single master/single slave
RS485
Asynchronous communications
Popular interface
Very long distance,
thousands of feet
Immune to noise due to
differential voltage
Implemented in
hardware or software
Ease to implement
Widely used in
industrial automation
Higher speeds beyond
115200 baud
More suitable for
system to system
communications, not so
much for chip to chip or
chip to sensor
Requires transceiver
chips and twisted pair
cable with terminating
resistors which add to
system cost
RS485 Network Topology: Any station can
communicate with any other station, but not at
the same time
UART : Universal asynchronous
receiver/transmitter
A universal asynchronous receiver/transmitter
(UART) is a type of "asynchronous
receiver/transmitter", a piece of computer
hardware that translates data between parallel
and serial interfaces
Used for serial data telecommunication, a
UART converts bytes of data to and from
asynchronous start-stop bit streams
represented as binary electrical impulses
UART : Universal asynchronous
receiver/transmitter
UARTs are commonly used in conjunction
with other communication standards such as
EIA RS-232.
A UART is usually an individual (or part of an)
integrated circuit used for serial
communications over a computer or
peripheral device serial port.
UARTs are now commonly included in
microcontrollers. A dual UART or DUART
combines two UARTs into a single chip.
As of now, UARTs are commonly used with
RS-232 for embedded systems
communications.
It is useful to communicate between
microcontrollers and also with PCs.
Many chips provide UART functionality in
silicon, and low-cost chips exist to convert
logic level signals (such as TTL voltages) to
RS-232 level signals (for example, Maxim
MAX232)
Ethernet
Very high speed (10
Mbit to 100 Mbit/s )
Very long distance,
hundreds of feet can
be achieved, more
with hubs and
switches
Immune to noise
Widely used in
industrial automation
due to noise immunity
Cost
More suitable for system
to system
communications, not so
much for chip to
chip/sensor
Requires Ethernet chipset,
transformer, jack and
special cabling that add to
system cost.
Complicated to implement
High code footprint
Programming
Hardware interface
Software emulation : Bit banging
Bit banging
Bit-banging is a technique in embedded
systems for example to use serial
communications without the use of dedicated
hardware such as a UART or shift register,
instead using software to emulate their
behavior
A software routine handles the UART transmit
function by alternating a pin on the
microcontroller by given time intervals
Bit banging
A receiver function is implemented by
sampling a pin on the microcontroller by a
given time interval
With a few extra components, video signals
can be output from digital pins
Although it is not referred to as bit-banging,
software-defined radio is an extreme
extension of the same idea
Bit banging
Although it is often considered to be
something of a hack, bit-banging does allow
greater flexibility, allowing the same device to
speak different protocols with minimal or no
hardware changes required
There are some problems with bit-banging
More power is normally consumed in the
software emulation process than in dedicated
hardware
Bit banging
Another problem is that the microcontroller is
busy most of the time looking at samples or
sending a sample to the pin, instead of
performing other tasks
Yet another potential problem is that the
signal produced normally has more jitter or
glitches, especially when the microcontroller
moves on to perform other tasks.
However, if the bit-banging software is
hardware interrupt-driven by the signal, this
may be of minor importance
More Buses
USB
RS232 <-> UART
RS-422
CAN
Diff between TWI and I
2
C
FIREWIRE
CONTROLLER AREA NETWORK
(CAN)
Controller Area Network (CAN) is a broadcast,
differential serial bus standard, originally
developed in the 1980s by Robert Bosch
GmbH, for connecting electronic control units
(ECUs)
CAN was specifically designed to be robust in
electromagnetically noisy environments and
can utilize a differential balanced line like RS-
485
CAN ...
It can be even more robust against noise if
twisted pair wire is used.
Although initially created for automotive
purposes (as a vehicle bus), nowadays it is
used in many embedded control applications
(e.g., industrial) that may be subject to noise
The messages it sends are small (8 data
bytes max) but are protected by a CRC-15
(polynomial 0x62CC) that guarantees a
Hamming bit length of 6 (so up to 5 bits in a
row corrupted will be detected by any node on
the bus).
CAN ...
Bit rates up to 1 Mbit/s are possible at
networks length below 40 m. Decreasing the
bit rate allows longer network distances (e.g.
125 kbit/s at 500 m)
USB
Universal Serial Bus (USB) is a serial bus
standard to interface devices
It was originally designed for computers, but
its popularity has prompted it to also become
commonplace on video game consoles,
PDAs, portable DVD and media players,
cellphones; and even devices such as
televisions, home stereo equipment, car
stereos and portable memory devices
The radio spectrum based USB
implementation is known as Wireless USB
USB : Overview
USB was devised as a major component in
the transition towards a legacy-free PC
The intention was to let go of all older serial
and parallel ports on personal computers
since these were not properly standardized
and required a multitude of device drivers to
be developed and maintained
A USB system has an asymmetric design,
consisting of a host controller and multiple
daisy-chained devices
USB : Overview
Additional USB hubs may be included in the
chain, allowing branching into a tree structure
No more than 127 devices, including the bus
devices, may be connected to a single host
controller
Modern computers often have several host
controllers, allowing a very large number of
USB devices to be connected
USB cables do not need to be terminated