You are on page 1of 4

UART Programming and MAX232 Interfacing for 8051

Introduction to RS-232 and MAX232


Serial Programming of AVR and 8051
Implementing software UART for 8051

Introduction

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


RS-232 Basics

RS-232 (Recommended Standard 232) is a standard for serial binary data signals connecting between a DTE
(Data terminal equipment) and a DCE (Data Circuit-terminating Equipment).
Voltage Levels:
The RS-232 standard defines the voltage levels that correspond to logical one and logical zero levels. Valid
signals are plus or minus 3 to 25 volts. The range near zero volts is not a valid RS-232 level; logic one is
defined as a negative voltage, the signal condition is called marking, and has the functional significance of OFF.
Logic zero is positive, the signal condition is spacing, and has the function ON.
So a Logic Zero represented as +3V to +25V and Logic One represented as -3V to -25V.

RS-232 Level Converters
Usually all the digial ICs works on TTL or CMOS voltage levels which cannot be used to communicate over
RS-232 protocol. So a voltage or level converter is needed which can convert TTL to RS232 and RS232 to TTL
voltage levels.
The most commonly used RS-232 level converter is MAX232. This IC includes charge pump which can
generate RS232 voltage levels (-10V and +10V) from 5V power supply. It also includes two receiver and two
transmitters and is capable of full-duplex UART/USART communication.

Fig A. - MAX232 Pin Description

Fig B. - MAX232 Typical Connection Circuit


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


In the next part of this tutorial we will discuss programming microcontroller to communicate over UART and
software implementation of half duples UART.
AVR Programming for UART

In AVR, following set of registers are used to communicate over USART

UCSRA

UCSRB

UCSRC

UBRRH

UBRRL

UCSRA: in this register there are flags for various errors that might occure during data transmission, e.g. parity
error, frame error etc.
UCSRB: in this register we have a lot of enable bits. For example different interrupt enable bits but also the
reciving and transmitting enable bits.
UCSRC: in this register we set the parity mode, stop bits and so on.
UBRRH & UBRRL: in UBRRH register, the higher byte and in UBRRL, lower byte is stored for generating a
required Baud rate.

More information on the above registers can be found in the datasheet of the AVR you are using.
Initialising USART in AVR


8051 Programming for UART

In 8051, we make use of Timer 1 to generate the required baud rate. Following are the registers that are need to
be configured to commnunicate over UART.

TMOD, SCON, TH1, TL1, TCON

TMOD: This register is used to set the mode of Timer0 and Timer1. It is also used to select whether the timers
are used as Timer or Counter.
SCON: Serial Control register has various functions like.. it has flags for Framing error, Transmit interrup and
receive interrupt. Its used to select the serial port mode, to enable or disable the reception etc.
TCON: This register has varios flag and control bits e.g. Timer overflow flags, interrupt edge flags, timer
control bits to start/stop the timer.
TH1 & TL1: Timer registers for Timer 1 determines the baudrate of UART.

More information on the above registers can be found in the 8051 Hardware manual.
Initializing USART in 8051
CODE:
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



in C we can do this as..
CODE:
#include <reg51.h>.
void serial_init(){
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFD;
TL1 = 0xFD;
TR1 = 1;
}

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...
CODE:
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



in C we can do this as..
CODE:
void serial_send(unsigned char dat){
while(!TI);
TI = 0;
SBUF = dat;
}
unsigned char serial_read(){
while(!RI);
RI = 0;
return SBUF;
}

You might also like