You are on page 1of 14

Compiled by Yinebeb T.

Three registers control the behavior of the pins:


1. Data Direction Register (DDR): Each bidirectional port has its own
DDR, which contains one bit for each pin of the port. The functionality of
a pin (input or output) is determined by clearing or setting its bit in the
DDR. Different pins of a port may be configured differently, so it is
perfectly okay to have three pins configured to output and use the other
five as inputs. After a reset, the DDR bits are generally initialized to input.
Reading the register returns its value.
2. Port Register (PORT): This register is used to control the voltage level
of output pins. Assuming a pin has been configured to output, then if its
bit in the PORT register is set, the pin will be high; if the bit is cleared,
the pin will be low. To avoid overwriting the other bits in the port when
setting a particular bit, it is generally best to use the controller’s bit
operations.
For output pins, reading the register returns the value you have written.
For input pins, the functionality depends on the controller. Some
controllers allow you to read the state of input pins through the port
register. Other controllers, e.g. the ATmega16, use the port bits for other
purposes if the corresponding pins are set to input, so here you will read
back the value you have written to the register.
3. Port Input Register (PIN): The PIN register is generally read-only and
contains the current state (high or low) of all pins, whether they are
configured as output or as input. It is used to read the state of input pins,
but it can also be used to read the state of output pins to verify that the
output was taken over correctly. A write to this register generally has
no effect

Register Bits

• DDxn bits are accessed at the DDRx I/O address


• PORTxn bits at the PORTx I/O address
• PINxn bits at the PINx I/O address

Where n = pin bit number in the Port Register


DDxn
The DDxn bits in the DDRx Register select the direction of this pin. If
DDxn is written to '1', Pxn is configured as an output pin. If DDxn is
written to '0', Pxn is configured as an input pin.

PORTxn
The PORTxn bits in the PORTx register have two functions. They can
control the output state of a pin and the setup of an input pin.
As an Output:
If a '1' is written to the bit when the pin is configured as an output pin, the
port pin is driven high. If a ‘0’ is written to the bit when the pin is
configured as an output pin, the port pin is driven low.
As an Input:
If a '1' is written to the bit when the pin is configured as an input pin, the
pull-up resistor is activated. If a ‘0’ is written to the bit when the pin is
configured as an input pin, the port pin is tri-stated.

PINxn
The PINxn bits in the PINx register are used to read data from port pin.
When the pin is configured as a digital input (in the DDRx register), and
the pull-up is enabled (in the PORTx register) the bit will indicate the
state of the signal at the pin (high or low).
Note: If a port is made an output, then reading the PINx register will give
you data that has been written to the port pins.

Interrupts

In digital computers, an interrupt is an input signal to


the processor indicating an event that needs immediate attention. An
interrupt signal alerts the processor and serves as a request for the
processor to interrupt the currently executing code, so that the event can
be processed in a timely manner. If the request is accepted, the processor
responds by suspending its current activities, saving its state, and
executing a function called an interrupt handler (or an interrupt service
routine, ISR) to deal with the event. This interruption is temporary, and,
unless the interrupt indicates a fatal error, the processor resumes normal
activities after the interrupt handler finishes.[1]
Interrupts are commonly used by hardware devices to indicate electronic
or physical state changes that require attention. Interrupts are also
commonly used to implement computer multitasking, especially
in real-time computing. Systems that use interrupts in these ways are said
to be interrupt-driven.[2]

It is a method of creating a temporary halt during program execution and


allows peripheral(output) device to access mpu (microprocessor unit)
Microprocessor unit response to that interrupt with interrupt service
routine ISR (short prog indicating how to handle the interrupt (events that
come into the program)).
The microprocessor jumps to ISR to service peripherals after its
microprocessor again continue to the main program

An interrupt is a condition that causes the microprocessor to temporarily


work on a different task, and then later return to its previous task.
Interrupts can be internal or external. Internal interrupts, or "software
interrupts," are triggered by a software instruction and operate similarly
to a jump or branch instruction. An external interrupt, or a "hardware
interrupt," is caused by an external hardware module. As an example,
many computer systems use interrupt driven I/O, a process where
pressing a key on the keyboard or clicking a button on the mouse triggers
an
interrupt. The processor stops what it is doing, it reads the input from the
keyboard or mouse, and then it returns to the current program.

