You are on page 1of 18

8051 UART

• One of the microcontroller features making it so powerful is an


integrated UART, better known as a serial port. It is a full-duplex port,
thus being able to transmit and receive data simultaneously and at
different baud rates (no of bits transmitted per sec).

• When using UART, all the programmer has to do is
• i) to simply select serial port mode and baud rate using SCON register
(Serial Control).
• Ii) When it's done, serial data transmit is nothing but writing to the SBUF
register, while data receive represents reading the same register.
UART
Transmit a data serially:

• Set baud rate for serial data transfer


• TI (transmit interrupt flag in SCON register) is cleared by s/w
instruction CLR T1
• Set mode (M0,M1,M2) of serial data transmission using
SCON register
• The character byte to be transferred serially is written into the
SBUF register.
• Bit is transmitted through Tx pin for every clock pulse
• TI is 1 (after transmission of all bits of data it indicates ready
to send next data)
Receive the data serially:

• Set baud rate for serial data transfer


• RI (transmit interrupt flag in SCON register) is cleared by s/w
instruction CLR R1
• Set mode (M0,M1,M2) of serial data transmission and set
REN (enable receiver) using SCON register
• Bit is received through Rx pin for every clock pulse
• The received data serially is written into the Receiver SBUF
register.
• RI is 1 (after receiving of all bits of data it indicates ready to
receive next data)


SCON (serial control) register
The SCON register is an 8-bit register used to program the start bit,
stop bit, and data bits of data framing, among other things.
• SMO and SMI are D7 and D6 of the SCON register, respectively.
These two bits determine the framing of data by specifying the number
of bits per character, and the start and stop bits.
• SM2 is the D5 bit of the SCON register. This bit enables the
multiprocessing capability of the 8051 and is beyond the discussion of
this chapter. For our applications, we will make SM2 = 0 since we are
not using the 8051 in a multiprocessor environment

SM0 SM1 Serial mode Explanation Baud rate

0 0 0 8-bit Shift Register Osc/12

0 1 1 8 bit UART Set by timer 1

1 0 2 9 bit UART Osc/64

1 1 3 9 bit UART Set by timer 1


• When the REN bit is high, it allows the 8051 to receive data on
the RxD pin of the 8051. As a result if we want the 8051 to both
transfer and receive data, REN must be set to 1. By making REN
= 0, the receiver is disabled.

• When the 8051 finishes the transfer of the 8-bit character, it


raises the TI flag to indicate that it is ready to transfer another
byte. The TI bit is raised at the beginning of the stop bit.

• When the 8051 receives data serially via RxD, it gets rid of the
start and stop bits and places the byte in the SBUF register. Then
it raises the RI flag bit to indicate that a byte has been received
and should be picked up before it is lost. RI is raised halfway
through the stop bit.
SBUF register

• SBUF is an 8-bit register used solely for serial communication in the 8051.
For a byte of data to be transferred via the TxD line, it must be placed in the
SBUF register. The moment a byte is written into SBUF, it is framed with the start
and stop bits and transferred serially via the TxD pin.

• Similarly, when the bits are received serially via RxD, the 8051 deframes it by
eliminating the stop and start bits, making a byte out of the data received, and then
placing it in the SBUF.

• SBUF can be accessed like any other register in the 8051.


Mode 0
• In mode 0, serial data are transmitted and received through the RXD
pin, while the TXD pin output clocks. The baud rate is fixed at 1/12
the oscillator frequency. On transmit, the least significant bit (LSB
bit) is sent/received first.

• TRANSMIT - Data transmit is initiated by writing data to the SBUF


register. In fact, this process starts after any instruction being
performed upon this register. When all 8 bits have been sent, the TI
bit of the SCON register is automatically set.

• RECEIVE - Data receive through the RXD pin starts upon the two
following conditions are met: bit REN=1 and RI=0 (both of them are
stored in the SCON register). When all 8 bits have been received,
the RI bit of the SCON register is automatically set indicating that
one byte receive is complete.
Mode 1
• In mode 1, 10 bits are transmitted through the TXD pin or received
through the RXD pin in the following manner: a START bit (always 0),
8 data bits (LSB first) and a STOP bit (always 1). The START bit is
only used to initiate data receive, while the STOP bit is automatically
written to the RB8 bit of the SCON register.

• TRANSMIT - Data transmit is initiated by writing data to the SBUF


register. End of data transmission is indicated by setting the TI bit of
the SCON register.

• RECEIVE - The START bit (logic zero (0)) on the RXD pin initiates
data receive. The following two conditions must be met: bit REN=1 and
bit RI=0. Both of them are stored in the SCON register. The RI bit is
automatically set upon data reception is complete.
Mode 2
• In mode 2, 11 bits are transmitted through the TXD pin or received
through the RXD pin: a START bit (always 0), 8 data bits (LSB first), a
programmable 9th data bit and a STOP bit (always 1). On transmit, the
9th data bit is actually the TB8 bit of the SCON register. This bit usually
has a function of parity bit. On receive, the 9th data bit goes into the
RB8 bit of the same register (SCON).The baud rate is either 1/32 or
1/64 the oscillator frequency.

• TRANSMIT - Data transmit is initiated by writing data to the SBUF


register. End of data transmission is indicated by setting the TI bit of the
SCON register.
• RECEIVE - The START bit (logic zero (0)) on the RXD pin initiates data
receive. The following two conditions must be met: bit REN=1 and bit
RI=0. Both of them are stored in the SCON register. The RI bit is
automatically set upon data reception is complete.
• Mode 3 : Mode 3 is the same as Mode 2 in all respects except the baud
rate. The baud rate in Mode 3 is variable.
Doubling the baud rate in the 8051
• The 8051 transfers and receives data serially at many different baud
rates. The baud rate in the 8051 is programmable. This is done with the
help of Timer 1.
• There are two ways to increase the baud rate of data transfer in the
8051.
i) Use a higher-frequency crystal.
Ii) Change a bit in the PCON register, shown below.

• When the 8051 is powered up, D7 (SMOD bit) of the PCON register is
zero. We can set it to high by software and thereby double the baud
rate.

Power control register (PCON)
power-saving modes of operation: Idle and Power Down.
• In some cases CPU has to wait for external signal for its operation. Even CPU is
inactive it consumes power from battery. To reduce power consumption of CPU
it should be operated in sleep mode.

• Idle mode
• Upon the IDL bit of the PCON register is set, the microcontroller turns off the
greatest power consumer- CPU unit while peripheral units such as serial port,
timers and interrupt system continue operating normally consuming 6.5mA. In
Idle mode, the state of all registers and I/O ports remains unchanged.

• Power Down mode


• By setting the PD bit of the PCON register from within the program, the
microcontroller is set to Power down mode, thus turning off its internal oscillator
and reduces power consumption enormously. The only way to get the
microcontroller back to normal mode is by reset.

• While the microcontroller is in Power Down mode, the state of all SFR registers
and I/O ports remains unchanged. By setting it back into the normal mode, the
contents of the SFR register is lost, but the content of internal RAM is saved.
Power Consumption Control

You might also like