Serial Port UART using 8051

feedinco

8051 Serial Port


Setting the serial Port Mode:

The first thing we must do when using the 8051’s integrated serial port is, obviously, configure it. This lets us tell the 8051 how many data bits we want, the baud rate we will be using, and how the baud rate will be determined. 

First, let’s present the "Serial Control" (SCON) SFR and define what each bit of the SFR represents:
 

Bit

Name

Bit Address

Explanation of Function

7

SM0

9Fh

Serial port mode bit 0

6

SM1

9Eh

Serial port mode bit 1.

5

SM2

9Dh

Multiprocessor Communications Enable (explained later)

4

REN

9Ch

Receiver Enable. This bit must be set in order to receive characters.

3

TB8

9Bh

Transmit bit 8. The 9th bit to transmit in mode 2 and 3.

2

RB8

9Ah

Receive bit 8. The 9th bit received in mode 2 and 3.

1

TI

99h

Transmit Flag. Set when a byte has been completely transmitted.

0

RI

98h

Receive Flag. Set when a byte has been completely received.




Additionally, it is necessary to define the function of SM0 and SM1 by an additional table 

SM0

SM1

Serial Mode

Explanation

Baud Rate

0

0

0

8-bit Shift Register

 Oscillator / 12

0

1

1

8-bit UART

Set by Timer 1 (*)

1

0

2

9-bit UART

Oscillator/ 32 (*)

1

1

3

9-bit UART

Set by Timer 1 (*)





(*) Note: The baud rate indicated in this table is doubled if PCON.7 (SMOD) is set. 

The SCON SFR allows us to configure the Serial Port. Thus, we’ll go through each bit and review it’s function

The first four bits (bits 4 through 7) are configuration bits. 




Initialize Uart in 8051 (assembly):
Serial_Init:
        ;Set timer 1 mode to 8-bit Auto-Reload
        mov TMOD,#20H
        ;Enable reception
        ;Set Serial port mode to 8-bit UART
        mov SCON,#50H
        ;Set baudrate to 9600 at 11.0592MHz
        mov TH1,#0FDH
        mov TL1,#0FDH
        ;Start Timer
        setb TR1
        ret

feedinco


Initialize Uart in 8051 ('c'):
#include <reg51.h>.
void serial_init(){
        TMOD = 0x20;
        SCON = 0x50;
        TH1  = 0xFD;
        TL1  = 0xFD;
        TR1  = 1;
}


Sending Data on serial Port:

To Send data to the serial port we just have to move the data in SBUF (serial buffer register) and wait for the Transmit Interrupt flag to be set. While receiving we wait for the Receive interrupt flag to be set and read the data from SBUF register. This can be done as shown below... 



Send/Receive (assembly):

Serial_Send:
        ;wait for last data to be
        ;sent completely
        jnb TI,Serial_Send
        ;clear the transmit interrupt flag
        clr TI
        ;Then move the data to send in SBUF
        mov SBUF,A
        ret
       
Serial_Read:
        ;Wait for Receive interrupt flag
        jnb RI,Serial_Read
        ;If falg is set then clear it
        clr RI
        ;Then read data from SBUF
        mov A,SBUF
        ret

feedinco
Send/Receive ('c'):

void serial_send(unsigned char dat){
        while(!TI);
        TI = 0;
        SBUF = dat;
}
unsigned char serial_read(){
        while(!RI);
        RI = 0;
        return SBUF;
}


Comments