Rs232 Serial Communication Projects:Output a Simple Text Message from the RS232 Port

PROJECT 22 - Output a Simple Text Message from the RS232 Port

Function

This project shows how we can interface our microcontroller to an external RS232 compatible device (e.g. an RS232 visual display unit, or COM1 or COM2 port of a PC) and send a text message to this device. The text message 'THIS IS AN RS232 TEST MESSAGE' is sent out continuously from the microcontroller. The frame format used in this project is 2400 baud, 8 data bits, noparity, and 1 stop bit.

Rs232-Serial-Communication-Projects-

Circuit Diagram

The block diagram of Project 22 is shown in Fig. 6.4. The TXD pin of the microcontroller is connected to the MAX232 Maxim voltage converter IC and the output of this IC can be connected to the input of a COM1 (or COM2) port of a PC, or to the input of an RS232 visual display unit. Similarly, the output of the external RS232 device is connected to the RXD input of the micro- controller via the MAX232 IC. A terminal emulation software can be activated on the PC to receive and display any data arriving at its serial port.

The complete circuit diagram of this project is shown in Fig. 6.5. Pin P3.1 of the microcontroller (TXD) is connected to pin 10 of the MAX232 converter IC. Pin 7 of this IC is connected to the external RS232 compatible serial device which is to receive and display our text message. Similarly, the output of the RS232 device is connected to pin 8 input of the MAX232 IC and pin 9 output of this IC is connected to pin 2 (RXD) serial input of the microcontroller. Correct operation of MAX232 requires four external capacitors to be connected as shown in the figure.

Program Description

The program initializes the RS232 port of the microcontroller and then sends a test message tothe port.

The following PDL describes the functions of the program:

Rs232-Serial-Communication-Projects-[2]

Rs232-Serial-Communication-Projects-[10]

Program Listing

The program listing is given in Fig. 6.6. Notice that the standard input/output library 'stdio.h' is included at the top of the program. The main program calls function serial_init which initializes the RS232 port to 2400 baud and enables transmissions. Standard C function printf is used tosend the serial data tothe RS232 port. A carriage return and line feed pair ('\n') are sent after each output.

It is important to notice that this simple program occupies about 1094 bytes in the memory of the microcontroller. This is because the printf function is a complex library function and is implemented in a large number of instructions. A simple function can be developed to emulate some of the functionalities of printf so that the output operations can be performed with less memory as described below.

Rs232-Serial-Communication-Projects-[6]

A Simple Serial Output Function

The 89C2051 microcontroller is equipped with 2 Kbytes of memory. It was shown in the previous example that using the built-in printf function causes a large part of this memory to be used, leaving little space for other operations. Figure 6.7 shows a program listing that performs serial output functions without using the printf function and the complete program occupies about 400 bytes of memory. In this program, the serial transmit register of the microcontroller (SBUF) is used to send out data directly. Function send_serial transmits a null-terminated string to the RS232 port of the microcontroller. The program waits until the transmit register is empty (TI = 1) before sending out the next character. In this example, the string 'ANOTHER TEST' is output continuously. Notice that calling this function with variable crlf causes a carriage return and line feed to be output at the end of the test message.

Components Required

The following components will be required in addition to the standard microcontroller components:

Rs232-Serial-Communication-Projects-[11]