How to program 8051 microcontroller serial port UART in Assembly and embedded c



Before going to actual implementation of Serial Communication, we first look at some basics about the serial communication.

What is Serial Data Communication?

If the Data transfer is done in bit by bit transfer via a single wire, then we refer it as serial communication.  Where as in parallel communication 8-bits are transfer at a time using 8-bit bus.

Difference between serial and parallel Communication

Parallel communication uses 8-bit, 16-bit or 32 bit bus cables for data transfer. Speed of data transfer is very high. Huge amount of data can be transfer within seconds depend upon processor. But cost of these cables increases as length increases. Hence parallel communication is not implemented for longer distances.
Examples for parallel communication: Data transfer between hard disk and mother board in your PC. Off course the size of the bus may be 8-bit, 16-bit, 32-bit.

Serial communication uses only three wires (Tx, Rx, Gnd), so implementation is easy and cost is also very less. That why it is used in longer distances but data transfer is slow compared to parallel communication.
Examples for serial communication: Communication (Com) ports in your PC, keyboard and mouse is also implements serial communication. Imagine the cost that your PC has keyboard with parallel bus cable. Generally this serial communication is used in wireless devices, example old wireless internet modems.




Serial Communication in 8051 Micro controller:

The 8051 microcontroller contains in-built serial port which provides both synchronous and Asynchronous serial communication.

What is Synchronous serial data communication?

In Synchronous serial data communication, the data is sent along with a clock signal. The Clock signal always synchronizes the two communicating devices. The figure 1.1 shows timing diagram for synchronous data communication between two devices.

Synchronous Data Communication
Figure 1.1 Synchronous Data Communication

What is Asynchronous serial data communication?

Unlike synchronous serial data communication, the two communicating devices are not in synchronizing. So it is difficult to the receiver to detect that when will be the data is coming from the transmitter. Therefore, to give the indication to the receiver device, the transmitters need to frame the data by adding start and stop bits as shown in the figure 1.2.

Asynchronous Data Communication Data Frame Format
Figure 1.2 Asynchronous Data Communication Data Frame Format


When the two devices are not in communication, at receiver side the logic high is called as Mark signal and the logic zero as Space.  The transmitter sends a start bit signal before 8-bit data, finally it add stop bit as shown in data frame format in figure 1.2. The receiver gets its attention by identifying the valid start bit in the serial line and reads the 8-bit data until stop bit is detected.

How the receiver device checks the valid start or stop bit in the Asynchronous serial communication

In asynchronous serial communication the receiver checks its pin continuously for valid start bit. If the receiver see logic low on its pin, then it wait for half the time interval of valid start bit and again checks its pin. If the signal still low then the receiver confirms it’s a valid start bit.

Block Diagram for Serial port control in 8051 micro controller

The Block diagram of the serial port of the 8051 microcontroller is shown in figure in 1.2. The serial port in 8051 microcontroller supports full duplex, synchronous and Asynchronous communication. Because of full duplex the serial port block diagram shows two separate lines for transmitter and receiver. The 8051 serial port internally contains two separate buffers for transmitter and receiver points to single SFR address (0x99).

Block diagram of serial port in 8051 microcontroller

Figure 1.3 Block diagram of serial port in 8051 microcontroller

The transmit buffer (SBUF) is filled with a character by the 8051 program written by us (programmers). Whenever we load some character into the SBUF register, the character immediately transfers to parallel to serial converter. Now parallel to serial convertor sends out the converted bit stream to TXD line with given baud rate frequency. The baud rate can be generated by dividing the CPU clock with certain value (baud rate divisor) calculated and loaded into the timer. Whenever the parallel to serial converter sends out the last bit of character then the transmitter flag TI is set automatically. If TI flag is set to 1, indicates that the last bit of character in transmitter buffer is sent out i.e., the transmitter is empty. Now we can load another character into the buffer. If we load a new character before the present character is sent out completely, then the new character will override the present character. For this purpose, while writing the program for serial transmission in 8051 microcontroller we always check for the TI flag is 1 or not.
Similarly, when the entire bit stream is received by the serial to parallel converter from RXD line will send to received buffer (SBUF). When the receiver buffer is full then the RI flag is set to 1 by indicating the receiver buffer is full. Now our program has to be read the character in the receiver buffer (SBUF).
The SCON Register in 8051 is used to initialize the serial communication setting like baud rate, data length, no. of stop bits etc.,. The figure 1.4 shows the SCON Register with bits.

