8051 SERIAL PORTS.pptx

530 views 31 slides Apr 23, 2022
Slide 1
Slide 1 of 31
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31

About This Presentation

serial ports of 8051


Slide Content

8051 SERIAL PORTS

8051 Serial Ports One of the 8051’s many powerful features is its integrated Universal Asynchronous Receiver Transmitter (UART ), otherwise known as a serial port. With integrated serial port of 8051, data can be transmitted and received easily by reading and writing the data to the serial port registers. The features of the 8051 serial ports are Full duplex operation. Receive Buffered. Access using single double buffered Register SBUF. Four different modes of operation. Option to use fixed baud rate or programmable baud rate.

8051 Serial Ports Full Duplex serial port means that it can transmit and receive data simultaneously. It is also receive-buffered, meaning it can commence reception of a second byte before a previously received byte has been read from the SBUF register. However, if the first byte still hasn’t been read by the time reception of the second byte is complete, one of the bytes will be lost. The serial port receive and transmit registers are both accessed at Special Function Register SBUF.

8051 Serial Ports Data to be transmitted are written to the register SBUF and the data received by the serial port are read from the register SBUF. Physically reading and writing SBUF actually accesses two separate registers. This technique of having same address for two different registers is called double buffering.

SERIAL PORT CONTROL SFRS The serial port of 8051 is controlled by two registers in SFR area of 8051 as shown in Table The two registers are Serial Port control registers; SCON and serial port buffer register SBUF. SFR Name Description SFR Address SCON Serial port control register 98h SBUF Serial port buffer register 99h

SERIAL PORT CONTROL SFRS In addition to the above two registers, the MSB of PCON register named as SMOD bit is used to double the baud rate of serial transmission and reception. If SMOD bit is set to 1, then the baud rate is doubled. The individual bits of SCON have the functions as shown in Table . As the SCON register has many individual status bits, the individual bits of this register are bit addressable. The bit address is also given in Table . The programmer can use these bit addresses to check the status of the serial port and set the mode individually.

Bit patterns for SCON (98h) SFR Bit Name Bit Address Explanation of Function D7 SM0 9Fh Serial port mode select bits D6 SM1 9Eh D5 SM2 9Dh Multiprocessor Communications Enable bit D4 REN 9Ch Receiver Enable. This bit must be set in order to receive characters. D3 TB8 9Bh Transmit bit 8. The 9th bit to transmit in mode 2 and 3. D2 RB8 9Ah Receive bit 8. The 9th bit received in mode 2 and 3. D1 TI 99h Transmit Interrupt Flag. Set when a byte has been completely transmitted. D0 RI 98h Receive Interrupt Flag. Set when a byte has been completely received.

Bit Pattern - Description D7 and D6 bits of the SCON register define the operation modes of the serial port and the basic operating modes are given in Table. The SM0 and SM1 bits can select any one of the four operating modes described in the next section.

Definition of Bits SM0 and SM1 in SCON SFR

Bit Pattern - Description The next bit, SM2, is a flag used for enabling "Multiprocessor communication" in modes 2 and 3. If SM2 is set to 1 in modes 2 and 3, the “Receive Interrupt” RI flag will not be activated if the received 9th data bit is 0. If SM2 is set in Mode1, then “Receive Interrupt” RI flag will not be activated if valid stop bit is not received. This can be useful in certain advanced serial applications. It can be now assumed that SM2 bit has to be cleared so that RI flag will be set when any character is received.

Bit Pattern - Description The next bit, REN, is "Receiver Enable ." This bit is set in order to receive the characters from the receive data line of the serial port. The TB8 bit is used in modes 2 and 3. In modes 2 and 3, a total of nine data bits are transmitted. The first 8 bits are the 8 bits of the data to be transmitted, and the ninth bit is taken from TB8. The RB8 also operates in modes 2 and 3 on the reception side.

Bit Pattern - Description When a byte is received in modes 2 or 3, a total of nine bits are received. In this case, the first eight bits received are the data of the serial byte received and the value of the ninth bit received will be placed in RB8. TI means "Transmit Interrupt." When a program writes a data to the serial port buffer SBUF, then the serial port will start shifting this data in the serial transmit line bit by bit at the predefined clock speed or baud rate. 8051 will give TI signal to the programmer after sending the data completely.

