Serial Communication Protocols

There are some standardized synchronous and asynchronous protocols that apply to serial communication, and in the detailed description we will examine the UART, SPI and I2C protocol. These allow devices to communicate with each other or with a main controller. Let's start by giving information about synchronous and asynchronous communication first;

Synchronous Communication

When performing synchronous communication, the data bit sent and the data bit received must be in harmony with each other. Devices that will perform communication must work simultaneously. That is, the receiver and transmitter must be on the same clock.

Asynchronous Communication

A specific clock is not needed to communicate asynchronously. Data can be transmitted at any time. It is carried out using certain standards and has a slower transmission than synchronous communication.

UART
Spi
I2C

UART Protocol

UART (Universal Asynchronous Receiver Transmitter) is a communication protocol that enables communication between computers and microcontrollers or microcontrollers and peripherals. Since it works as an asynchronous, it does not need any "clock". USART (Universal Synchronous Asynchronous Receiver Transmitter) can work both synchronously and asynchronously. It is a more advanced protocol than UART. Communication works in the same way that makes sense, but USART can also perform synchronous communications. When you look at the datasheet of a newly released microprocessor, we usually see these units as USART units because USART is also designed as a unit that includes UART. USART is capable of moving data with a data length of 5 to 9 bits. However, in general, 8 or 9 bits of use is preferred.

UART-USART Communication

When performing UART communication, baudrate (data transport speed) must be set first. The data migration speed can be in a wide variety of ranges, but the widely used baudrates on the market are 4800, 9600, 57600, 115200 and 921600, although not very preferred for microprocessors. (The 921600 is typically used where fast processing is required.) Baudrate helps us determine how much byte per second our data will carry. For example, if we choose our data migration speed of 115200, this will provide data transmission for us approximately 11520 bytes per second. Data transmission uses a structure as in the following image. In other words, our communication process is terminated by sending data bits after a start bit, then parity bits, and finally end bits. During this process, data length and parity bit may vary as an option. serial communication serial communication In order to use this type of communication, the data transport speeds (baudrate) of the receiver and transmitter must be the same (or there must be values very close to each other.) This is due to minimizing errors that may occur during the transfer. Error margins will not be a problem if they are tolerable (~%1-3) As you can see in the image above, the serial communication communication line at logic 1 (HIGH) level on the transmitter side is first reduced to logic 0 (LOW) for communication to start, which represents our Start Bit. Then we add the data we want to send to the back of the start bit. If we have a parity bit, we will add it and finally pull the end bit (stop bit) required to end the communication to the HIGH level and notify the recipient that the communication has ended. While we do these operations in the transmitter section, the recipient acts in the same way and writes only the data we send to his or her UART Data Register.

How to use it

We use previously defined pins in our microprocessor to communicate with USART. For this we use either a USB-TTL converter or an RS232 module. After connecting both modules to the RX-TX pins in a way that is inverted to our microprocessor's RX-TX pins, we can initiate communication. (i.e. microprocessor TX —> Module RX, microprocessor RX —> Module TX) serial communication

SPI Protocol

It stands for Serial Peripheral Interface, which works as a Full-duplex. (Data and send simultaneously), communication takes place between master and slave devices. Master selects the device he wants to communicate with and communicates with that device. This selection is usually made with the SS (Slave select) pin, which is included in the SPI hardware, the SS pin is determined by the user on the master device. However, the device used in the SS is Slave (the feature that distinguishes it from IC2)

  • SCLK : Serial Clock (output from master). :Used for synchronous serial communication. Creates a square wave for communication. In other words, it has a clock that provides synchrony in SPI communication. The watch signal is generated by the master device.
  • MOSI : Master Output, Slave Input (output from master). Master's output creates the bus where Slave is the input.
  • MISO : Master Input, Slave Output (output from slave). Master's input creates the bus from which Slave exits.
  • FAX : Slave Select (active low, output from master).Slave means select. The master allows the device to select Slave devices. Master's SS pins are selected and determined by the user according to the Slave device to be checked.

serial communication Data transmission occurs in 8-bits. After selecting slave using the CS pin, you can even write the data you want to send from the master device by changing the MOSI pin to Logistics 0 and Logistics 1. For each bit, simply make the CLK pin 0 – 1. The picture below is more clearly visible. serial communication

The SPI library must be installed on the microprocessor to indicate that communication will take place with the SPI. When writing codes, code is written separately for both master and slave devices. There is also a library in Arduino for SPI communication. However, this library arduino has always been prepared with the thought that it will be in a master position.

When writing code to the Slave device, the necessary changes are made to the registers used for the data transfer of the SPI library. These registers are for arduino;

SPDR(SPI Data Register): This register holds the data to be transferred in the SPI.

SPSR(SPI Status Register): Specifies the SPI status of this register. Checks for data to transfer in bit scrolling.

SPCR(SPI Control Register) : The register where SPI startup settings are set. Master Slave settings are made here.

I2C Protocol

I2C is a software protocol that enables communication via 2 pins. It was created by Philips (NXP). An integrated or part can be used with this protocol if there is an I2C system in it, which means that if there is no I2C in a RAM integrated, it cannot be used with this protocol. In the I2C protocol, executive MCAs are called "master" and other managed parts are called "slaves".

Positives

Downsides

It is flexible, you can improve it as you wish by adding many slave and master/slave parts within the system.

Address conflict may occur because they are identified when generating part addresses

It makes an address-based selection , that is, you don't need an extra CS (chip select) pin.

Their speed is limited compared to other parallel communication systems

The connection is simple, if you use it on more than one part, only 2 lines are connected

In some cases putting too much pull-up resistance can lead to space shortages in PCBs

There is ACK and NACK, which are error detection systems. Thus, it is understood whether the procedure is correct. (will be explained in the future)

Works with all parts with this protocol regardless of speed

I2C Protocol Intended Use

The I2C protocol aims to drive parts that normally require a lot of pin separation using only 2 pins. In the I2C protocol, only 2 pins can be allocated and many RAM, EEPROM, RTC, etc. parts can be connected and used on the same line, eliminating the need for extra pins. In the I2C protocol, it can be connected to the line in other MCAs, which can be replaced as both master and slave at the necessary times.

serial communication

I2C Protocol Structure

Basically there is no need for an extra circuit only the SDA and SCL tips are pull-ups with resistors.

The I2C protocol has a speed range of 4.

100 kbit/s

400 kbit/s

1 mbit/s

3.2 mbit/s

However, the parts used must support these speeds.

Although there is a calculation path for the resistors to be used, I will now give the standardized values here, since in order to make the correct calculation, the resistance of the copper paths to the parts to be connected to the I2C protocol, etc. must be calculated. Pull-up resistance values can be selected according to the speed to be used in the table below. The line to be used for the I2C protocol should not be too long. This communication protocol is suitable for short distance.

Mode

Speed

Resistance Range

Standard Mode

100 Khz

5kΩ – 10kΩ

Quick Mode

400 Khz

2kΩ – 5kΩ

High Speed Mode

3.4 Mhz

1kΩ

serial communication