Embedded Systems Communication Protocol

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

ADDIS ABABA SCIENCE AND TECHNOLOGY UNIVERSITY

COLLEGE OF ENGINEERING
DEPARTMENT OF ELECTROMECHANICAL ENGINEERING

Embedded Individual Assignment

Name: Yeabsera Yismashewa


ID: ETS0673/12

Submitted to: Mr. Yonas T.


February 2024
What is UART?
UART stands for universal asynchronous receiver / transmitter and defines a protocol, or set
of rules, for exchanging serial data between two devices. UART is very simple and only uses two
wires between transmitter and receiver to transmit and receive in both directions. Both ends also
have a ground connection. Communication in UART can be simplex (data is sent in one
direction only), half-duplex (each side speaks but only one at a time), or full-duplex (both sides
can transmit simultaneously). Data in UART is transmitted in the form of frames. The format
and content of these frames is briefly described and explained.
Data Transmission
In UART, the mode of transmission is in the form of a packet. The piece that connects the
transmitter and receiver includes the creation of serial packets and controls those physical
hardware lines. A packet consists of a start bit, data frame, a parity bit, and stop bits.

 Start Bit
 Data Frame
 Parity
 Stop Bits

Steps of UART transmission

1. The transmitting UART receives data in parallel from the data bus.
2. The transmitting UART adds the start bit, parity bit, and the stop bit(s) to the data frame.
3. The entire packet is sent serially from the transmitting UART to the receiving UART.
The receiving UART samples the data line at the pre-configured baud rate.
4. The receiving UART discards the start bit, parity bit, and stop bit from the data frame.
5. The receiving UART converts the serial data back into parallel and transfers it to the data
bus on the receiving end.
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

How to use UART in PIC16F


To use UART (Universal Asynchronous Receiver Transmitter) in the PIC16F controller, you
will need to configure the controller's hardware and software settings. Here's a brief explanation
of the steps involved:
1. Configure the UART Pins: First, you need to configure the appropriate pins for UART
communication. In the PIC16F controller, UART functionality is usually available on
specific pins, such as RC6 (TX) and RC7 (RX). Configure these pins as input or output
using the TRISC register.
2. Configure the Baud Rate: The baud rate determines the speed at which data is
transmitted and received. Set the desired baud rate by configuring the SPBRG (Serial
Port Baud Rate Generator) register. The SPBRG value is calculated based on the desired
baud rate and the controller's clock frequency.
3. Configure UART Control Registers: The UART operation and behavior can be
controlled using several registers. The key registers to configure are:
 TXSTA (Transmit Status and Control Register): Configure the transmit
settings, such as enabling the transmitter, setting the transmission mode, and
enabling interrupts if needed.
 RCSTA (Receive Status and Control Register): Configure the receive settings,
such as enabling the receiver, setting the reception mode, and enabling interrupts
if necessary.
4. Enable UART: Enable the UART module by setting the appropriate bits in the TXSTA
and RCSTA registers. This action allows the UART to start transmitting and receiving
data.
5. Send Data: To send data via UART, you can use the TXREG (Transmit Register)
register. Write the data you want to send into the TXREG register, and the UART module
will automatically transmit it.
6. Receive Data: To receive data via UART, you can read the received data from the
RCREG (Receive Register) register. Check the RCIF (Receive Interrupt Flag) bit in the
PIR1 (Peripheral Interrupt Request 1) register to ensure data is available for reading.
7. Handle Interrupts (Optional): If you want to use UART interrupts, enable them by
setting the appropriate bits in the PIE1 (Peripheral Interrupt Enable 1) register. You will
also need to write the interrupt service routine (ISR) to handle the UART interrupts.
What is SPI?
SPI stands for the Serial Peripheral Interface. It is a serial communication protocol that is used
to connect low-speed devices. It was developed by Motorola in the mid-1980 for inter-chip
communication. It is commonly used for communication with flash memory, sensors, real-time
clock (RTC), analog-to-digital converters, and more. It is a full-duplex synchronous serial
communication, which means that data can be simultaneously transmitted from both directions.

The main advantage of the SPI is to transfer the data without any interruption. Many bits can be
sent or received at a time in this protocol.

In this protocol, devices are communicated in the master-slave relationship. The master device
controls the slave device, and the slave device takes the instruction from the master device. The
simplest configuration of the Serial Peripheral Interface (SPI) is a combination of a single slave
and a single master. But, one master device can control multiple slave devices.

SPI Interface

The SPI protocol uses the four wires for the communication. There are shown in the figure.

1. MOSI: MOSI stands for Master Output Slave Input. It is used to send data from the
master to the slave.
2. MISO: MISO stands for Master Input Slave Output. It is used to send data from the slave
to the master.
3. SCK or SCLK (Serial Clock): It is used to the clock signal.
4. SS/CS (Slave Select / Chip Select): It is used by the master to send data by selecting a
slave.

Advantages of SPI

1. The main advantage of the SPI is to transfer the data without any interruption.
2. It is simple hardware and provides full-duplex communication.
3. There is no need for a unique address of the slave in this protocol.
4. In this, software implementation is very simple.
5. It provides high transfer speed and Signals are unidirectional.
6. It has separate lines of MISO and MOSI, so the data can be sent and received at the same
time.

Disadvantages of SPI

1. Usually, it supports only one master.


2. It does not check the error like the UART.
3. It uses more pins than the other protocol and it can be used only from a short distance.
4. It does not give any acknowledgment that the data is received or not.

Applications of SPI

 Memory: SD Card, MMC, EEPROM, and Flash.


 Sensors: Temperature and Pressure.
 Control Devices: ADC, DAC, digital POTS, and Audio Codec.
 Others: Camera Lens Mount, Touchscreen, LCD, RTC, video game controller, etc.