The grey bars represent the control flow. The top line is the program that
is currently running, and the bottom bar is the interrupt service routine
(ISR). Notice that when the interrupt (Int) occurs, the program stops
executing and the
microcontroller begins to execute the ISR. Once the ISR is complete, the
microcontroller returns to processing the program where it left off.
Microcontrollers tend to be deployed in systems that have to react to
events. Events signify state changes in the controlled system and
generally require some sort of reaction by the microcontroller. Reactions
range from simple responses like incrementing a counter whenever a
workpiece crosses a photoelectric barrier on the conveyor belt to
time-critical measures like shutting down the system if someone reaches
into the working area of a machine.
As soon as the event occurs, the microcontroller calls an interrupt service
routine (ISR) which handles the event. The ISR must be provided by the
application programmer.

I2C bus arbitration

Procedure that ensures that only one of the master decide will control the
bus. This ensure that the transfer data does not get corrupted.
Process to get the bus access
Related to SDA line
It is a multi-master bus driving the bus simultaneously so constantly
monitoring the bus continuously start and stop condition they can
determine whether the bus is idle(first) or not
multiple master start transfers at the same time in this case only one
master will be win who ever send zero first is called arbitration

When we use more than one master. consider a scenario when both
master (master1 and master2) start the communication at time in this
condition only the bus arbitration occur.
1f master1 starts the communication by pulling the clock low then then
the master2 cannot able to control the clock until the SCL is released by
the master1(master2 is considered as a slave in this condition). after the
stop bit is observed by the master2 then it will start the communication.
When the two masters want to write or read they compared by their one
or two first bits this is an arbitration master2 waits at slave condition until
master1 finish.
This is how I2C bus arbitration work.

Clock Synchronization
Procedure where the clock signals of two or more device are
synchronized
Every master has an internal computer L determining the time during
which it’s generated clock will be SCL=0 and internal computer H
determine the SCL=’1’ period. The SCL output have the same wired
-AND behavior as the SDA
Clock Synchronization is activated from the moment that the arbitration
is started.

In Synchronization master decides the clock speed by controlling the it.


However, the slave may not cooperate with the clock speed given by
master and it may need to slow down. This can be done by clock
synchronization.
I2C is not Synchronized with the predefined baud rate.

What is UART?
UART stands for Universal Asynchronous Receiver Transmitter. At a
high-level, a UART is simply a microchip that enables communications
between a computing device (PC, embedded system, etc.) and other
equipment.

To understand what a UART does in more detail, it is useful to


understand serial communication and parallel communication. We will
provide a crash course on those two topics here.

Serial communication in simple terms is sending data a single bit at a


time over a given communications bus. While RS-232 (note that UART
is commonly used for RS-232) and RS-485 are the two protocols we most
commonly associate with the term “serial communication”, many other
modern technologies such as USB (Universal Serial Bus), SATA (Serial
ATA), and Firewire/IEEE 1394 are also serial protocols. One of the
advantages of serial communication is that data can be transmitted at
higher frequencies, therefore increasing the amount of data that can be
sent despite the fact that less data is sent at once when compared to
parallel communication.

Parallel communication, on the other hand, is sending multiple bits of


data at the same time over a given communications bus. For example, a
32-bit parallel communications medium such as conventional PCI
(Peripheral Component Interconnect) would send 32-bits of data per
clock cycle. While one would think that this would give parallel
communication significant speed advantages over serial, in practice serial
is faster because it can accommodate more clock cycles per second (e.g.
GHz speeds).

So, what exactly does this have to do with UART anyway? Simple,
UART chips are used to convert inbound serial data to parallel data for
the system to read and conversely converts outbound parallel data to
serial before sending it on to other systems. Worded differently, a UART
enables a system to act as a DTE (Data Terminal Equipment) Device.

Before we move onto SPI, let’s quickly cover a few other important
details about UART communication:

• UART uses 2 wires: 1 for transmit (or Tx) and one for receive (or
Rx)
• UART communication is asynchronous, meaning that it is not
synchronized using a clock
• UART has a max communication distance of 15 meters
• UART leverages shift registers to convert serial communication to
parallel communication
• UART is commonly used as a “serial port” on computers or in
microcontrollers
• UART supports full-duplex communication

