Anda di halaman 1dari 22

Introduction

UART (Universal Asynchronous Receiver Transmitter) or USART (Universal Synchronous Asynchronous Receiver Transmitter) are one of the basic interfaces which you will find in almost all the controllers. This interface provides a cost effective simple and reliable communication between one controller to another controller or between a controller and PC.

MAX232 Interfacing with Microcontrollers


To communicate over UART or USART, we just need three basic signals which are namely, RXD (receive), TXD (transmit), GND (common ground). So to interface MAX232 with any microcontroller (AVR, ARM, 8051, PIC etc..) we just need the basic signals. A simple schematic diagram of connections between a microcontroller and MAX232 is shown below.

Serial Communication
Since IBM PC are widely used to communicate with the 8051-based systems, We will emphasize serial communications of 8051 with the COM port of the PC. To allow the data transfer between the PC and an 8051 systems without any error, the baud rate of the TRX and the RCX should match.

Baud Rate in 8051 system


The 8051 transfers and receives the data serially at different Baud rates. The Baud rate of 8051 is programmable. This is done with the help of timer 1 mode 2. The 8051 divides the crystal frequency by 12 to get the machine cycle frequency. The UART divides this machine cycle frequency by 32 once more before it is used by the timer 1 to set the Baud rate.

Baud Rate in 8051 system


Assume the XTAL frequency to be equal to 11.0592 MHz. The Machine cycle frequency =11.0592/12 = 921.6 KHz The 8051s serial communication UART circuitry divides by 32 gives 28,800 Hz. This frequency value is used to set the baud rate by loading the value in the register. When Timer 1 is used to set the baud rate it must be programmed in MODE 2,that is 8bit, Auto-reload.

Calculation of Baud Rate


The Machine cycle frequency =11.0592/12 = 921.6 KHz The 8051s serial communication UART circuitry divides by 32 gives 28,800 Hz. 28800/9600 Baud = -3 = FD H 28800/2400 Baud = -12 = F4 H 28800/1200 baud = -24 =E8 H

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 TxD line, it must be placed in the SBUF register. Similarly, SBUF can holds the byte of data when it is received from the 8051 RxD line. SBUF can be accessed like any other register of 8051.
MOV SBUF,#D MOV SBUF,A MOV A,SBUF

SBUF
The moment the data is written into SBUF, it is framed with the start and stop bits and transferred over the TxD line.

SCON-Serial Control Register


The SCON register is a 8-Bit register used to program the start bit , the stop bit and data bits of data framing etc SCON is bit addressable

SM0

SM1 SM2 REN TB8 RB8

TI

RI

SCON-Serial Control Register


SM0 SM1 0 0 Mode Mode 0, 8-bit shift register. Baud = f/12 Mode 1 ,8-bit data,1 start bit, 1 stop bit, Baud variable Mode 2, 9-bit UART; baud=f/32 or f/64 Mode 3, 9-bit UART; baud= variable

0
1 1

1
0 1

SCON
SM2: This bit (D5) enables the Multiprocessing capability - Not using 8051 in multiprocessor environment, so make SM2=0 REN: Receive enable bit. Set to one enable reception.
REN =1 Enable 8051 to transfer and receive the data The instruction is SETB SCON.4 REN=0 The receiver is disabled The instruction is CLR SCON.4

SCON-Serial Control Register


TB8( Transfer bit 8): this is used in mode 2 and 3. - make TB8 = 0 since it is not used in our application. RB8 ( Receive bit 8): In serial mode 1 it gets the copy of the stop bit when 8bit is received. this is used in mode 2 and 3. - Like TB8 make RB8 = 0 since it is not used in our application. TI (Transmit Interrupt): When 8051 finishes the transfer of the 8-bit character, it raises the TI flag to indicate that it is ready to transfer another byte of data. RI (Receive Interrupt): When 8051 receives the data serially via RxD, after the deframing of the data, the data will be placed in the SBUF register. Then it raises the RI flag to indicate that the data must be stored before being overwritten by another byte.

Program the 8051 to transfer the data serially


1. The TMOD register is loaded with the value to set the Timer 1 in mode2 to set the Baud rate. 2. The TH1 is loaded with one of the values for the required baud rate serial data transfer. 3. The SCON register is loaded with the serial control word 4. TR1 is set to 1 to start the Timer 1. 5. TI is cleared by the CLR TI instruction 6. The character byte to be transferred serially is written into the SBUF register.

Program the 8051 serially to transfer the data


7. The TI flag bit is monitored with the use of the instruction JNB TI,XX to see if the character has been completely transferred. 8. To transfer the next character, go to step 5.

Importance of TI Flag
The Byte character to be transmitted is written in the SBUF register. It transfers the start bit. The 8-bit character is transferred one bit at a time. The stop bit is transferred. It is during the transfer of the stop bit that the 8051 raises the TI flag (T1=1), indicating that the last character was transmitted and it is ready to transfer the next character. By monitoring the TI flag ,We make sure that we are not overloading the SBUF register.

Importance of TI Flag
If we write another byte into SBUF register before T1 is raised, the un-transmitted portion of the previous byte will be lost. After SBUF is loaded with a new byte, the T1 flag bit must be forced to 0 by the CLR T1 instruction in order for this new byte to be transferred.

Program the 8051 to receive data serially


1. The TMOD register is loaded with the value, indicating the use of timer 1 in mode2 to set the Baud rate. 2. TH1 is loaded with one of the values to set the Baud rate. 3. The SCON register is loaded with the value, Indicating the serial mode 1, where 8 bit data is framed with start and stop bits. 4. TR1 is set to 1 to start the timer. 5. RI is cleared with the instruction CLR RI instruction.

Program the 8051 serially to receive the data


6. The RI flag bit is monitored with the use of the instruction JNB RI,XX to see if an entire character has been received yet. 7. When RI is raised, SBUF has the byte. it has to be moved into the safe place before being overwritten. 8. To receive the next character, go to step 5.

Importance of RI flag bit


RxD pin receives the start bit indicating that the next bit is the first bit of the character byte it is about to receive. The 8-bit character is received one bit at a time. When last bit is received, a byte is formed and placed in the SBUF. The stop bit is received. It is during receiving the stop bit that the 8051 makes RI = 1, indicating that an entire character byte has been received and the data should be moved to safer destination before being overwritten.

Importance of RI flag bit


By checking the RI flag bit when it is raised, we know that a character has been received and available in SBUF. After the SBUF contents are copied into a safe place, the RI flag bit must be forced to obey the CLR RI instruction to allow the next received character byte to be placed in SBUF.

Anda mungkin juga menyukai