Bit Pattern - Description Upon sensing the TI bit set to 1, the programmer can then write the next data for transmission. When the TI bit is set, the programmer may assume that the serial port is "free" and ready to send the next byte. Finally, the RI bit means "Receive Interrupt." Whenever a data is received on the receive data line of the serial port, this serial data will be shifted in to a buffer and then stored in the SBUF register. Setting of RI bit indicates that a byte has been received. Upon sensing the RI bit set to 1, the programmer may read the data from the SBUF.

OPERATING MODES Bits SM0 and SM1 are used to set the serial mode to a value between 0 and 3. Selecting the Serial Mode selects the mode of operation (8-bit/9-bit, UART or Shift Register) and also determines how the baud rate will be calculated. In modes 0 and 2 the baud rate is fixed based on the oscillator’s frequency. In modes 1 and 3 the baud rate is variable based on Timer 1 overflows. © Oxford University Press 2013

Block Diagram of the serial port of 8051 © Oxford University Press 2013

SBUF SBUF is physically two registers with the same address. When data to be transmitted is written to the SBUF register, then it will be shifted bit by bit into the TXD line of 8051. The port 3 pin 3.1 acts as the TXD line . The shifting is done by the transmit clock which determines the baud rate. Similarly, when the data bits are received on the RXD line (Pin 3.0 of port 3), the bits are shifted serially into the shift register according to the Receive clock. After the reception is complete, the data received will be placed on SBUF from where it can be read by the programmer through the internal bus.

Reception / Transmission Mode 0: In this mode serial data is entering and exiting through RxD pin. So, in mode 0, Full duplex is not possible – meaning that both transmission and reception cannot take place simultaneously. TxD pin outputs the shift clock. 8 bits are transmitted/received (LSB first). The baud rate is fixed at 1/12 the oscillator frequency. Transmission is started by writing a data byte to the SBUF register and once the transmission is complete, TI flag is set The reception is started by enabling REN in SCON register. Once the data reception is complete, the RI flag is set The baud rate in Mode 0 is fixed at one twelfth of the clock frequency. Baud rate= (Clock frequency/12)

Signal Transmission / Reception pattern © Oxford University Press 2013

Reception / Transmission Mode 1: In this mode, 10 bits are transmitted through TxD and simultaneously 10 bits can be received through RxD . The 10 bits are made up of a start bit (0), 8 data bits (LSB first), and a stop bit (1). On completion of reception, the stop bit goes into RB8 in Special Function Register SCON. The baud rate is variable and is set by the Timer 1 overflow rate. The baud rate for mode 1 is fixed at the following rate.

Reception / Transmission Baud rate= (Timer 1 overflow rate /16) if SMOD bit in PCON SFR is set to 1. Baud rate= (Timer 1 overflow rate /32) if SMOD bit in PCON SFR is set to 0. The MSB of PCON register can be set or reset by the programmer. The baud rate can be doubled by setting the MSB of PCON.

Reception / Transmission In order to generate the baud rate clock from timer 1, Timer 1 can be configured to act as timer in auto reload mode with the timer 1 interrupt disabled. As in mode 0, the transmission is initiated by writing a data to SBUF register. Reception is initiated by a 1 to 0 transition that is the start bit received and also when REN of SCON SFR is 1.

Reception / Transmission Mode 2: In this mode, 11 bits are transmitted through TxD or received through RxD . The 11 bits are made up of one start bit (always 0), 8 data bits (LSB first), a programmable 9th data bit, and a stop bit (always 1). The 9th data bit transmitted is same as TB8 bit in SCON special function register. It can be assigned the value of 0 or 1 by the programmer. Or, for example, the parity bit (P, in the PSW) could be moved into TB8. On reception, the 9th data bit goes into RB8 in Special Function Register SCON, while the stop bit is ignored. The baud rate is programmable to either 1/32 or 1/64 of the oscillator clock frequency. Baud rate= (Clock frequency /32) if SMOD bit in PCON SFR is set to 1. Baud rate= (Clock frequency /64) if SMOD bit in PCON SFR is set to 0.

