0% found this document useful (0 votes)
13 views

Microprocessor Lect 1 Final N Serial Comm

This document discusses serial communication interfaces in microprocessors. It describes the Universal Synchronous Asynchronous Receiver Transmitter (USART), Serial Peripheral Interface (SPI), and Two Wire Interface (TWI)/Inter-Integrated Circuit (I2C) serial interfaces available on the ATmega328 microcontroller. Specifically, it provides details on the USART interface, including how it transmits asynchronous serial data using start and stop bits, the standard baud rates used, and how the internal baud rate generator sets the baud rate from the oscillator clock frequency. An example calculation of baud rates for different USART operating modes is also shown.

Uploaded by

arifinsabid59
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Microprocessor Lect 1 Final N Serial Comm

This document discusses serial communication interfaces in microprocessors. It describes the Universal Synchronous Asynchronous Receiver Transmitter (USART), Serial Peripheral Interface (SPI), and Two Wire Interface (TWI)/Inter-Integrated Circuit (I2C) serial interfaces available on the ATmega328 microcontroller. Specifically, it provides details on the USART interface, including how it transmits asynchronous serial data using start and stop bits, the standard baud rates used, and how the internal baud rate generator sets the baud rate from the oscillator clock frequency. An example calculation of baud rates for different USART operating modes is also shown.

Uploaded by

arifinsabid59
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

AMERICAN INTERNATIONAL UNIVERSITY – BANGLADESH (AIUB)

Where leaders are created

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.

February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
2 2
Serial Data Microprocessor and Embedded Systems

Communication
Where leaders are created

 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.

 Disadvantage of serial communication:


• More time is required to transmit/receive compared to parallel communication.

February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
3 3
Microprocessor and Embedded Systems
Where leaders are created

Types of Serial Communication


ATmega328 has 3 types of serial communication interfaces:

1. Universal Synchronous Asynchronous Receiver & Transmitter (USART).


2. Serial Peripheral Interface (SPI).
3. Two Wire Interface (TWI)/ Inter-Integrated Circuit (I2C).

February 27, 2024 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 similar to
Structure two, after data word), parity bit (even or odd) in its UART. Hence USART can be used as
base format for data formatting. UART but 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.

February 27, 2024 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:

February 27, 2024 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.

February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
7 7
Where leaders are created
USART: Internal Clock Generation – Microprocessor and Embedded Systems

The Baud Rate Generator


• Internal clock generation is used for the asynchronous and the synchronous
master modes of operation.
• The USART Baud Rate Register (UBRRn) and the down-counter connected
to it functions as a baud rate generator.
• The down-counter, running at system oscillator clock frequency (fosc), is
loaded with the UBRRn value, each time the counter has counted down to
zero, thus a clock is generated.
• The Transmitter divides the baud rate generator clock output by 2, 8, or 16
depending on mode.
• The baud rate generator output is used directly by the Receiver’s clock and
data recovery units.
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
8 8
Microprocessor and Embedded Systems
Where leaders are created
Calculation of the Baud Rate
Table 17.1 Equations to calculate Baud Rate Register Setting
Operating Mode Baud Rate Equations Equations for UBRRn Values
Asynchronous Normal
Mode (U2Xn = 0)
Asynchronous Double
Speed Mode (U2Xn = 1)
Synchronous Master
Mode

Note:
1. The Baud rate is defined as the data transfer rate in bits per second (bps)
2. System oscillator clock frequency () should be set in Hz
3. UBRRn means contents of the UBRRnH and UBRRnL registers, and their values may vary from
0 to 4095; there are 12 bits data, so the total values can be 212 = 4096.
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
9 9
Microprocessor and Embedded Systems
Where leaders are created
Example to Practice
Find the baud rate for the three operating modes when fOSC = 1 MHz and
UBRRn = 25. Calculate the baud error and comment whether there will be any
communication error or not.
Solution:
For asynchronous normal mode:
bps

So, there will be no communication error for the given information.

February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
10 10
Microprocessor and Embedded Systems
Where leaders are created
Continuation...
Solution:
For asynchronous double speed mode:
bps

So, there will be no communication error for the given information.

February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
11 11
Microprocessor and Embedded Systems
Where leaders are created
Continuation...
Solution:
For synchronous master mode:
bps

So, there will be no communication error for the given information.

February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
12 12
Microprocessor and Embedded Systems
Where leaders are created

USART- Arduino Libraries


• USART functions can be used with Serial Monitor of the Arduino.
1. serial.begin(baud) – to enable input/output to serial monitor with baud
speed or rate in bps. Must be written in setup().
2. serial.available() – Get the number of bytes (characters) available for
reading from the serial port.
3. serial.println(val) – to display val value to serial monitor with newline
added.
4. serial.print(val) – as above but without newline.
5. serial.print(“Error”) – display message “Error” without newline.
6. serial.read() – Reads incoming serial data.
• others functions – refer to arduino.cc. 13
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan 13
Microprocessor and Embedded Systems
USART- Arduino Libraries: Camera shutter speed
Where leaders are created

example
•This example detects how long a camera else
shutter is open by using a change interrupt. At startTime = micros ();
the first transition, it gets the time and at the started = !started; } // end of the shutter
second one, it gets the new time. Then the main void setup () {
loop shows the difference. Serial.begin (115200);
•This is tested down to a 50 µs pulse, but it could Serial.println ("Shutter test ...");
probably go a bit shorter, as it takes around 5 µs attachInterrupt (digitalPinToInterrupt (2), shutter,
to enter and leave an ISR. CHANGE);
volatile boolean started; } // end of the setup
volatile unsigned long startTime;
volatile unsigned long endTime; void loop () {
if (endTime) {
// interrupt service routine Serial.print ("Shutter open for ");
void shutter () { Serial.print (endTime - startTime);
if (started) Serial.println (" microseconds.");
endTime = micros (); endTime = 0; } // end of if statement
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
14 14
Microprocessor and Embedded Systems

Advantages and Disadvantages of USART


Where leaders are created

 Advantages
• Hardware complexity is low.
• As this is one to one connection between two devices, software addressing
is not required.
• Due to its simplicity, it is widely used in the devices having 9 pin
connectors.
 Disadvantages
• It is suitable for communication between only two devices.
• It supports fixed data rate between devices wanting to communicate
otherwise data will be garbled (distorted ,unclear).
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
15 15
Microprocessor and Embedded Systems

Serial Peripheral Interfaces (SPI)


Where leaders are created

• SPI is a synchronous data communication.


• SPI uses 4 pins in Port B:
• SS/PB2 – Slave Selection pin, this pin on each peripheral enables the
Master to enable and disable a slave or peripheral device.
• MOSI/PB3 – Master Out Slave In, the Master line for sending data to the
peripherals (Slaves), this pin enables to Master drive a slave.
• MISO/PB4 – Master In Slave Out, the Slave line for sending data to the
master, this pin enables the Master to receive any slave data.
• SCK/PB5 – The clock pulses which synchronize data transmission generated
by the Master.
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
16 16
Serial Peripheral Interfaces
Where leaders are created
Microprocessor and Embedded Systems

(SPI)
• Connection using SPI is in the Master-Slave configuration.
• Master – Normally, is the ATmega328. Master initiates the data transfer. SPI clock is
also generated by master.
• Slave – Consists of 1 or more SPI I/O peripherals. The slave transfers data as a
reaction to master.

February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
17 17
Serial Peripheral Interfaces Microprocessor and Embedded Systems

(SPI)
Where leaders are created

• Connection using SPI is in the Master-Slave configuration.

SPI Communication
Schematic
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
18 18
Serial Peripheral Interfaces Microprocessor and Embedded Systems

(SPI)
Where leaders are created

• 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

Pin connections of these two Arduinos 19


February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan 19
SPI Arduino Libraries
Microprocessor and Embedded Systems
Where leaders are created

• SPI.begin() – Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, set SCK & MOSI
low, & SS high. Must be written in setup()
• SPI.end() – Disables the SPI bus.
• SPI.setBitOrder(order) – Sets the order of the bits shifted out of and into the SPI bus, either
LSBFIRST or MSBFIRST.
• SPI.setClockDivider(divider) – Sets the SPI clock divider (SPI_CLOCK_DIVn, n = 2, 4, 8, 16, 32,
64, or 128). The default setting is SPI_CLOCK_DIV4, which sets the SPI clock to 4 MHz for Uno
• SPI.setDataMode(mode) – Sets the SPI data mode: clock polarity and phase. Available modes:
SPI_MODE0 – SPI_MODE3. refer to arduino.cc
• SPI.transfer(val) – Transfers one byte over the SPI bus, both sending and receiving. val: the
byte to send out.
• Returns: the byte read from the bus.
• SPI.beginTransaction(SPISettings(speedMaximum, dataOrder, dataMode)) − speedMaximum
is the clock, dataOrder(MSBFIRST or LSBFIRST), dataMode(SPI_MODE0, SPI_MODE1,
SPI_MODE2, or SPI_MODE3).
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
20 20
SPI Examples SPI as SLAVE Microprocessor and Embedded Systems
Where leaders are created #include <SPI.h>
SPI as MASTER char buff [50];
#include <SPI.h>
volatile byte indx;
volatile boolean process;
void setup (void) { void setup (void) {
Serial.begin(115200); //set baud rate to 115200 for USART Serial.begin (115200);
digitalWrite(SS, HIGH); // disable Slave Select pinMode(MISO, OUTPUT); // have to send on master in so it set as output
SPCR |= _BV(SPE); // turn on SPI in slave mode
SPI.begin (); indx = 0; // buffer empty
SPI.setClockDivider(SPI_CLOCK_DIV8);//divide the clock by 8 process = false;
} SPI.attachInterrupt(); // turn on interrupt
}
void loop (void) { ISR (SPI_STC_vect) // SPI interrupt routine {
byte c = SPDR; // read byte from SPI Data Register
char c; if (indx < sizeof buff) {
digitalWrite(SS, LOW); // enable Slave Select buff [indx++] = c; // save data in the next index in the array buff
// send test string if (c == '\r') //check for the end of the word
process = true;
for (const char * p = "Hello, world!\r" ; c = *p; p++) { }
SPI.transfer (c); }
Serial.print(c); void loop (void) {
if (process) {
} process = false; //reset the process
digitalWrite(SS, HIGH); // disable Slave Select Serial.println (buff); //print the array on serial monitor
delay(2000); indx= 0; //reset button to zero
}
} }
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
21 21
SPI Modes
Microprocessor and Embedded Systems
Where leaders are created

We have four modes of operation in SPI as follows −


• Mode 0 (the default) − Clock is normally low (CPOL = 0), and the data is sampled on the
transition from low to high (leading edge) (CPHA = 0).
• Mode 1 − Clock is normally low (CPOL = 0), and the data is sampled on the transition from high
to low (trailing edge) (CPHA = 1).
• Mode 2 − Clock is normally high (CPOL = 1), and the data is sampled on the transition from
high to low (leading edge) (CPHA = 0).
• Mode 3 − Clock is normally high (CPOL = 1), and the data is sampled on the transition from low
to high (trailing edge) (CPHA = 1).
• SPI.attachInterrupt(handler) − Function to be called when a slave device receives data from
the master.
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
22 22
SPI Hardware Structures
Microprocessor and Embedded Systems
Where leaders are created

The SPI Control Register (SPCR) has 8 bits, each bit position may take values.

SPIE SPE DORD MSTR CPOL CPHA SPR1 SPR0

• SPIE - Enables the SPI interrupt when 1


• SPE - Enables the SPI when 1
• DORD - Sends the data: Least (LSB) and Most (MSB) Significant Bit first when 1 and
0, respectively
• MSTR - Sets the Arduino in Master mode when 1, Slave mode when 0
• CPOL - Sets the data clock to be idle when high if set to 1, idle when low if set to 0
• CPHA - Samples the data on the clock’s falling edge when 1, rising edge when 0
• SPR1 and SPR0 - Sets the SPI speed: 00 = fastest (4 MHz), 11 = slowest (250 kHz)
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
23 23
SPI Examples SPI as MASTER Microprocessor and Embedded Systems
Where leaders are created
void loop() {
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 //We sent the data here and wait for the response from device
char receivedValue = SPI.transfer(data);
it //HIGH to prevent to start communication right
//And then write the answer to the serial port
away Serial.println(receivedValue);
digitalWrite(SlaveSelection, HIGH); //Start the //Disable slave Arduino with setting the Slave Selection pin to 5 V
SPI //communication. digitalWrite(SlaveSelection, HIGH);
SPI.begin(); }
} 27, 2024
February Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
24 24
SPI Examples SPI as SLAVE Microprocessor and Embedded Systems
Where leaders are created SPI as SLAVE void loop()
//Slave device of the SPI communication {
#include <SPI.h> delay(1000);
}
char i = 0;
#define SlaveSelection 10 //SPI Interrupt function

void setup() { ISR(SPI_STC_vect) {


//Start the Serial Communication //Here we read the SPI lines, this line will check data for every ASCII
Serial.begin(9600); codes //for 8-bit received data SPDR -> SPI Data Read bit
// initialize SPI : SPDR = i;
pinMode(SlaveSelection , INPUT); // Set Slave Selection as input i ++;
pinMode(13,OUTPUT); // Set clock as output if ( i > 255)
pinMode(11,OUTPUT); // Set MOSI as output i = 0;
pinMode(12,INPUT); // Set MISO as input while(!(SPSR & (1 << SPIF)));
// SPCR - SPI Control Register //Load the received data to the variable
// According to the structure of table we, enable the SPI and Interface char received = SPDR;
SPCR |= 0b11000000; //And send it to the serial communication bus
// SPSR - SPI Status Register Serial.println(received);
SPSR |= 0x00; }
}
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
25 25
Microprocessor and Embedded Systems
Where leaders are created Advantages and Disadvantages of SPI
 Advantages
• It is a simple protocol and hence does not require processing overheads.
• Supports full duplex communication.
• Due to separate use of CS lines, same kind of multiple chips can be used in the circuit design.
• SPI uses push-pull and hence higher data rates and longer ranges are possible.
• SPI uses less power compare to I2C

 Disadvantages
• As number of slave increases, number of CS lines increases, this results in hardware
complexity as number of pins required will increase.
• To add a device in SPI requires one to add extra CS line and changes in software for particular
device addressing is concerned.
• Master and slave relationship can not be changed as usually done in I2C interface.
• No flow control available in SPI.
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
26 26
I2C (Inter-Integrated Circuit): What is
Where leaders are created
Microprocessor and Embedded Systems

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.
February 27, 2024 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.

February 27, 2024 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
February 27, 2024
7 bit addressing and
Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
data
29 transfer of 100 kbps. 29
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 Extension to standard
 5 MHz in ultra-fast mode mode I2C
• 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.
February 27, 2024 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.
February 27, 2024 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.
February 27, 2024 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.
February 27, 2024 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.

February 27, 2024 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).

(1) No device is allowed to acknowledge at the reception of the START byte.


(2) The CBUS address has been reserved to enable the inter-mixing of CBUS compatible and
I2C-bus compatible devices in the same system. I2C-bus compatible devices are not allowed
to respond on reception of this address.
(3) The address reserved for a different bus format is included to enable I2C and other
protocols to be mixed. Only I2C-bus compatible devices that can work with such formats and
protocols are allowed to respond to this address. 35
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan 35
Microprocessor and Embedded Systems
Where leaders are created
Working of I2C in Arduino
• 10-bit Addressing
• One of the reasons that Total Phase decided to use 7-bit addressing for all of its
products was to ensure that 10-bit addressing could be properly handled.
• 10-bit addressing was designed to be compatible with 7-bit addressing, allowing
developers to mix two types of devices on a single bus. When communicating with a
10-bit addressed device, the special reserved address is used to indicate that 10-bit
addressing is being used.

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.
February 27, 2024 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; }
February 27, 2024 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/
February 27, 2024 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:

February 27, 2024 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
February 27, 2024 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
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan 41
Microprocessor and Embedded Systems
Where leaders are created RS232

DB9: 9 pin connectors


February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
42 42
Microprocessor and Embedded Systems
Where leaders are created Summary
 The UART is good for basic, full-duplex data communication between two devices
with a similar clock.
 An SPI is good for full-duplex, high-speed data communication with two or more
peripherals.
 An I2C is good for slow-speed data communication with multiple devices, among
multiple masters over a 2-wire bus.

February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
43 43
Microprocessor and Embedded Systems

Thanks for attending….


Where leaders are created

February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
44 44

You might also like