ADVANTAGES AND DISADVANTAGES OF UARTS


No communication protocol is perfect, but UARTs are pretty good at what
they do. Here are some pros and cons to help you decide whether or not
they fit the needs of your project:

ADVANTAGES
• Only uses two wires
• No clock signal is necessary
• Has a parity bit to allow for error checking
• The structure of the data packet can be changed as long as both
sides are set up for it
• Well documented and widely used method
DISADVANTAGES
• The size of the data frame is limited to a maximum of 9 bits
• Doesn’t support multiple slave or multiple master systems
• The baud rates of each UART must be within 10% of each other

What is SPI?
SPI (Serial Peripheral Interface) is a serial communication protocol
originally developed by Motorola that enables communication between
nearly any electronic device that supports clocked serial streams. SPI uses
a master-slave method for communication that enables high-speed data
streaming.

As opposed to just using two wires, SPI must use at least 4 wires. As
there can be multiple slave devices in an SPI implementation, the actual
amount of wires or traces used will be n+3 where n = the number of slave
devices in use.

A few additional key details on SPI before we move on:

• SPI is synchronous
• SPI is a simple protocol with little overhead
• SPI supports full-duplex communication
• SPI communication does not have a means for acknowledgment or
flow control
• SPI does not use much board space

Differences between UART vs SPI


While UART and SPI both support full-duplex communication, both use
“serial” communication in some way, and they are both only suitable for
short-distance use cases, there are not too many similarities beyond that.
So, what are the differences between UART vs SPI? The answer is there
are many; we will cover a number of them here.

Hardware vs protocol
One of the biggest differences is that UART is a type of hardware while
SPI is a protocol. When you are dealing with the nuances of getting
things to work in an embedded system, this can be easy to overlook.
However, UART is an actual piece of hardware (a microchip) while SPI
is a protocol or specification for communication.

Number of pins
UARTs use just two pins while SPI devices need at least 4. When
designing a system this means that if pins/traces are scarce, SPI may not
be an attractive solution.

Number of devices that can be connected


A corollary to the number of pins, UARTs and SPI devices support a
different number of devices. UART, leveraging only Tx and Rx for
outbound communication, is effectively limited to 1 to 1 communication.
SPI, on the other hand, can leverage its master/slave paradigm to enable
one too many communications.

Communications speed
SPI is significantly faster than UART. In some cases, an SPI solution can
be three times faster than a UART solution.

Price
In any engineering endeavor, the cost of a given solution is a big driver of
choice. Generally speaking, SPI is cheaper than UART.

Asynchronous vs Synchronous
As you can tell from the descriptions of the two earlier, another major
difference when comparing UART vs SPI is UART communication is
asynchronous while SPI is synchronous.

Size
Generally speaking, SPI devices take up relatively less space than UART
chips. This means use cases where there is limited board space may be
better served by SPI.

UART vs SPI: What should I use?


There is no one-size-fits-all answer to the “UART vs SPI: What should I
use?” question. However, with the information we have given you here,
you are now equipped to make an informed decision about which solution
is best for a given application you may be working on.

Generally speaking, SPI is preferred in applications that require higher


speed communication between chips or applications that require
communications between multiple devices. On the other hand, UART
may be better suited for applications that have to travel slightly more
distance such as diagnostic displays or other applications that require
RS-232 support.

Tools to debug and develop UART vs SPI


Here at Total Phase, we manufacture solutions designed to enable
debugging and development of embedded systems. Given the prevalence
of SPI in embedded systems, it is no surprise that we have a variety of
solutions that can aid with the debugging and development of systems
that use SPI.

For example, our SPI Development Kit enables developers to test target
devices as a master device, simulate a master device, program flash
devices that are SPI-based, and monitor an SPI bus with granularity down
to 20 nanoseconds.
Alternatively, if you need to debug data traveling to or from a UART, a
Total Phase protocol analyzer may prove useful. For example, check out
this article that covers how to monitor UART data over RS232 with the
Beagle USB 480 Power Protocol Analyzer.

Introduction of I2C Protocol


An I2C protocol is one of the serial communication protocols that is used
for the chip to chip communication. Similar to the I2C protocol, SPI and
UART also used for the chip to chip communication.

The I2C is the short form of Inter-Integrated Circuit, is a type of bus,


