Professional Documents
Culture Documents
Microcontroller Interfacing Techniques: Document Revision: 1.01 Date: 3rd April, 2005
Microcontroller Interfacing Techniques: Document Revision: 1.01 Date: 3rd April, 2005
Interfacing Techniques
Document Revision: 1.01
Date: 3rd April, 2005
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.
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.
Microcontroller Interfaces
Digital Analog
Asynchronous Synchronous
1-wire 2-wire (I2C)
RS232/RS485 4-wire (SPI, Microwire)
Ethernet
Page 2
Digital Inputs/Outputs
Single-ended
(non-matrix) switches
P2.7
P2.6
P2.5
8051 P2.4
Microcontroller
P2.3
(AT89C51ED2)
P2.2
P2.1
P2.0
P2.6
Columns
P2.5
8051 1 2 3 A
P2.4
Microcontroller
P2.3 4 5 6 B
(AT89C51ED2)
Rows
P2.2 Rows
7 8 9 C
P2.1
P2.0
* 0 # D
Page 3
Digital Output Example: LED control
VCC
LED Interface
Current
Limiting
Resistors
8051 LED's
Microcontroller
P0.3
(AT89C51ED2)
P0.2
P0.1
P0.0
VCC
Relay Interface
8051 7407
Microcontroller
P1.4
(AT89C51ED2)
Page 4
Analog Inputs/Outputs
Disadvantages
Advantages • High cost for higher resolutions
• Simple interface • Not all microcontrollers have analog
• Low cost for low-resolutions inputs/outputs built-in
• High speed • Complicates the circuit design when external ADC
• Low programming overhead or DAC are needed.
• Short distance, few feet maximum.
• 0 to 2.5V
• 0 to 4V
• 0 to 5V
• +/- 2.5V
• +/- 4V
• +/- 5V
• 0-20mA
• 4-20mA
Analog Interface
Vcc
Amplifier
Potentiometer
4-20mA
Strain-gage
Output
Page 5
Parallel Bus
• 4-bit
• 8-bit ( e.g. Centronics )
• 16-bit ( e.g. ISA )
• 32-bit ( e.g. PCI )
Advantages Disadvantages
• High speed
• High throughput: Several bits are transmitted • Large number of microcontroller pins that
on one clock transition needed for implementing the parallel bus
• Low cost
Alphanumeric LCD
P0.7 D7
P0.6 D6
8051 P0.5 D5
P0.4 D4
Microcontroller D3
(AT89C51ED2)
D2
D1
Hello World
D0
P0.2 E
P0.1 R/W
P0.0 RS
Alphanumeric LCD
P0.7 D7
P0.6 D6
8051 P0.5 D5
P0.4 D4
Microcontroller P0.3 D3
(AT89C51ED2)
P0.2
P0.1
D2
D1
Hello World
P0.0 D0
P2.2 E
P2.1 R/W
P2.0 RS
Page 6
Serial Buses
2-wire interface with one master and multiple slaves ( multi-master configurations possible ).
Originated by Philips Semiconductor in the early 80’s 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.
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.
Advantages Disadvantages
• Multiple slave devices can be accessed with
• Short distance
only 3 wires
• Slow speed: 100 KHz although 400 KHz and 1
• Low-cost to implement
MHz slave devise exist. These can not coexist
• Implemented in hardware or software with slower devices.
• Ease to implement, many examples • Limited device addresses
• Supports multi-master configuration
2.2K 2.2K
Crystal
Page 7
Start and Stop
An I2C 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)
Page 8
Writing a byte to a serial EEPROM (24C04 ) on the I2C bus:
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 )
Example:
Page 9
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 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.
Advantages
Disadvantages
• Multiple slave devices can be accessed with
only few wires • Short distance
• Low-cost • Data and clock lines can be shared but each
• Implemented in hardware or software device requires a separate Chip Select
• Ease to implement, many examples signal, limiting the number of devices in
• Can be high speed ( e.g. 4MHz or higher if limited I/O systems
implemented in hardware )
MMC Interface
CS
P3.5
DIN
P3.7 Multimedia
8051 DOUT Card
Microcontroller P3.3
MMC
(AT89C51ED2) CLK
P3.4
Page 10
1-wire
Advantages
Multiple slave devices can be accessed with only 2 Disadvantages
wires
• Slow speed
Low-cost
• 1-wire slave devices typically has to come
Implemented in hardware or software
from one source: Dallas Semiconductor
Ease to implement, many examples
Relatively long distance. Theoretically 300 meters
but this is limited in practice due to noise and cable
capacitance
For more information on the 1-wire bus, please refer to BiPOM Application Note: Temperature
Measurements with 1-Wire Bus Sensors http://www.bipom.com/applications/ds18xx_app.pdf
Page 11
RS232
Asynchronous communications
Advantages Disadvantages
• Popular interface with many examples • More suitable for system to system
• Many compatible legacy devices communications, not so much for chip to chip
or chip to sensor
• Relatively long distance, 50 feet maximum for low
• Low speed for long distance, 115200 baud can
baud rates although longer distances work in
practice, with low baud rates and error correction be achieved with small microcontrollers using
short distances
• Immune to noise due to +/-5 Volts or higher voltage
• Requires transceiver chips which add to
levels for logic “0” and “1”
system cost ( TTL/CMOS level RS232 can be
• Implemented in hardware or software
used without transceiver chips ).
• Ease to implement, many examples
• Single master/single slave
Page 12
RS485
Asynchronous communications
Advantages Disadvantages
• Popular interface with many examples • More suitable for system to system
• Very long distance, thousands of feet communications, not so much for chip to chip
• Immune to noise due to differential voltage or chip to sensor
• Implemented in hardware or software • Requires transceiver chips and twisted pair
• Ease to implement, many examples cable with terminating resistors which add to
• Widely used in industrial automation system cost.
• Higher speeds beyond 115200 baud
RS485 Network Topology: Any station can communicate with any other station, but not at the
same time.
Page 13
Ethernet
Disadvantages
Advantages
• Cost
• Very high speed ( 10Mbit to 100Mbit/s ) • More suitable for system to system
• Very long distance, hundreds of feet can be communications, not so much for chip to
achieved, more with hubs and switches chip/sensor
• Immune to noise • Requires Ethernet chipset, transformer,
• Widely used in industrial automation due to noise jack and special cabling that add to
immunity system cost.
• Complicated to implement
• High code footprint
Page 14
Appendix A: Comparison of Serial Digital Data Networks ( from MAXIM website: www.maxim-ic.com )
2
1-Wire I C* SMBUS™ SPI™ MicroWire/PLUS™ M-Bus (EN1434) CAN (ISO11898) LIN Bus
Network single master, multiple masters, multiple masters, single master, single master, multiple single master, multiple multiple masters, single master,
Concept multiple slaves multiple slaves multiple slaves multiple slaves slaves slaves multiple slaves multiple slaves
Number of 2, (SMBCLK, 4, (CS\, SI, SO, 2 (lines can be 2 (CAN_H, CAN_L,
1 (IO) 2, (SCL, SDA) 4, (CS\, DI, DO, SK) 1 (LIN)
Signal Lines SMBDAT) SCK) swapped) terminated)
Optional SMBSUS#, 2nd GND, Power,
N/A N/A N/A N/A N/A N/A
signals SMBALERT# Shield
Limited by max. Limited by max.
Up to 300 m (with Max. 350m per
400pF bus 400pF bus N/A (circuit board 40m @1M bps1000m Up to 40m, max.
Network Size suitable master N/A (circuit board level) segment of max. 250
capacitance capacitance level) @ 50k bps (example) 10nF total load
circuit) slaves; max. 180nF
requirement requirement
open drain, open drain, resistive open drain, Differential open
Network Push-pull with M to S: voltage drive open drain, resistive
resistive or active or active master resistive or active Push-pull with tristate drain/source or open
Interface tristate S to M: current load master pull-up
master pull-up pull-up master pull-up coll./emitter
From 1.8V to
Network From 2.8 to 6.0 V, From 1.8 to 5.5V, From 1.8V to 5.5V, device VDD-VD (diode drop);
2.7V to 5.5V 5.5V, device ~40V 8 to 18V
Voltage device specific device specific specific ~4.5V max.
specific
VDD-related level:
Fixed level: >1.5V, VDD-related level: Fixed level: <0.8V, >2.0V; Master to slave: 24V, Differential: <50mV
<20%, >80% of VDD
Logic Vary with network >3.0 V VDD-related <20% (30%), VDD-related level: <20% 36V nominalSlave to (recessive), >1.5V
<0.8V, >2.1V (driver spec.)<40%,
Thresholds voltage level: <30%, >70% >70% of VDD (30%), >70% (80%) of VDD master: <1.5mA, (dominant); driver
>60% of VDD
of VDD (inconsistent) (inconsistent) >11mA specification
(receiver spec.)
MS bit first plus MS bit first plus LS bit first, half-
LS bit first, half- MS bit first, full- LS bit first, half-
Transmission Acknowledge bit, Acknowledge bit, MS bit first, full-duplex duplex, acknowledge MS bit first, half-duplex
duplex duplex duplex
half-duplex half-duplex response
Message identifier 11
7 bits, (10 bits 7 bits, (10 bits 8 bits (primary Message identifier 8
Address bits (standard format),
56 bits defined but not defined but not N/A N/A address), 64 bits bits, including 2 parity
Format 29 bits (extended
implemented) implemented) (secondary address) bits
format)
ARP, Address
Automatic, N/A; slave N/A; slave select N/A; message-based N/A; message-based
Network Resolution N/A; slave select (CS\)
supports dynamic addresses hard- (CS\) hard-coded Automatic protocol, not address protocol, not address
Inventory Protocol (Rev. hard-coded in firmware
topology change coded in firmware in firmware based based
2.0 only)
Standard: ~0 to
Standard: ~0 to
100k bps; Fast: ~0
Gross Data 16.3k bps ~0 to ~10 M bps ~0 to ~5 M bps (device
to 400k bps; High- 10k to 100k bps 300, 2400, 9600 bps ~0 to 1M bps ~1k to ~20k bps
Rate Overdrive: ~0 to (device specific) specific)
Speed: ~0 to 3.4M
142k bps)
bps
Standard: ~ 5.4ms Standard: Primary address, At 1M bps 19µs
At 20k bps 1.7ms
Overdrive: ~0.6ms ~95µsFast: ~95µs @ 100k 2400 bps: 13.75ms (standard) or 39µs
Access Time N/A N/A from start of frame to
(at maximum ~23µs(at maximum bps (short frame), 27.5ms (extended) from start
1st data bit
speed) speed) (long frame) of frame to 1st data bit
PEC Packet
Data 8-bit and 16-bit Even parity, check 15-bit CRC, frames,
N/A Error Code N/A N/A Check sum, frames
Protection CRC sum, frames frame acknowledge
(Rev.1.1, 2.0)
Collision Yes, through non- Yes (multi-master Yes (Rev. 2.0 Yes ("medium" and Yes, through check
N/A N/A Yes: CSMA/CD
Detection matching CRC operation only) only) "strong" collisions) sum
Parasitic (typical), Parasitic and/or local VDD only, local or
Slave supply VDD only VDD only VDD only VDD only Parasitic only
VDD (exception) supply remote source