Serial Communication (UART)
In order to enable successful UART serial communication, it is necessary to meet specific rules of the RS232 standard. It primarily refers to voltage levels required by this standard. Accordingly, -10V stands for logic one (1) in the message, while +10V stands for logic zero (0). The microcontroller converts accurately data into serial format, but its power supply voltage is only 5V. Since it is not easy to convert 0V into 10V and 5V into -10V, this operation is on both transmit and receive side left to a specialized IC circuit. Here, the MAX232 by MAXIM is used because it is widespread, cheap and reliable.
This example shows how to receive message sent by a PC. Timer T1 generates boud rate. Since the 11.0592 MHz quartz crystal is used here, it is easy to obtain standard baud rate which amouts to 9600 bauds. Each received data is immediately transferred to port P1 pins.
Almost all digital devices which we use require either TTL or CMOS logic levels. Therefore the first step to connecting a device to the RS-232 port is to transform the RS-232 levels back into 0 and 5 Volts. As we have already covered, this is done by RS-232 Level Converters.
Two common RS-232 Level Converters are the 1488 RS-232 Driver and the 1489 RS-232 Receiver. Each package contains 4 inverters of the one type, either Drivers or Receivers. The driver requires two supply rails, +7.5 to +15v and -7.5 to -15v. As you could imagine this may pose a problem in many instances where only a single supply of +5V is present. However the advantages of these I.C's are they are cheap.
RS-232 Driver/Receiver. Right: (Figure 7) Typical MAX-232 Circuit. |
There are also many variations of these devices. The large value of capacitors are not only bulky, but also expensive. Therefore other devices are available which use smaller capacitors and even some with inbuilt capacitors. (Note : Some MAX-232's can use 1 micro farad Capacitors). However the MAX-232 is the most common, and thus we will use this RS-232 Level Converter in our examples.
SETTING SERIAL PORT.
SCON
8 bit UART ,RN enabled , TI & RI operated by program. - 50hex
Timer 1 Count
TH1 = 256 - ((Crystal / 384) / Baud) -PCON.7 is clear.
TH1 = 256 - ((Crystal / 192) / Baud)-PCON.7 is set.
so with PCON.7 is clear we get timer value = FDhex
Example Code:
#include "reg51.h" #include "stdio.h" void main (void) { SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */ TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */ TH1 = 0xf3; /* TH1: reload value for 2400 baud */ TR1 = 1; /* TR1: timer 1 run */ TI = 1; /* TI: set TI to send first char of UART */ while(1) { unsigned char aaa; aaa = _getkey(); putchar(aaa); } }