Working of UART Communication

Thread Starter

Parth786

Joined Jun 19, 2017
642
I am reading Uart communication but I do not know whether my concept is right or wrong If something is wrong then please correct me. Uart is use to send/receive data between two devices. let's suppose If i want to send/ receive data from Micro-controller to LCD, Then I can send/receive data using Uart.

My efforts :
I am following this links to understand basic working of uart
https://en.wikipedia.org/wiki/Universal_asynchronous_receiver-transmitter
http://www.idc-online.com/technical...egister_SCON_Of_8051_8031_Microcontroller.pdf
http://www.circuitbasics.com/basics-uart-communication/

8051 timer is used to generate baud rate. 8051 has registers like TMOD, SCON, TH, TL, TCON. Following are registers that are need to be configured to communicate over Uart.
  • TMOD : This register is used to set the mode of Timer0 and Timer1
  • SCON : Serial Control register has various functions. Transmit interrupt and receive interrupt
  • TH1/TL1 : Timer registers for Timer 1 determines the baud rate of UART
  • TCON : This register has various flag and control bits e.g. Timer overflow flags, interrupt edge flags, timer control bits to start/stop the timer.
Uart use Start Bit and Stop Bit for digital communication. Example : 8051 --- Uart communication ----------LCD

...Start Bit - Bit 0 Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7 Stop Bit -Next -Start Bit- Bit 0 Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7 - Stop Bit.....

I don't understand How does 8051 send start Bit then 8 bit data and then Stop Bit. Which part of 8051 send start/stop Bit and which part of 8051 send/Receive 8 bit data to LCD.
 

LesJones

Joined Jan 8, 2017
4,190
As far as I can see an 8051 does not contain a built in uart so you would then have to use an external one. The uarts I have used in the past have had separate sets of transmit and receive data pins (8 bits wide.) These would be connected to the data bus from the 8051 (Probably using an octal tri state buffer chip) You would then chose 4 addresses in the group of addresses that you have chosen for input/output devices You would then decode the address bits on the address bus from bit 2 and above for the addresses that you have chosen. Address bits 0 and 1 would select which register in the uart you were transferring data to or from. (RX data, TX data, RX status and TX status) You would also connect the read and write pins from the 8051 to the uart to strobe data in and out. To transmit a byte you would start by reading the TX staus register to make sure the TX data buffer was empty. When you had confirmed it was empty you would write a byte to the TX data buffer. The uart then sends the byte. You would have first have configured the uart for the number of data bits and stop bits by writing bits to the status registers. To read data you would first test the data available bit in the receive status register. If it is set then you would read the byte from the RX buffer register. You may also want to check the data overrun error bit in the RX ststus register. I have never used any LCD display that accept this type of serial data so you would need to read the data sheet. The clock signals for the uart could be generated by the 8051 or by a separate clock generator. Most microcontrollers have built in uart (Or eusarts) so you dont have to do any address decoding. The clock generation is also built in.
Edit I have just noticed that MrChips has just posted a link to data on one of the UARTS that I used many years ago with Z80 based systems.

Les.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Put aside the 8051 for now. Read and study this first:
Yes I am doing that, I have tried to understand basic working of uart communication. I have gone through some links tutorials But I'm finding it very difficult now.I understand that if we want to initialize Uart , then we have to configure these register's TMOD, SCON, TH, TL, TCON .

I don't understand how does data transmission start and stop and how does data transmit/receive between two devices. Does it happen that the micro-controller sends a one start signal to another device. let's suppose we are sending data to other device, then we send 8 bit data to device and when transmission complete then micro-controller send stop signal to that device
 

MrChips

Joined Oct 2, 2009
30,821
For a basic 8-bit transmission, think of the transmitter as a 10-bit serial shift register. The first and last bits are the START and STOP bits respectively. The transmitter clock shifts the data out of the shift register at the same clock frequency as the baud rate.

The receiver operates in the reverse process as the transmitter. On the reception of the first falling edge of the signal, the receiver clock circuitry counts for ½ of the bit spacing and clocks the first bit (START) into the 10-bit serial shift register. Then in clocks the remaining nine bits at every full bit spacing. Usually, the receiver clock runs at 16 times the baud rate.
 

AlbertHall

