Max 232
Max 232
1. Microcontroller
2. PC or any external device which can be interfaced with the microcontroller through
serial communication.
3. RS 232 cable
4. MAX 232 IC
5. HyperTerminal
When the pins P3.0 and P3.1 of microcontroller are set, UART which is inbuilt in the
microcontroller will be enabled to start the serial communication.
SBUF (Serial Buffer) is an 8-bit register used while transmitting or receiving the data.
Whenever the microcontroller tries to transmit any data, the data should be first placed
into SBUF register. Thus, it is the duty of SBUF to transmit the data to PC.
Similarly, while the microcontroller received the data from PC, the data coming from the
PC first falls into SBUF register and from there, the microcontroller reads the received
data from PC.
TIMERS:
The 8051 has two timers: Timer 0 and Timer 1. They can be used either as timers to
generate a time delay or as counters to count events happening outside the
microcontroller.
Both Timer 0 and Timer 1 are 16-bit wide. Since the 8051 has an 8-bit architecture, each
16-bit timer is accessed as two separate registers of low byte and high byte.
Lower byte register of Timer 0 is TL0 and higher byte is TH0. Similarly lower byte
register of Timer1 is TL1 and higher byte register is TH1.
Both timers 0 and 1 use the same register TMOD to set the various operation modes.
TMOD is an 8-bit register in which the lower 4 bits are set aside for Timer 0 and
the upper 4 bits for Timer 1. In each case, the lower 2 bits are used to set the timer mode
and the upper 2 bits to specify the operation.
(MSB) (LSB)
TIMER 1 TIMER 0
GATE
Every timer has a means of starting and stopping. Some timers do this by software, some
by hardware and some have both software and hardware controls. The timers in the 8051
have both. The start and stop of the timer are controlled by the way of software by the TR
(timer start) bits TR0 and TR1. These instructions start and stop the timers as long as
GATE=0 in the TMOD register. The hardware way of starting and stopping the timer by
an external source is achieved by making GATE=1 in the TMOD register.
C/T
Timer or counter selected. Cleared for timer operation and set for counter operation.
M1
Mode bit 1
M0
Mode bit 0
it overflows
1 1 3 Split timer mode
This mode 2 is an 8-bit timer and therefore it allows only values of 00H to FFH to be
loaded into the timer’s register TH. After TH is loaded with the 8-bit value, the 8051 give
a copy of it to TL. When the timer starts, it starts to count up by incrementing the TL
register. It counts up until it reaches its limit of FFH. When it rolls over from FFH to
00H, it sets high the TF (timer flag). If Timer 0 is used, TF0 goes high and if Timer 1 is
used, TF1 goes high. When the TL register rolls from FFH to 0 and TF is set to 1, TL is
reloaded automatically with the original value kept by the TH register.
Computers transfer data in two ways: parallel and serial. In parallel data transfers, often 8
or more lines are used to transfer data to a device that is only a few feet away. Although a
lot of data can be transferred in a short amount of time by using many wires in parallel,
the distance cannot be great. To transfer to a device located many meters away, the serial
method is best suitable.
In serial communication, the data is sent one bit at a time. The 8051 has serial
communication capability built into it, thereby making possible fast data transfer using
only a few wires.
The fact that serial communication uses a single data line instead of the 8-bit data line
instead of the 8-bit data line of parallel communication not only makes it cheaper but also
enables two computers located in two different cities to communicate over the telephone.
Serial data communication uses two methods, asynchronous and synchronous. The
synchronous method transfers a block of data at a time, while the asynchronous method
transfers a single byte at a time. With synchronous communications, the two devices
initially synchronize themselves to each other, and then continually send characters to
stay in sync. Even when data is not really being sent, a constant flow of bits allows each
device to know where the other is at any given time. That is, each character that is sent is
either actual data or an idle character. Synchronous communications allows faster data
transfer rates than asynchronous methods, because additional bits to mark the beginning
and end of each data byte are not required. The serial ports on IBM-style PCs are
asynchronous devices and therefore only support asynchronous serial communications.
Asynchronous means "no synchronization", and thus does not require sending and
receiving idle characters. However, the beginning and end of each byte of data must be
identified by start and stop bits. The start bit indicates when the data byte is about to
begin and the stop bit signals when it ends. The requirement to send these additional two
bits causes asynchronous communication to be slightly slower than synchronous however
it has the advantage that the processor does not have to deal with the additional idle
characters.
There are special IC chips made by many manufacturers for serial data communications.
These chips are commonly referred to as UART (universal asynchronous receiver-
transmitter) and USART (universal synchronous-asynchronous receiver-transmitter). The
8051 has a built-in UART.
In the asynchronous method, the data such as ASCII characters are packed between a
start and a stop bit. The start bit is always one bit, but the stop bit can be one or two bits.
The start bit is always a 0 (low) and stop bit (s) is 1 (high). This is called framing.
The rate of data transfer in serial data communication is stated as bps (bits per second).
Another widely used terminology for bps is baud rate. The data transfer rate of a given
computer system depends on communication ports incorporated into that system. And in
asynchronous serial data communication, this baud rate is generally limited to
100,000bps. The baud rate is fixed to 9600bps in order to interface with the
microcontroller using a crystal of 11.0592 MHz.
RS232 CABLE:
MAX 232:
To ensure data transfer between PC and microcontroller, the baud rate and voltage levels
of Microcontroller and PC should be the same. The voltage levels of microcontroller are
logic1 and logic 0 i.e., logic 1 is +5V and logic 0 is 0V. But for PC, RS232 voltage levels
are considered and they are: logic 1 is taken as -3V to -25V and logic 0 as +3V to +25V.
So, in order to equal these voltage levels, MAX232 IC is used. Thus this IC converts
RS232 voltage levels to microcontroller voltage levels and vice versa.
Fig: Pin diagram of MAX 232 IC
The SCON register is an 8-bit register used to program the start bit, stop bit and data bits
of data framing.
cleared by software.
cleared by software.
SM0 SM1
0 0 Serial Mode 0
1 0 Serial Mode 2
1 1 Serial Mode 3
Of the four serial modes, only mode 1 is widely used. In the SCON register, when serial
mode 1 is chosen, the data framing is 8 bits, 1 stop bit and 1 start bit, which makes it
compatible with the COM port of IBM/ compatible PC’s. And the most important is
serial mode 1 allows the baud rate to be variable and is set by Timer 1 of the 8051. In
serial mode 1, for each character a total of 10 bits are transferred, where the first bit is the
start bit, followed by 8 bits of data and finally 1 stop bit.
MICROCONTROLLER INTERFACE WITH ANY EXTERNAL DEVICES
USING SERIAL COMMUNICATION: