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.
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
2 2
Serial Data Microprocessor and Embedded Systems
Communication
Where leaders are created
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
3 3
Microprocessor and Embedded Systems
Where leaders are created
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
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
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
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
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
12 12
Microprocessor and Embedded Systems
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
• 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
(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
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
• 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
The SPI Control Register (SPCR) has 8 bits, each bit position may take values.
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).
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
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
43 43
Microprocessor and Embedded Systems
February 27, 2024 Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
44 44