How to use SPI in PIC16F


To use SPI (Serial Peripheral Interface) in a PIC16F controller, you need to configure the
hardware and software settings. Here's a brief explanation of the steps involved:
1. Configure the SPI Pins: First, you need to configure the appropriate pins for SPI
communication. In the PIC16F controller, SPI functionality is usually available on
specific pins, such as RC3 (SCK), RC4 (SDI), and RC5 (SDO). Configure these pins as
input or output using the TRISC register.
2. Configure the SPI Control Registers: The SPI operation and behavior can be controlled
using several registers. The key registers to configure are:
 SSPSTAT (Master Synchronous Serial Port Status Register): Configure the
data input and output modes, such as whether the data is sampled on the rising or
falling edge of the clock.
 SSPCON (Master Synchronous Serial Port Control Register): Configure the
SPI settings, such as enabling the SPI module, selecting the SPI mode (Master or
Slave), setting the clock polarity and phase, and enabling interrupts if necessary.
3. Configure the SPI Clock Speed: Set the desired clock speed for SPI communication by
configuring the SSPADD (Master Synchronous Serial Port Address) register. The
SSPADD value is calculated based on the desired clock speed and the controller's clock
frequency.
4. Enable SPI: Enable the SPI module by setting the appropriate bits in the SSPCON
register. This action allows the SPI module to start transmitting and receiving data.
5. Send and Receive Data: To send and receive data via SPI, you can use the SSPBUF
(Master Synchronous Serial Port Buffer) register. Write the data you want to send into
the SSPBUF register, and the SPI module will automatically transmit it. To receive data,
read the received data from the SSPBUF register.
6. Handle Interrupts (Optional): If you want to use SPI interrupts, enable them by setting
the appropriate bits in the PIE1 (Peripheral Interrupt Enable 1) register. You will also
need to write the interrupt service routine (ISR) to handle the SPI interrupts.
What is I2C Protocol?
The I2C stands for the inter integrated controller. The I2C protocol is a serial communication
protocol that is used to connect low-speed devices. For example, EEPROMs, microcontrollers,
A/D and D/A converters, and input/output interfaces. It was developed by Philips’s
semiconductor in 1980 for inter-chip communication. Almost all major IC manufacturers now
use it. It is a master-slave communication in which you can connect and control multiple slaves
from a single master. In this, each slave device has a particular address. It supports various data
rates according to versions ranging from 100 Kbps, 400 Kbps, 1 Mbps to 3.4 Mbps. It is
synchronous communication like SPI.

Why is I2C used?

Compared to protocols like USB, ethernet, Wi-Fi, or Bluetooth, I2C is much slower. However, it
is a lot simpler and uses significantly less hardware and system resources. This makes it ideal for
communication between microcontrollers or microcontrollers and sensors. Much like the SPI
(Serial Peripheral Interface), I2C is intended only for short distances.

I2C interface

I2C protocol uses only two cables for the communication in which one cable is used for the data
(SDA), and another cable is used for clock (SCL). Both cables are required to pull up with a
resistor for + Vdd. It can be used to link two I2C buses with different voltages.

Applications of I2C

1. Reading certain memory ICs and Reading hardware sensors


2. Accessing DACs and ADCs
3. Transmitting and controlling user-directed actions
4. Communicating with multiple micro-controller

Advantages of I2C

1. It provides flexible data transmission rates.


2. It provides long-distance communication than SPI.
3. Each device on the bus is controlled independently.
4. It increases the complexity of firmware or low-level hardware.
5. This protocol imposes overhead that also reduces throughput.
6. This protocol requires only two cables.
7. It can accommodate several master interactions through arbitration and collision
detection.
Disadvantages of I2C

1. The complexity of hardware increases when no. of master/slave devices are high in the
circuit.
2. It provides a half-duplex mode for communication.
3. It is managed by the stack.
4. Many devices have multiple addresses stored, which can cause conflicts.

How to use I2C in PIC16F

To use I2C (Inter-Integrated Circuit) in a PIC16F controller, you need to configure the hardware
and software settings. Here's a brief explanation of the steps involved:
1. Configure the I2C Pins: First, you need to configure the appropriate pins for I2C
communication. In the PIC16F controller, I2C functionality is usually available on
specific pins, such as RC3 (SCL) and RC4 (SDA). Configure these pins as input or
output using the TRISC register.
2. Configure the I2C Control Registers: The I2C operation and behavior can be
controlled using several registers. The key registers to configure are:
 SSPSTAT (MSSP Status Register): Configure the I2C settings, such as setting the
I2C mode (Master or Slave), enabling slew rate control, and setting the
acknowledge bit.
 SSPCON (MSSP Control Register): Configure the I2C settings, such as enabling
the I2C module, selecting the I2C mode (Master or Slave), enabling interrupts if
necessary, and setting the clock frequency.
3. Configure the I2C Clock Speed: Set the desired clock speed for I2C communication by
configuring the SSPADD (MSSP Address) register. The SSPADD value is calculated
based on the desired clock speed and the controller's clock frequency.
4. Enable I2C: Enable the I2C module by setting the appropriate bits in the SSPCON
register. This action allows the I2C module to start transmitting and receiving data.
5. Write and Read Data: To write and read data via I2C, you can use the SSPBUF (MSSP
Buffer) register. Write the data you want to send into the SSPBUF register, and the I2C
module will automatically transmit it. To receive data, read the received data from the
SSPBUF register.
6. Handle Interrupts (Optional): If you want to use I2C interrupts, enable them by setting
the appropriate bits in the PIE1 (Peripheral Interrupt Enable 1) register. You will also
need to write the interrupt service routine (ISR) to handle the I2C interrupts.
.

You might also like