Joined Jun 4, 2014
12,347
Generally you just send the data byte to the UART and it automatically handles sending the start and stop bits around the data.
At the receiving end, the start bit triggers the start of reception.
 

Papabravo

Joined Feb 24, 2006
21,227
As far as I can see an 8051 does not contain a built in uart so you would then have to use an external one. The uarts I have used in the past have had separate sets of transmit and receive data pins (8 bits wide.) These would be connected to the data bus from the 8051 (Probably using an octal tri state buffer chip) You would then chose 4 addresses in the group of addresses that you have chosen for input/output devices You would then decode the address bits on the address bus from bit 2 and above for the addresses that you have chosen. Address bits 0 and 1 would select which register in the uart you were transferring data to or from. (RX data, TX data, RX status and TX status) You would also connect the read and write pins from the 8051 to the uart to strobe data in and out. To transmit a byte you would start by reading the TX staus register to make sure the TX data buffer was empty. When you had confirmed it was empty you would write a byte to the TX data buffer. The uart then sends the byte. You would have first have configured the uart for the number of data bits and stop bits by writing bits to the status registers. To read data you would first test the data available bit in the receive status register. If it is set then you would read the byte from the RX buffer register. You may also want to check the data overrun error bit in the RX ststus register. I have never used any LCD display that accept this type of serial data so you would need to read the data sheet. The clock signals for the uart could be generated by the 8051 or by a separate clock generator. Most microcontrollers have built in uart (Or eusarts) so you dont have to do any address decoding. The clock generation is also built in.
Edit I have just noticed that MrChips has just posted a link to data on one of the UARTS that I used many years ago with Z80 based systems.

Les.
The 8051 architecture has always had an onboard UART since they were first introduced in 1978. To directly answer the original question, a write to a specific SFR (Special Function Register) named SBUF triggers the start of a hardware state machine, that clocks out on the TxD pin, a START bit (always a 0), 7 or 8 data bits, an optional parity bit, and one or two stop bits (always 1's). There is a flag associated with the transmitter that says that another byte of data can be loaded into the Transmit Data Register.

The UART state machine is driven by a clock that runs at 16 times the baudrate of the serial transmission. So for 9600 bps you need a clock that runs at 153,600 Hz. In the 8051 architecture there are some crystal frequencies that make generating this cloc very easy. There are some crystal frequencies that make generating this clock much harder.

The original 8051 was designed to run with a 12 MHz. crystal or clock oscillator. If you were willing to slow the crystal down to 11.0592 MHz you could divide that by 72 to get 153,600. Most of the designs I ever did chose crystal frequencies that made baudrate generation easy.
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
The 8051 architecture has always had an onboard UART since they were first introduced in 1978. To directly answer the original question, a write to a specific SFR (Special Function Register) named SBUF triggers the start of a hardware state machine, that clocks out on the TxD pin, a START bit (always a 0), 7 or 8 data bits, an optional parity bit, and one or two stop bits (always 1's). There is a flag associated with the transmitter that says that another byte of data can be loaded into the Transmit Data
This is basic program to send string using uart communication How to make this program workable for LCD 16*2
C:
/* keil compiler */
#include <reg51.h>        /* Include x51 header file */

void UART_Init()
{
    TMOD = 0x20;        /* Timer 1, 8-bit auto reload mode */
    TH1 = 0xFD;        /* Load value for 9600 baud rate */
    SCON = 0x50;        /* Mode 1, reception enable */
    TR1 = 1;        /* Start timer 1 */
}

void Transmit_data(char tx_data)
{
    SBUF = tx_data;        /* Load char in SBUF register */
    while (TI == 0);        /* Wait until stop bit transmit */
    TI = 0;            /* Clear TI flag */
}

void String(char *str)
{
    int i;
    for(i = 0; str[i] != 0; i++)    /* Send each char of string till the NULL */
    {
        Transmit_data(str[i]);    /* Call transmit data function */
    }
}

void main(void)
{
    UART_Init();        /* UART initialize function */
    String("hello");        /* Transmit 'test' */
    while(1);
}
I am thinking, I have to write code for following function, I can write code for following function
  • STEP1: Initialization of LCD.
  • STEP2: Sending command to LCD.
  • STEP3: Writing the data to LCD
 
Last edited:
Top