SCON Register:
SCON register
Figure 1.4 SCON register




SM0, SM1 bits in SCON are used to define the type of the serial communication, baud rate and framing.

SM0
SM1
Mode
Function
0
0
0
Shift register, baud rate = f/12, Synchronous serial communication mode
0
1
1
8-bit UART; baud = variable , asynchronous serial communication mode
1
0
2
9-bit UART; baud = f/32 or f/64, asynchronous serial communication mode
1
1
3
9-bit UART; baud = variable, asynchronous serial communication mode

SM2 - bit is used for multiprocessor communication. Set or cleared by the program to enable multiprocessor communications in mode 2 and 3. When set to 1 an interrupt is generated if bit 9 of the received data is a 1; no interrupt is generated if bit 9 is 0;
 REN – Receiver enable bit. To accept reception of data this bit must be 1;
TB8 – Transmitted bit 8.
RB8 – Received bit 8.
TI – Transmit interrupt flag. This will be enabled when all bits in the transmitted buffer is shifted out.
RI – Receive interrupt flag. This will be enabled when a character is received in the receiver buffer.

How to write a program to transmit a character with 9600 baud rate using 8051 assembly code in polling method? F= 11.0592MHz

Step1: Initialize SCON register
Step2: Calculated baud rate.
Step3: Initialize TMOD register, so that make the timer0 in auto re-load feature.
Step4: Start timer
Step5: Load a character in SBUF.
Step6: wait for TI to be 1.
Step7: end or load new character.

How to initialize SCON register?

Here I am choosing mode 1 (8-bit UART), so that M0 and M1 bit combination is 01. In this program I disable the receiver and multiprocessor communication. And also TI and RI bit are cleared to zero.
Now the data loaded into SCON register = 01000000 = 40H
Assembly Code: MOV SCON, #40H

How to calculate Timer value to be loaded with the given baud rate?

The given baud rate is 9600 bits/sec, the value loaded into timer register is given by
THX = maxCount - (Clock Frequency)/ (12*32* baud rate).
Therefore, THX = 256 - (11.0592M)/ (12*32*baud rate).
THX = 256 – 3 = 253d = FDh

How to initialize timer0 in auto reload mode?

TMOD register

Figure 1.5 TMOD register.

From the figure 1.5, the Timer 0 can be initialized in auto reload feature by making first MSB bit combination would be – ‘10’
TMOD = 00000010b = #02 h.

 Program:
Org 0000h
MOV     SCON, #40h; initialize SCON register
MOV     TMOD, #02h; initialize TMOD register
MOV     TH0, #0FDh;   load timer 0 with FD for 9600 bits/sec baud rate.
SETB TR0; start timer 0
MOV SBUF, ‘A’; load ‘A’ IN SBUF register
Here: JNB TI, Here;  wait here until TI flag is set to one. Means all bits in the TXD buffer is shifted out
CLR TR0; STOP timer0;
END
  

Interrupt method serial communication:
Program: The program is written to accept a received character from serial port using interrupt method. If character is not received, our program itself transmits a character ‘N’ via serial port using polling method.
ORG 0000
LJMP Main_Label    ; redirect the control to some other location
ORG 0023
JNB RI, end; Check whether the interrupt is occurred on the reception of character
MOV A, SBUF; read received character into A register.
End : nop
RETI
ORG 0040h
Main_Label :    MOV    SCON, #50h; initialize SCON register with reception enable
MOV     TMOD, #02h; initialize TMOD register
MOV     TH0, #0FDh;   load timer 0 with FD for 9600 bits/sec baud rate.
SETB TR0; start timer0
Back: MOV SBUF, ‘N’; load ‘N’ IN SBUF register
Here: JNB TI, Here;  wait here until TI flag is set to one.
SJMP Back
END