which designed and developed by Philips in 1980 for inter-chip
communication.I2C is adopted by a lot of vendor companies for the chip
to chip communication.

It is a multi-master and multi-slave serial communication protocol means


that we have the freedom to attach multiple IC at a time with the same
bus. In I2C protocol, communication always started by the master and in
the case of multi-master, only one master has the ownership of the bus.

In this article, you will learn the I2C protocol and its bus configuration
and uses in the chip to chip communication.

Example uses of I2C on different device

The I2C protocol are also used for two-wire interface to connect
low-speed devices like microcontrollers, EEPROMs, A/D and D/A
converters, I/O interfaces and other similar peripherals in embedded
systems.

What is I2C?
I2C is a serial communication protocol. It provides the good support to
the slow devices, for example, EEPROM, ADC, and RTC etc.
I2c is not only used with the single board but also used with the other
external components which have connected with boards through the
cables.

I2C is basically two-wire communication protocol. It uses only two wire


for the communication. In which one wire is used for the data (SDA) and
other wire is used for the clock (SCL).

In I2C, both buses are bidirectional, which means master able to send and
receive the data from the slave. The clock bus is controlled by the master
but in some situations, slave is also able to suppress the clock signal, but
we will discuss it later.

Additionally, an I2C bus is used in the various control architecture, for


example, SMBus (System Management Bus), PMBus (Power
Management Bus), IPMI (Intelligent Platform Management Interface)
etc.

SDA (Serial Data) – The line for the master and slave to send and
receive data.

SCL (Serial Clock) – The line that carries the clock signal.

Clock synchronization in I2C


Unlike Rs232, I2c is synchronous communication, in which clock is
always generated by the master and this clock is shared by both master
and slave.
In the case of multi-master, all master generates their own SCL clock,
hence it is necessary that clock of all master should be synchronized. In
the i2C, this clock synchronization is done by wired and logic.

For the better understanding, I am taking an example, where two masters


try to communicate with a slave. In that situation, both masters generate
their own clock, master M1 generate clk1 and master M2 generate clk2
and clock which observed on the bus is SCL.

Arbitration in I2C
The arbitration is required in case of multi-master, where more than one
master is tried to communicate with a slave simultaneously. In I2C
arbitration is achieved by the SDA line

Advantages of I2C communication protocol


There is a lot of advantage of I2C protocol which makes the user helpless
to use the I2C protocol in many applications.

• It is the synchronous communication protocol, so no need of


precise oscillators for the master and slave.
• It requires only two wire, one wire for the data (SDA) and other
wire for the clock (SCL).
• It provides the flexibility to the user to select the transmission rate
as per the requirements.
• In I2C Bus, each device on the bus is independently addressable.
• It follows the master and slave relationships.
• It has the capability to handle the multiple masters and multiple
slaves on the I2C Bus.
• I2C has some important features like arbitration, clock
synchronization, and clock stretching.
• I2C provide ACK/NACK (acknowledgment/
Not-acknowledgement) features which provide the help in error
handling.

Some important limitation of I2C communication protocol


An I2C protocol has a lot of advantage but beside it, I2C has a few
limitations.

• It consumes more power than other serial communication busses


due to open-drain topology.
• It is good only for the short distance.
• I2C protocol has some limitation for the number of slaves, the
number of the slave depends on the capacitance of the I2C bus.
• It only provides few limited communication speed like 100
kbit/s,400 kbit/s etc.
• In I2c, devices can set their communication speed, slower
operational devices can delay the operation of faster speed devices.
• Speed of I2C is also dependent by data speed, wire quality and
external noise

Summary
In summary, I have compiled all the various advantages / disadvantages
and functions of the various communication peripherals and compared
them so you can easily pick which is the best for your project. Do keep in
mind that the device you are using must support the communication
peripheral as well.

UART I2C SPI

Easy to
Complex as device
Complexity Simple chain many
increases
devices.

Faster than
Speed Slowest Fastest
UART
Up to 127
but may
Many, but there are
Number of Up to 2 get
practical limits and
devices devices complex
may get complicated
as devices
increases

Number of wires 1 2 4

Half
Duplex Full Duplex Full Duplex
Duplex

Number of No multiple Multiple Only 1 master but


masters and slaves and slaves and can have multiple
slaves masters masters slaves.

You might also like