Serial Communication 

Introduction to Communication:-

Communication refers to the process of exchanging ideas, thoughts, and experiences between two or more persons. In IOT it refers to the process of exchange of data between two devices.

 There are two types of communication:-

1. Parallel Communication:-

Parallel Communication is Communication in which Data is transmitted simultaneously across multiple data lines.

2. Serial Communication:-

Serial Communication is a Communication in which Data is transmitted one bit at a time across a single line.

What is the Baud Rate?

Baud rate is the rate of data at which it is transmitted over a communication channel. It is measured in bits per second.

The 8051 microcontroller uses the UART protocol for communication.

What is UART?

UART stands for: Universal Asynchronous Receiver/Transmitter                                                

It is a hardware communication protocol used for serial communication between devices.

UART converts parallel data from a microcontroller into serial form for transmission and vice versa for reception.

Key Feature of UART

Asynchronous: No clock line is shared between sender and receiver.

Full Duplex: Can send and receive data simultaneously.Uses 2 main lines:

TX (Transmit) – Sends data out.

RX (Receive) – Receives data in

Serial Communication-Related Registers and Their Functions:

1. SCON:- SCON (Serial Control Register) is an 8-bit Special Function Register (SFR) in the 8051 microcontroller used to configure and control serial communication (UART).

SM0

 SM1

SM2

REN

TB8

RB8

TI

RI

SM0 & SM1:- These two bits are used together to select one of the four serial communication modes in 8051. They control how many bits are transmitted/received and how the baud rate is generated.

Serial Communication Mode Selection Table :

Mode Name Duplex Baud Rate Data Frame Notes
0 Shift Register Half-duplex Fixed 8-bit Clock on TxD; not standard UART
1 8-bit UART Full-duplex Variable Start + 8-bit + Stop Most common mode; standard UART
2 9-bit UART Full-duplex Fixed Start + 8-bit + 9th bit + Stop 9th bit used for multiprocessor communication
3 9-bit UART Full-duplex Variable Start + 8-bit + 9th bit + Stop Same as Mode 2, but with variable baud rate

SM2:- It is used for microcontroller-to-microcontroller (MC to MC) communication — especially in multiprocessor setups. 

REN(Receiver Enable):- Controls whether the 8051 can receive data or not. 

REN=1: Receiving enabled

REN = 0: Receiving disabled.

TB8(Transmit bit 8):- Used only in 9-bit data modes (Mode 2 and 3).  This is the 9th bit that gets transmitted.
RB8(Receive bit 8):- Also used in 9-bit modes to store the 9th bit of received data. Can be used for multiprocessor address detection.
TI(Transmit Interrupt Flag):- Set to 1 when one byte of data has been transmitted (via SBUF).  Must be cleared manually in code to send the next byte.
RI(Receive Interrupt Flag):- Set to 1 when one byte of data is received (into SBUF).  
Must be cleared manually after reading the received data.
2. SBUF:- SBUF (Serial Buffer) is an empty 8-bit register used for serial communication in the 8051 microcontroller. It plays a central role in UART-based transmission and reception.
  • When data is written to SBUF, it is placed into the transmit buffer and automatically sent out through the TXD (P3.1) pin.
  • When data is received via RXD (P3.0), it is automatically stored in the receive buffer part of SBUF, and the RI flag is set.
Although it appears as one register, SBUF behaves like two separate internal registers:
  • One for transmitting (write-only)
  • One for receiving (read-only)
SBUF:-

 

 

 

 

 

 

 

 


Task 1:- Print a single character on Serial monitor.

Code:-

#include<reg51.h>
void uart_begin();
void uart_transfer(unsigned char a);
void main()
{
uart_begin();
uart_transfer('s');
while(1);
}
void uart_begin()
{
SCON=0x50;
TMOD=0x20;
TH1=0xFD;
TR1=1;
}
void uart_transfer(unsigned char a)
{
SBUF=a;
while (TI==0);
TI=0;
}


Output:-



Task 2:- Print a String on Serial monitor.

Code:-

#include<reg51.h>
void uart_begin();
void uart_transfer(unsigned char a);
void uart_str(unsigned char b[]);
void main()
{
uart_begin();
uart_str("HI WELCOME MY BLOG");
while(1);
}
void uart_begin()
{
SCON=0x50;
TMOD=0x20;
TH1=0xFD;
TR1=1;
}
void uart_transfer(unsigned char a)
{
SBUF=a;
while (TI==0);
TI=0;
}
void uart_str(unsigned char b[])
{
int i;
for (i=0;b[i]!='\0';i++)
{
uart_transfer(b[i]);
}
}


Output:-



Task 3:- Print The received message on 4 bit lcd

Code:-

#include<reg51.h>
#define datapins P2
sbit RS=P2^4;
sbit E=P2^5;
int len=0;
void delay(unsigned int t);
void lcd_begin();
void lcd_command(unsigned int cmd);
void lcd_message(unsigned char ch);
void lcd_string(unsigned char a[]);
void uart_begin();
unsigned char uart_receive();
void main()
{
lcd_begin();
uart_begin();
lcd_command(0x01);
while(1)
{
if (len<16)
{
for (len=0;len<16;len++)
{
lcd_message(uart_receive());

}
}
else
{    lcd_command(0xC0);
for(len=17;len<32;len++)
{
lcd_message(uart_receive());
}
lcd_command(0x01);
len=0;
}
}
}
void lcd_begin()
{
lcd_command(0x28);
lcd_command(0x01);
lcd_command(0x0F);
lcd_command(0x80);
}
void lcd_command(unsigned int cmd)
{
datapins=(cmd>>4);
RS=0;
E=1;
delay(1);
E=0;
datapins=cmd;
RS=0;
E=1;
delay(1);
E=0;
}
void lcd_message(unsigned char ch)
{
datapins=(ch>>4);
RS=1;
E=1;
delay(1);
E=0;
datapins=ch;                             
RS=1;
E=1;
delay(1);
E=0;
}
void lcd_string(unsigned char a[])
{
unsigned int i;
for(i=0;a[i]!='\0';i++)
{
lcd_message(a[i]);
}
}
void delay(unsigned int t)
{
int i,j;
for(i=0;i<=10*t;i++)
{
for(j=0;j<=1257;j++);
{
}
}
}
void uart_begin()
{
SCON=0x50;
TMOD=0x20;
TH1=0xFD;
TR1=1;
}
unsigned char uart_receive()
{
while (RI==0);
RI=0;
return SBUF;
}

Output:-