8051 serial port (RS232) interface circuit diagram












7 comments :

  1. Hello ,
    Ali Afzal here i need help in my project.
    MOV R0,#10000101B/#85H
    MOV A,SBUF
    CJNE A,R0,LOOP
    LOOP:
    SETB P2.0
    IN ABOVE CODE IF DATA IN REGISTER R0 & A ,ARE NOT SAME THEN IT JUMP INTO THE LOOP SUBROUTINE WHILE ME WANT IF BOTH DATA ARE SAME THEN IT SHOULD JUMP INTO LOOP SUBROUTINE.CAN U TELL ME THE COMMAND(LIKE "CJNE") OR THE WAY THROUGH WHICH I CAN FULFILL MY DESIRED RESULT. PLZ MAIL ME ON AT aliafzal_957@hotmail.com

    ReplyDelete
    Replies
    1. Hi Ali! U did every thing in the code is correct, But a small change. I will Provide Two types of programs. First Program code compares the bytes if equal then SET P2.0 bit and then exit. otherwise it will simply exit. Other program code continuously compares the bytes if equal set the P2.0 and exit. Otherwise
      the control stays in the infinite loop.

      1) Code to compares two bytes only one time and set P2.0 if equal else exit the program
      MOV R0,#10000101
      MOV A,SBUF
      CJNE A,R0,loop
      SJMP exit
      loop: SETB P2.0
      exit: END

      2) Code to compares two bytes until both are equal and set P2.0.
      MOV R0,#10000101
      back: MOV A,SBUF
      CJNE A,R0,loop
      SJMP back
      loop: SETB P2.0
      END

      Delete
  2. hi.. i have to interface a wireless receiver in 8051 mc, it will b connected to the single pin of a port (lets say p1.1). i need a asm code for serial transmission of the character. please help me if possible. baud rate 9600.

    ReplyDelete
    Replies
    1. hello anup garg! the serial communication in 8051 can be done by using P3.0 pin (RXD) and P3.1 (TXD). you should interface your wireless receiver to P3.0(RXD) pin. There is another way to make the P1.1 as your receiver pin by writing asm code for P1.1 as custom serial port receiver. i.e., you should write your code to validate the start/stop bit length, and you should validate the data bits.
      Which one you want?

      Delete
  3. Hello

    I need connect serial mode to one compass http://www.robot-electronics.co.uk/htm/cmps10ser.htm
    In this case exist two stop bits and no parity.
    The RX of 8051 connect to TX of cmps10 and TX of 8051connect of RX of cmps10.
    I make this code, but not work.

    VALOR EQU 00H
    MOV TMOD,#00100000B
    MOV TL1,#(256-3)
    MOV TH1,#(256-3)
    SETB TR1
    MOV SCON,#01010000B ;;;;;;;;;; i not sure this !!!!

    CIMA: LCALL PEDE_SERIE
    MOV A,VALOR
    CJNE A,#200,CIMA
    CLR P0.0
    LJMP $

    PEDE_SERIE:
    MOV SBUF,#0*12 ;;;send valor of Angle as a single byte 0-255 for compass
    CLR TI
    JNB TI,$

    CLR RI
    JNB RI,$
    MOV VALOR,SBUF
    RET

    I think this is because two stop bits
    Can you help me?

    Thanks

    ReplyDelete
    Replies
    1. hi Gil Vieira! we don't have two stop bits communication using 8051 or we don't have option to configure the stop bits. have u tried this using hyper terminal in windows? in hyper terminal you may find two stop bits option and make sure that you should interface compass to pc via a max232 IC.

      Delete
  4. very gr8 thank you so much

    ReplyDelete