Reception / Transmission Mode 3: In this mode, 11 bits are transmitted through TxD and simultaneously 11 bits are received through RxD . The 11bits are made up of a start bit (0), 8 data bits (LSB first), a programmable 9th data bit, and a stop bit (1). In fact, Mode 3 is the same as Mode 2 in all respects except baud rate. The baud rate in Mode 3 is variable. The baud rate for mode 3 is fixed at the following rate similar to mode1. Baud rate= (Timer 1 overflow rate /16) if SMOD bit in PCON SFR is set to 1. Baud rate= (Timer 1 overflow rate /32) if SMOD bit in PCON SFR is set to 0.

Reception / Transmission In all four modes, transmission is initiated by any instruction that uses SBUF as a destination register. Reception is initiated in Mode 0 by the condition RI = 0 and REN = 1. Reception is initiated in the other modes by the incoming start bit when REN = 1. The flow chart 11.20 shows the steps in programming the serial port of 8051. These steps are detailed in the following sections and also in the example programs.

Programming the Serial Port

Initializing Serial Port Once the Serial Port Mode has been selected, then the programmer must configure the serial port’s baud rate. Only serial port modes 1 and 3, the baud rates are programmable. The Baud Rate is determined based on the oscillator’s frequency when in mode 0 and 2. In mode 0, the baud rate is always the oscillator frequency divided by 12. This means if the crystal is 11.059 MHz, mode 0 baud rate will always be 921,583 baud. In mode 2 the baud rate is always the oscillator frequency divided by 64, so an 11.059 MHz crystal speed will yield a baud rate of 172,797. © Oxford University Press 2013

Initializing Serial Port In modes 1 and 3, the baud rate is determined by how frequently timer 1 overflows. The most common method is to put timer 1 in 8-bit auto-reload mode (timer mode 2) and set a reload value (TH1) that causes Timer 1 to overflow at a frequency appropriate to generate a baud rate. To determine the value that must be placed in TH1 to generate a given baud rate, the following equation is used. TH1 = 256 - ((Clock frequency / 384) / Baud) if SMOD in PCON SFR is 0. TH1 = 256 - ((Clock frequency / 192) / Baud) if SMOD in PCON SFR is 1. The following table gives the commonly used baud rates and the corresponding reload for the timer in mode 2 assuming the clock frequency of 11.059 MHz and SMOD is reset. © Oxford University Press 2013

Commonly used baud rates Baud Rate Timer value TH1 300 A0h 1200 D0h 2400 FAh 9600 FDh © Oxford University Press 2013

Baud Rate Following set of instructions will set the timer for the baud rate of 9600. MOV TMOD, #00100000B ;timer/counter 1 set for mode 2, 8-bit TIMER ;operation MOV TH1, #0FDh ; timer/counter 1 is timed for 9600 baud SETB TR1 ; timer/counter 1 is enabled for free run For initializing the serial port for mode 3 operation, the following instruction can be used. MOV SCON, #11010000B © Oxford University Press 2013

Transmitting and Receiving Data Using Serial Port Once the Serial Port has been properly configured as explained above, the serial port is ready to be used to send data and receive data. To write a byte to the serial port one must simply write the value to be transmitted to the SBUF (99h) SFR. For example, to send the letter "A" to the serial port, the following instruction can be written. MOV SBUF, #’A’ © Oxford University Press 2013

Upon execution of the above instruction the 8051 will begin transmitting the character via the serial port. Once the transmission is complete, the serial port transmit interrupt flag TI is set. Since the 8051 does not have a serial output buffer, a character can not be written to SBUF before the previous written character is completely transmitted. This can be accomplished by checking the TI flag. Reading data received by the serial port is equally easy. To read a byte from the serial port one just needs to read the value stored in the SBUF (99h) SFR after the 8051 has automatically set the RI flag in SCON. © Oxford University Press 2013 Transmitting and Receiving Data Using Serial Port
Tags