Table of Contents
Communication Protocols in Embedded System
Serial communication
In serial communication, we have only one line for transmitting and receiving data which is why it is half-duplex. It is best for high frequencies as it uses only a single bus or channel for communication, but it can be a bit slower than parallel as it sends only a single bit per clock pulse also because of the single wire here we have fewer chances of cross-talks. It can detect the error as well.
When transferring a bit quick change in voltage are required like for 5v OS, 0 bit communicated as a short pulse is 0v, and 1 bit communicated by a short pulse of 5v. In serial communication, the first bit is the most significant bit and the last one is the least significant bit.
Parallel Communication
In parallel communication, we have a number of lines or buses equal to the number of bits we are transferring. We call it full duplex as the line for transmitting and receiving bits is different. It is faster for small frequencies as all bits are sent at once but as we have to use multiple wires it is costlier also here, we have lots of chances of cross-talks.
As in serial, here also we have the first bit is the most significant bit and the last one is the least significant bit.
How do they convert?
The protocols we have read above communicate in serial form, but they get the data from their peripheral device in parallel form. How do bits get converted into serial form to parallel form? Suppose we have 4 bits, to convert them from parallel to serial we need a Parallel to Serial converter, In the same way, if we want to convert parallel from series, we need a serial to parallel converter. Here comes the D Flip-Flop which is specially designed for such conversions.
What is Flip-Flop?
A basic memory element or basic digital memory circuit is known as Flip-Flop. It is some medium in which one bit of information (1 or 0) can be stored or retained until it\’s necessary.
- As one flip-flop can store one bit of information. To store multiple bits, we need multiple flip-flops.
- The group of flip-flops, which are used to store the binary data is known as register.
Flip flop circuits are classified into four types based on their use:
- D-Flip Flop
- T- Flip Flop
- SR- Flip Flop
- JK- Flip Flop.
Here for conversion between Serial and parallel bits of data D-Flip Flop is used.
Now, what is D Flip-Flop?
D flip-flop is also known as Data or Delay Flip-Flop. It has a single D input and a clock input C which is why we call it a D flip-flop. Or for the word delay, it describes what happens to the data at input 0. The data at D input is delayed by one clock pulse before it gets to the output Q.
There are two different ways of conversions:
- Parallel to Serial conversion.
- First, we need Flip-Flops equivalent to the number of bits. Then we will put the multiplexer in between the Flip-Flop.
Multiplexer- It is a combinational circuit that takes input from one of many input lines(parallel) and directs it to a single output line(serial).
It has two inputs:
1) From the previous Flip-Flop
2) From Parallel bit data.
Now we will load the data which will be transferred one by one.Here msb will be transferred first.
- Serial to Parallel conversion.
- First, we need Flip-Flops equivalent to the number of bits. This time we won\’t put the multiplexer in between the Flip-Flop.
- We need to store all the bits at first for this we will pull the clock signal low. Once all bits are loaded, we will pull the clock signal high, and it will shift the LSB to the input of Flip-Flop 1 and output of Flip-Flop 0. Same way all bits gets transferred.
- Now all the bits are stored in these Flip-Flop and as shown above all the Flip-Flop will transfer the bits at once the way it happens in parallel..
Overview about Serial Communication Protocols in MCU
UART Protocol
What is UART Protocol?
UART stands for Universal Asynchronous Receiver/Transmitter. As we can understand by the name it says it receives and transmits asynchronously. Asynchronously means it doesn\’t use clock pulse to synchronize the transmitter and the receiver.
In UART, the transmitter takes the signals in parallel from a controlling device[ex-CPU] then it sends the bits to a receiver in serial then the receiver converts those bits in parallel again. UART follows one master one slave approach as it uses only 2 wires for communication. It has one transmitter and one receiver.
How does it work?
As I, ve mentioned above it transmits asynchronously hence it uses a start and stops a bit, in it contains main data and a parity bit. The parity bit is only needed if the device needs to check the error.
When the device wants to start the communication, it pulls the transmitting line low which means it sends a 0 bit and receiving device understands that the transmitter wants to send the data. When the transmission line goes lows it stays low for one clock pulse and then it starts sending the data of 8 bits then the 9th one is a parity bit, the last one is stopping bit it stays high for the one clock pulse until the transmitter sends a low bit to start another frame.
To get, in detail understanding on UART Protocol, refer to this blog.
SPI Protocol
What SPI Protocol is?
SPI stands for Serial Peripheral Interface. Just like UART it also uses Serial Communication for transmitting and receiving data but instead of asynchronous it uses synchronous communication protocol for transmission.
Synchronous means the transmitter and the receiver must have the same clock signal. It uses a Full duplex communication protocol which means it can send and receive at the same time. SPI follows one Master multiple slave configuration.
This protocol has 4 parts
1. MOSI-Master Out Slave In
Here Master sends data to the slave and the slave receives the data. Master sends data to each device it is connected to via a single wire.
2. MISO-Master In slave out
Here Slave sends the data to the master and the master receives the Data. Master receives the data from each device it is connected to through a single wire.
3. CLK-Clock Signal
It is important for synchronizing the data when the master sends or receives it.
4. CS/SS-Chip select/slave select
We have different wires connected to the chips. Suppose we have 3 chips connected. Then the master would have three wires cs1,cs2,cs3 connected from chip1, chip2,chip3 individually.
To get in detail understanding on SPI Protocol, refer this blog.
I2C Protocol
I2C stands for the inter-integrated circuit. If you have read carefully about SPI and UART you will find I2C got the best features of these two.
It follows multi-master protocol as multiple masters through a single slave or to multiple slaves. It is a half-duplex communication protocol since it has only one line for transferring and receiving data. It is also called Two Wired Interface as it uses two wires for communication.
This protocol has 2 parts
- SDA: For sending bits
- SCL: For synchronizing the data.
How does it work?
In I2C as we have multiple slaves, every slave must have a different address before sending a bit master will address or call the receiver. If we have multiple masters then the master will check the clock signal. If it is high it will send the data or if it is low then it means another master is sending the data at the moment.
To get more in detail understanding of I2C protocol, refer to this blog.
Conclusion Para
- In the end, if you want to use two devices then UART works best and fastest.
- If you have multiple slaves and one master then I would suggest going for SPI
- And if you want to connect one slave with multiple masters then I2C is the fastest as it lowers the wirings as well.
Other blogs to explore
About Microcontrollers
About IoT
HC-05 Bluetooth Module
Table of Contents In our era, people are more likely to use wireless devices because it makes using them much easier. HC-05 is a Bluetooth module used for wireless communication. It uses Serial communication to communicate. Its work is based on UART. Usually, it is used to connect small devices like a mobile phone or laptop and exchange files over the range of less than 10m. You can also communicate between two microcontrollers like Arduino through this. However, this module does not transfer multimedia like photos or songs. It can take up to 4-6V of power supply. It supports baud rate of 9600, 19200, 38400, 57600, etc. Refer to these blogs if you feel stuck. Serial Communication UART HC-05 Default Settings Specifications How to connect HC-05 module HC-05 Module PIN OUT of HC-05 Connection or HC-05 & Arduino Modes of Communication Data Mode: This mode is used to communicate with other Bluetooth devices wirelessly. Command mode: AT Command mode is where we can change the default device settings. Let\’s see an example here for both of the modes, starting with Data Mode. Bluetooth communication between Devices (Data Mode) Here we will connect our module with Arduino and our phone. We will send signals from the phone to the HC-05 module wirelessly which will be displayed on the serial monitor. If we will send 1 it will turn on the LED or If we will send or it will turn off the LED. Components required: Arduino Uno LED Jumper wires HC-05 Bluetooth An android phone Steps. Follow the steps mentioned above to connect your module. Now follow these steps to make the circuit turn on/off the LED. Connect the long leg of the led with the resistor. The resistor should be connected with any digital pin, let\’s say pin 13. Connect the short leg with the ground. 3. Now we are using the same TX, RX pin for communication. For this, First, we need to remove RX, TX wire. We did this because Arduino Uno has only 1 pair of UART pins and by them, it receives the data from the IDE. We are aware that Arduino uses the UART protocol for communication. If we connect these pins with the HC-05 module while uploading we will get an error as it will clash. Upload the code to the Arduino and reconnect the pins. int ledP=13;int dtime=1000;int readData;void setup() { // put your setup code here, to run once: pinMode(ledP,OUTPUT); Serial.begin(9600);}void loop() { // put your main code here, to run repeatedly:while(Serial.available()==0) {} // reading the data from the serial monitorreadData = Serial.parseInt();Serial.println(readData);if(readData==0){ digitalWrite(ledP,LOW);}if(readData==1){ digitalWrite(ledP,HIGH);} To send the signals via phone we need to install an app. I use an Arduino Bluetooth controller. Pair it, add the password which is usually 1234 or 0000. Changing the default settings of HC-05 (Command mode) AT Command mode is where we can change the default device settings. Steps to use AT commands Connect your Arduino Board to the Pc. Upload the bare minimum code to Arduino without any breadboard connection. Unplug the Arduino from the computer. Connect the HC-05 module to the Arduino as below. Press and hold the push-button on the module for 1-2 sec and while holding it connect the Arduino to the PC. Now you’ll see the red light will blink in 1-2 sec intervals which shows AT command mode is active. Now select the baud rate to 38400. We do that because 9600 is slow for high-speed transmission. It can go even higher according to the HC-05 reference. Set the box next to the baud rate from newline to both NL(newline – n) & CR(carriage return – r). Now we will use AT commands. First, type At on the Serial Monitor and send. If you will see an OK then everything is fine. To change the name of the module we use AT+NAME=HC-05Devanshi. To change the Password of the module we use AT+PSWD=\”9876\” You can refer to this for AT commands. Applications of HC-05 module Robots Wireless communication between two microcontrollers Communication with Mobile and Laptop Author: Kunal Gupta
SPI Peripheral In STM32F103
Overview So, in this blog we will be covering another alternate functionality of GPIO pins i.e SPI (Serial Peripheral Interface). Previously we hve covered following peripherals implementation in STM32F103 MCU’s. ADC(Analog To Digital Converter) in STM32F103 UART Peripheral in STM32F103 GPIO Peripheral in S32K144 MCU Clock Peripheral in STM32F103 PWM on STM32F103 SPI is a synchronous and full duplex communication between a master and several slave devices. It is used in devices or sensors in which speed is a priority . It operates at data transmission rate 8 Mbits or more. The protocol uses 3 or usually 4 wires for data transmission and receiver .It is used by various sensors and modules such as OLED Display, BMP280 , RC522 , DAC , Shift Registers etc. SPI Theory The SPI uses 2 pins for data transfer SDIN and SDO , SCLK clock for synchronization of data transfer between 2 chips, CE chip select that is used to initiate and terminate the data transfer. SDI = MOSI SDO = MISO SCLK = SCK CE = SS MOSI: MASTER OUT SLAVE IN This pin is used to send data from master to slave MISO: MASTER IN SLAVE OUT Lorem ipsum dolor sit amet, consectetur adipisi cing elit, sed do eiusmod tempor incididunt ut abore et dolore magna SCK: SERIAL CLOCK This is used to generate clock to synchronize data transfer between the master and slave device . This is generated by master SS:SLAVE SELECT Used to select the particular slave to send data . Besides SPI communication the SPI interface can switch between I2S communication protocol that is a synchronous serial communication interface. It supports 4 audio standards including the I2S Philips standard, the MSB- and LSB-justified standards, and the PCM standard. The operating modes can be full duplex(4 wires) and half duplex (6 wires) Multi Mode Configuration Multiple subnodes can be used with a single SPI main. The subnodes can be connected in regular mode or daisy-chain mode. How Data is Transmitted in SPI Initially SCK is enabled that starts the transmission The master sets the SS line low of the slave Data is usually shifted out with most significant bit first , shifting a new least significant bit into the same register Data in slave side is shifted into least significant bit of the register Hence after all the shifting is done the master and slave has transferred the data If more data is to be exchanged the registers are reloaded and the process is repeated When no more data transmission is there the master stops toggling the SCK and deselects the slave using SS SPI Peripheral Bus Modes Before discussing about the various bus modes we will be discussing the clock phase and polarity i.e CPOL:Clock Polarity and CPHA:Clock Phase and it is the combination of CPOL and CPHA that is referred to as Bus Modes. CPOL = 0 Active state of clock = 1 Idle state of clock= 0 Means the sampling on the first edge CPHA = 0 – Data is captured on the rising edge and output on falling edge CPHA = 1 – Data is captured on the falling edge and output on the rising edge CPOL = 1 Active state of clock = 0 Idle state of clock = 1 Means sampling is on the second edge CPHA = 0 – Data is captured on the falling edge and output on rising edge CPHA = 1 – Data is captured on the rising edge and output on the falling edge SPI Features in STM32F103 Full-duplex synchronous transfers on three lines Simplex synchronous transfers on two lines with or without a bidirectional data line 8- or 16-bit transfer frame format selection Multimaster mode capability 8 master mode baud rate prescalers (fPCLK/2 max.) Slave mode frequency (fPCLK/2 max) Faster communication for both master and slave NSS management by hardware or software for both master and slave: dynamic change of master/slave operations Programmable clock polarity and phase Programmable data order with MSB-first or LSB-first shifting Dedicated transmission and reception flags with interrupt capability SPI bus busy status flag Hardware CRC feature for reliable communication: – CRC value can be transmitted as last byte in Tx mode – Automatic CRC error checking for last received byte Master mode fault, overrun and CRC error flags with interrupt capability 1-byte transmission and reception buffer with DMA capability: Tx and Rx requests SPI Instances in STM32F103 SPI instances vary from microcontroller to microcontroller from 1 in stmf103c6t6a to 6 in stm32f7 each having different pins NSS pulse mode , TI mode and hardware crc calculations SPI1 features PA5 as SCK , PA6 as MOSI and PA7 as MISO SPI2 features PB3 as SCK, PB4 as MISO and PB5 as MOSI. NSS Management in SPI protocol for STM32F103 NSS line can to be driven via 2 modes Software Mode- SS is driven internally by firmware Hardware Mode – A dedicated GPIO pin is used to drive the SS line Also NSS features NSS output and output disabled mode. Output mode is used only when device operates in master mode and it is disabled allowing mutli master capability NSS hardware mode must be used in TI mode . CPHA and CPOL are forced to conform to Texas Instrument (TI) protocol requirements. In this NSS signal pulses at the end of every transmitted byte APPLICATIONS OF SPI PROTOCOL Application 1 Memory Devices- SD-Card, MMC, EEPROM and FLASH Application 2 Sensors- Temperature and pressure (BMP280) Application 3 Control Devices -ADC, DAC, Audio Codec Application 4 Others- Camera Lens , RTC, LCD , Touch Screen RFID Module interfacing with STM32F103 W25Q SPI Flash Memory ST77389 LCD Display with STM32F103 NRF24L01 RF Module with STM32F103 How to Configure SPI Peripheral for STM32F103 We would be using STM32 HAL and STM32CubeIDE for using the SPI peripheral in STM32F103 in this blog tutorial series. SPI BLOCK DIAGRAM SPI CONFIGURATION ALERNATE FUNCTION MAPPING CONFIGURATION IN STM32CUBEIDE FIG 1- Selecting MOSI, MISO , SS and SCK pins
Servo Control Using PWM(MCAL Driver)
Interfacing MG90 Servo Motor using PWM Driver of Autosar MCAL Layer via ElecronicsV3 Board (S32K144 MCU)
Autosar ADC MCAL Layer Explanation using ElecronicsV3 Board
In this blog, get to know how to use ADC Driver in automotive MCU’s via Autosar MCAL Layer
How to start with Microcontrollers?
What is Microcontroller Technology??? How to start learning about Microcontrollers technology
Author: Kunal Gupta
Author