Microprocessor Lect 1 Final N Serial Comm
Microprocessor Lect 1 Final N Serial Comm
Lecture # 4 (Final)
Serial Communications Interfaces
Prepared by: Ms. Tahmida Islam, Lecturer, EEE Department, AIUB
Modified by the Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan,
EEE Department, AIUB
Department of EEE
Microprocessor and Embedded Systems
Where leaders are created Data Transmission
• Data transmission can be performed
two ways.
1. Parallel Communications, where
several bits of data are
transmitted/received as a whole, on a
link with several parallel channels.
2. Serial Communications, where data is
transmitted/ received bit by bit
through a single channel.
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
2 2
Microprocessor and Embedded Systems
Where leaders are created
Serial Data Communication
❑ Advantage of serial communication:
• Smaller number of communication lines is required compared to parallel
communication.
▪ 2 lines (transmit & receive) are required in asynchronous full duplex serial comm.
▪ 3 lines (transmit, receive & clock) are required in synchronous serial communication.
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
3 3
Microprocessor and Embedded Systems
Where leaders are created
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
4 4
Microprocessor and Embedded Systems
Where leaders are created USART vs. UART
UART USART
Full Name Universal Asynchronous Receiver/Transmitter Universal Synchronous/Asynchronous
Receiver/Transmitter
Data type It generates asynchronous data, hence has low data It generates clocked/synchronous data,
and rate rate. hence has higher data rate.
Baud rate Receiver need to know baud rate of the transmitter Receiver need not be required to know
before communication to be established so that UART the baud rate of the transmitter. This is
can generate clock internally and synchronize it with derived from the clock signal and data
data stream with the help of transition of start bit. line.
Data It uses start bit (before data word), stop bits (one or USART can also generate data like UART.
Structure two, after data word), parity bit (even or odd) in its Hence USART can be used as UART, but
base format for data formatting. reverse is not possible.
Protocol UART is simple protocol to generate data. USART is complex and uses many
different protocols to generate the data
for transmissions.
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
5 5
Microprocessor and Embedded Systems
Where leaders are created USART
• It is an asynchronous serial communication.
• It uses 2 pins in Port D:
1. TXD/PD1 – The serial data transmission line.
2. RXD/PD0 – The serial data reception line.
• Data is transmitted/received in a serial frame as follows:
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
6 6
Microprocessor and Embedded Systems
Where leaders are created USART
• Each bit is sent with a specific time duration τ, called bit-time. The smaller
is τ, the faster is data transmission. The rate of data transmission/reception
is called the Baud rate.
• Standard Baud rates are: 2400, 4800, 9600, 14400,19200,... bps
• In the ATmega328, the Baud rate is generated from internal clock. The Baud
rates at the transmitter and receptor must be the same to avoid
communication error.
• The baud error should be < ± 2% to avoid communication error.
𝑺𝒕𝒂𝒏𝒅𝒂𝒓𝒅 𝒃𝒂𝒖𝒅 𝒓𝒂𝒕𝒆−𝑪𝒂𝒍𝒄𝒖𝒍𝒂𝒕𝒆𝒅 𝒃𝒂𝒖𝒅 𝒓𝒂𝒕𝒆
• 𝑩𝒂𝒖𝒅 𝒆𝒓𝒓𝒐𝒓 𝒓𝒂𝒕𝒆 = × 𝟏𝟎𝟎%
𝑺𝒕𝒂𝒏𝒅𝒂𝒓𝒅 𝒃𝒂𝒖𝒅 𝒓𝒂𝒕𝒆
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
7 7
Where leaders are created
USART: Internal Clock Generation – Microprocessor and Embedded Systems
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
11 11
Microprocessor and Embedded Systems
Where leaders are created
Continuation...
Solution:
For synchronous master mode:
𝑓𝑜𝑠𝑐 1×106
𝐵𝑎𝑢𝑑 𝑟𝑎𝑡𝑒 = = = 19231 bps
2 𝑈𝐵𝑅𝑅𝑛+1 2 25+1
𝑆𝑡𝑎𝑛𝑑𝑎𝑟𝑑 𝑏𝑎𝑢𝑑 𝑟𝑎𝑡𝑒 − 𝐶𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑒𝑑 𝑏𝑎𝑢𝑑 𝑟𝑎𝑡𝑒
𝐵𝑎𝑢𝑑 𝑒𝑟𝑟𝑜𝑟 𝑟𝑎𝑡𝑒 = × 100%
𝑆𝑡𝑎𝑛𝑑𝑎𝑟𝑑 𝑏𝑎𝑢𝑑 𝑟𝑎𝑡𝑒
19200 − 19231
= × 100% = −0.161% < ±2%
19200
So, there will be no communication error for the given information.
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
12 12
Microprocessor and Embedded Systems
Where leaders are created
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
17 17
Microprocessor and Embedded Systems
Where leaders are created
Serial Peripheral Interfaces (SPI)
• Connection using SPI is in the Master-Slave configuration.
SPI Communication
Schematic
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
18 18
Microprocessor and Embedded Systems
Where leaders are created
Serial Peripheral Interfaces (SPI)
• Let's make an example with Arduino. In this example, we are going to let the two
Arduinos to communicate with each other.
We will connect two Arduino UNO
boards together; one as a master
and the other as a slave.
▪ (SS): pin 10; Slave Selection
▪ (MOSI): pin 11
▪ (MISO): pin 12 Communication
▪ (SCK): pin 13
SPI as MASTER
//SPI Master Device for(count=0; count<255; count++){
//We need to import SPI.h library first sendSerialData(count, SlaveSelection);
#include <SPI.h> delay(2000);
}
//Our Slave Selection pin
delay(500);
#define SlaveSelection 10 }
int count = 0; void sendSerialData(char data, int SlaveSelection) {
//Enable slave Arduino with setting the Slave Selection pin to 0 V
void setup() digitalWrite(SlaveSelection, LOW);
{ // Wait for a moment
//Set SlaveSelection pin as output. delay(10);
pinMode(SlaveSelection, OUTPUT); //and Make it //We sent the data here and wait for the response from device
char receivedValue = SPI.transfer(data);
//HIGH to prevent to start communication right away
//And then write the answer to the serial port
digitalWrite(SlaveSelection, HIGH); //Start the SPI Serial.println(receivedValue);
//communication. //Disable slave Arduino with setting the Slave Selection pin to 5 V
SPI.begin(); digitalWrite(SlaveSelection, HIGH);
} }
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
24 24
Microprocessor and Embedded Systems
Where leaders are created
SPI Examples SPI as SLAVE void loop()
SPI as SLAVE
❑ Disadvantages
• As the number of slaves increases, the number of CS lines increases, this results in hardware
complexity as the number of pins required will increase.
• To add a device in SPI requires one to add an extra CS line and changes in software for the
particular device addressing is concerned.
• Master and slave relationship can not be changed as usually done in the I2C interface.
• No flow control available in SPI.
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
26 26
Microprocessor and Embedded Systems
Where leaders are created
I2C (Inter-Integrated Circuit): What is it?
• An inter-integrated circuit (I2C) or two-wire interface (TWI) is a synchronous serial
protocol originally developed by Philips Semiconductors (now NXP).
• It’s a multi-master, multi-slave serial bus for low-speed devices that only requires
two wires among multiple devices. It can easily be implemented with two digital
input/output channels on a device.
• An I2C bus has just two wires over which hundreds of devices communicate serially.
• As a master-slave type communication standard, at least one device connected to
the bus should be the master that generates a clock signal for synchronous serial
data communication.
• The slave devices can transfer data to and from the master device(s), which access
slave devices by their I2C addresses. The address of each slave device on an I2C bus
must be unique. The I2C slave devices still must obtain their addresses from NXP.
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
27 27
Microprocessor and Embedded Systems
Where leaders are created
I2C (Inter-Integrated Circuit)
• A chip-to-chip protocol for communicating with low-speed peripherals
• The I2C bus drivers are open drain, which means the devices can pull the I2C signal
line low but cannot drive it high. By default, both the lines are pulled high by pull-
up resistors until the bus is accessed by a master device to avoid bus contention.
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
28 28
Microprocessor and Embedded Systems
Where leaders are created I2C (Inter-Integrated Circuit)
• I2C is another serial protocol for two-wire interface to connect to low-speed
devices like Micro-controller, EEPROMs, I/O Interfaces, and other similar devices
used in embedded systems.
• I2C is a bus for communication between a master (or can be multiple masters) and
a single or multiple slave devices.
• I2C uses only two wires- SCL (Serial Clock) and SDA (Serial Data).
• SCL (Serial Clock): The clock line used to synchronize all data transfers over the I2C
bus, the line over which master device(s) generate the clock signal.
• SDA (Serial Data): The data line used to transmit the data between devices, the line
over which the master and slave devices communicate serial data
• Each I2C Slave devices have a 7-bit/10-bit addressing.
• The data transfer rate depends on the clock frequency. In the standard mode, the
clock frequency is 100-400 kHz with 7 bit addressing and data transfer
29 of 100 kbps. 29
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
Microprocessor and Embedded Systems
Where leaders are created
I2C Addresses
• The clock frequencies for the following three modes are with 10-bit addressing:
▪ 1 MHz in fast mode I2C
▪ 3.4 MHz in high-speed mode
▪ 5 MHz in ultra-fast mode
• Addresses need to be unique on the bus to determine the slave that were to
transmit the data.
• The master device needs no address since it generates the clock (using SCL) and
addresses individual I2C Slave devices.
• The maximum number of Slave devices that can be used while using 7-bit
addressing are 112 devices The I2C specification has reserved 2 sets of 8 addresses,
1111XXX and 0000XXX. and the maximum number of Slave devices used in 10-bit
addressing are 1008 devices. The remaining 16 are reserved.
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
30 30
Microprocessor and Embedded Systems
Where leaders are created
Working of I2C in Arduino
• The I2C is a half-duplex type of communication. A master device can only read or
write data to the slave at a time. All operations are controlled by master device(s).
• In I2C data transfer occurs in Message Frames which are then divided into Frames of
Data. A message contains the various number of Frames in which one frame contain
the address of the slave, and remaining frames for data to be transmitted.
• The message includes START/STOP Conditions, READ/WRITE Bits and ACK/NACK
(Acknowledgement/No-acknowledgement) Bits between each Data Frame. Working
shown below:
• Start Condition: The SDA line switches from high to low voltage level before SCL
switches from high to low.
• Stop Condition: The SDA line switches from low to high voltage level after SCL
switches from low to high.
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
31 31
Microprocessor and Embedded Systems
Where leaders are created
Working of I2C in Arduino
• Address Frame: 7 or 10-bit sequence unique to each slave that identifies
the slave when the master wants to talk.
• Read/Write Bit: A bit specifying whether the master is sending data to the
slave or requesting data from it.
• ACK/NACK Bit: Each frame in a message follows an ACK/NACK Bit.
❑ 7-bit Addressing:
• In 7-bit addressing procedure, the slave address is transferred in the first
byte after the Start condition. The first seven bits of the byte comprise the
slave address. The eighth bit is the read/write flag where 0 indicates a
write and 1 indicates a read.
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
32 32
Microprocessor and Embedded Systems
Where leaders are created Working of I2C in Arduino
Figure 1: 7-bit addressing. The I2C bus specification specifies that in standard-mode I2C, the
slave address is 7-bits long followed by the read/write bit.
• All I2C products from Total Phase, follow this standard convention. The slave
address used should only be the top seven bits. In the case of the Aardvark I2C/SPI
Host Adapter, the software will automatically append the correct read/write bit
depending on the transaction to be performed. In the case of the Beagle I2C/SPI
Protocol Analyzer, the slave address and the type of transaction are displayed in
two different columns.
• Reserved Addresses
• The I2C specification has reserved two sets of eight addresses, 1111XXX and
0000XXX. These addresses are used for special purposes.
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
33 33
Microprocessor and Embedded Systems
Where leaders are created Working of I2C in Arduino
Figure 1: 7-bit addressing. The I2C bus specification specifies that in standard-mode I2C, the
slave address is 7-bits long followed by the read/write bit.
• The first byte of an I2C transfer contains the slave address and the data direction.
• The address is 7 bits long, followed by the direction bit (read or write operation).
Like all data bytes, the address is transferred with the most significant bit first.
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
34 34
Microprocessor and Embedded Systems
Where leaders are created
Working of I2C in Arduino
• The following table has been taken from the I2C Specifications (2000).
Figure 2: 10-bit addressing. In 10-bit addressing, the slave address is sent in the first two bytes. The first byte begins with the
special reserved address of 1111 0XX which indicates that 10-bit addressing is being used. The 10 bits of the address is encoded
in the last 2 bits of the first byte and the entire 8 bits of the second byte. The 8th bit of the first byte remains the read/write flag.
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
36 36
Microprocessor and Embedded Systems
Where leaders are created Program for I2C: I2C_SCANNER
#include <Wire.h> for(address = 1; address < 127; address++ )
{
void setup() // The i2c_scanner uses the return value of the
{ // Write.endTransmission to see if a device did acknowledge to
Wire.begin(); // the address.
Wire.beginTransmission(address);
serial.begin(9600); error = Wire.endTransmission();
while (!Serial); // Leonardo: wait for serial monitor
serial.println("\nI2C Scanner"); if (error == 0)
} {
serial.print("I2C device found at address 0x");
void loop()
if (address<16)
{
serial.print("0");
byte error, address;
serial.print(address ,HEX);
int nDevices;
serial.println(" !");
serial.println("Scanning...");
nDevices++;
nDevices = 0; }
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
37 37
Microprocessor and Embedded Systems
Where leaders are created Program for I2C: I2C_SCANNER
else if (error==4)
{
serial.print("Unknown error at address 0x");
if (address<16)
serial.print("0");
serial.print(address,HEX);
serial.println(" !");
}
}
if (nDevices == 0)
serial.println("No I2C devices found\n");
else
serial.println("done\n"); Find addresses of different I2C devices
delay(5000); // wait 5 seconds for next scan connected to Arduino
} https://playground.arduino.cc/Main/I2cScanner/
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
38 38
Microprocessor and Embedded Systems
Where leaders are created
Serial Monitoring
• Upload it to the Arduino and open the serial monitor. Every found device
on the I2C-bus is reported.
• You can change the wires, and plug-in I2C devices while the I2C_scanner is
running.
• The output of the serial monitor looks like this:
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
39 39
Microprocessor and Embedded Systems
Where leaders are created Advantages and Disadvantages of I2C
❑ Advantages
• Due to open collector design, limited slew rates can be achieved.
• More than one masters can be used in the electronic circuit design.
• Needs fewer i.e., only 2 wires for communication.
• I2C addressing is simple which does not require any CS lines used in SPI and it is easy to add
extra devices on the bus.
• It uses open collector bus concept. Hence there is bus voltage flexibility on the interface bus.
• Uses flow control.
• Disadvantages
• Increases complexity of the circuit when number of slaves and masters increases.
• I2C interface is half duplex.
• Requires software stack to control the protocol and hence it needs some processing
overheads on microcontroller/ microprocessor 40
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan 40
Microprocessor and Embedded Systems
Where leaders are created
RS232
• RS232 is the interface mainly used for
serial data communication.
• It supports data transfer rate from about
110 bps to about 115200 bps.
• Hyper terminal is the application mainly
used to check serial communication port of
the computer, often referred as COM port.
• The interface is of two types-
▪ DB9 pin connector and
▪ DB25 pin connector.
• The interface is mainly used for one-to-
one serial communication.
DB25: 25 pin connectors 41
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan 41
Microprocessor and Embedded Systems
Where leaders are created RS232
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
43 43
Microprocessor and Embedded Systems
15 March 2023 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
44 44