Thanks for visiting NPEDUCATIONS. We will contact you soon

7 comments :

ali said...

Hello ,
Ali Afzal here i need help in my project.
MOV R0,#10000101B/#85H
MOV A,SBUF
CJNE A,R0,LOOP
LOOP:
SETB P2.0
IN ABOVE CODE IF DATA IN REGISTER R0 & A ,ARE NOT SAME THEN IT JUMP INTO THE LOOP SUBROUTINE WHILE ME WANT IF BOTH DATA ARE SAME THEN IT SHOULD JUMP INTO LOOP SUBROUTINE.CAN U TELL ME THE COMMAND(LIKE "CJNE") OR THE WAY THROUGH WHICH I CAN FULFILL MY DESIRED RESULT. PLZ MAIL ME ON AT aliafzal_957@hotmail.com

rogwil said...

Hi Ali! U did every thing in the code is correct, But a small change. I will Provide Two types of programs. First Program code compares the bytes if equal then SET P2.0 bit and then exit. otherwise it will simply exit. Other program code continuously compares the bytes if equal set the P2.0 and exit. Otherwise
the control stays in the infinite loop.

1) Code to compares two bytes only one time and set P2.0 if equal else exit the program
MOV R0,#10000101
MOV A,SBUF
CJNE A,R0,loop
SJMP exit
loop: SETB P2.0
exit: END

2) Code to compares two bytes until both are equal and set P2.0.
MOV R0,#10000101
back: MOV A,SBUF
CJNE A,R0,loop
SJMP back
loop: SETB P2.0
END

anup garg said...

hi.. i have to interface a wireless receiver in 8051 mc, it will b connected to the single pin of a port (lets say p1.1). i need a asm code for serial transmission of the character. please help me if possible. baud rate 9600.

rogwil said...

hello anup garg! the serial communication in 8051 can be done by using P3.0 pin (RXD) and P3.1 (TXD). you should interface your wireless receiver to P3.0(RXD) pin. There is another way to make the P1.1 as your receiver pin by writing asm code for P1.1 as custom serial port receiver. i.e., you should write your code to validate the start/stop bit length, and you should validate the data bits.
Which one you want?

Gil Vieira said...

Hello

I need connect serial mode to one compass http://www.robot-electronics.co.uk/htm/cmps10ser.htm
In this case exist two stop bits and no parity.
The RX of 8051 connect to TX of cmps10 and TX of 8051connect of RX of cmps10.
I make this code, but not work.

VALOR EQU 00H
MOV TMOD,#00100000B
MOV TL1,#(256-3)
MOV TH1,#(256-3)
SETB TR1
MOV SCON,#01010000B ;;;;;;;;;; i not sure this !!!!

CIMA: LCALL PEDE_SERIE
MOV A,VALOR
CJNE A,#200,CIMA
CLR P0.0
LJMP $

PEDE_SERIE:
MOV SBUF,#0*12 ;;;send valor of Angle as a single byte 0-255 for compass
CLR TI
JNB TI,$

CLR RI
JNB RI,$
MOV VALOR,SBUF
RET

I think this is because two stop bits
Can you help me?

Thanks

Lovakiranvarma Myla said...

hi Gil Vieira! we don't have two stop bits communication using 8051 or we don't have option to configure the stop bits. have u tried this using hyper terminal in windows? in hyper terminal you may find two stop bits option and make sure that you should interface compass to pc via a max232 IC.

Anonymous said...

very gr8 thank you so much

Post a Comment

Thanks for visiting NPEDUCATIONS. We will contact you soon

If you really like this tutorial, Don't forget to give the comment or please subscribe to the RSS feed by submitting your E-mail or like our Facebook page.
Related Posts Plugin for WordPress, Blogger...

 
Powered by Blogger