Many stand-alone devices still use a DB9 or DB25 connector to communicate with a desktop computer over what is commonly called a serial port. This project will help you build an autonomous device with an RS-232 output that can talk to a computer. It also lays the groundwork for trying Microchip’s MCP2200, an interface chip that adapts a serial connection to USB—in effect, replacing those RS-232-to-USB cables that may not have worked for you. As always, it helps to learn to walk before trying to run.

Of course, we know the serial port is considered obsolete and is no longer easy to find on the back of a computer. After so many NeoTeo projects using USB, bringing a serial connection to the workbench may feel like a trip into the past, or a “retro” build. Yet serial ports will remain relevant for years in applications that need to communicate with computers. The classic COM port once hosted a mouse or an infrared receiver that let you control Winamp from bed. That male DB9 connector could even damage a motherboard during early experiments; when the port failed, a PCI card was sometimes the only way to get the mouse working again.

Why use serial communication?

Microcontroller projects that go beyond simple gadgets often need useful data handling: sooner or later, their data must reach a computer for processing. Most microcontrollers available today include a serial communications unit. It is an efficient way for two microcontrollers to exchange information, but the steady disappearance of DB9 connectors from computers—and especially laptops—has created a hardware and connectivity problem. Everything is USB now.

The market, especially the Chinese market, responded with inexpensive male-DB9-to-USB adapters. They looked like a universal fix, but many disappointed buyers found that their cables did not work at all; the ones that did often had limited or erratic functionality. The FT232 from FTDI seemed to offer a lasting answer. It appears in countless designs that connect a microcontroller’s USART (Universal Synchronous/Asynchronous Receiver/Transmitter) to the USB bus; Arduino, for example, uses this approach. But its small package and setup complexity can put off beginners. Not everyone has the hands-on skill needed to build around such a tiny component. If a DIP package is already a challenge, handling an LQFP package can seem like science fiction.

BA39.jpg
The FT232 has provided USB connectivity for many platforms

Microchip saw an opportunity in this confusing landscape and introduced the MCP2200, a complete USART-to-USB converter, along with tools for configuring it to suit different applications. But let’s not skip ahead. Starting with the MCP2200 before learning serial communication would be like beginning a movie halfway through: you might see an exciting ending without knowing how it happened. (If you know a film where the villains win, let us know.)

The MAX232 interface

The MAX232, made by Maxim, adapts the signals between a device using TTL voltage levels—such as our microcontroller—and equipment compliant with EIA/TIA-232E and V.28/V.24. TTL operates between 0 and 5 volts; RS-232 uses voltages from −12 to +12 volts. The MAX232 is designed for 5-volt equipment and needs only a few external components to work properly. It is an effective way to bridge these different voltage levels.

BA3A.jpg
External and internal structure of the MAX232

Inside, the MAX232 includes the circuitry needed to generate the voltages on both sides of the link, along with four voltage-conversion inverters. Two handle TTL-to-RS-232 conversion and two handle the reverse direction. Most microcontroller projects need just one of each. Special applications, such as RS-485 networks, may use an additional connection to control bus priority. In that arrangement, the computer’s RTS (Request To Send) signal passes through a MAX232 receiver and reaches the RS-485 bus controller at TTL voltage levels.

BA3B.jpg
Proposed circuit for a microcontroller / RS-232 interface

Our circuit uses all the MAX232 sections, including ones that could prove useful in other applications. The board matches the dimensions and connector layout reserved on the NeoTeo trainer, so it can plug into the trainer and form a single unit, as shown in the videos. To keep costs down, we did not use a double-sided printed circuit board. Note an omission in the sample PCB drawing: the connected anodes of the LED diodes must be tied to the board’s positive supply. You can see this jumper in the second video; the connection points are marked in yellow in the drawing below. Feel free to design your own board and arrange the components to suit your needs.

BA3C.jpg
Printed circuit board used in our project
BA3D.jpg
Finished board

The LED colors follow examples we have seen on other boards, not a recognized color code: red indicates reception (Rx), and green indicates transmission (Tx). Yellow marks the two auxiliary connections, distinguishing them from the data-traffic indicators. These lights make it easier to check that the board is working. If needed, RTS and CTS are connected to the trainer using external jumpers.

What will the project do?

We will make a simple data mirror. Connect the computer’s serial port to a program that can send and receive data; Windows users, for instance, can use HyperTerminal. Many programs for creating, simulating, compiling, and debugging PIC code also include a serial communications utility for working with a remote terminal connected to any COM port on the computer. In the videos, we use the utility included with Proton (PIC Basic).

The PIC program follows nine basic steps. The same ideas apply to other programming languages; here we demonstrate them in Basic:

  1. Specify the PIC and its clock speed.
  2. Tell the system that a Bootloader will be used.
  3. Initialize the LCD and define the USART settings.
  4. Declare the program variables.
  5. Put the PIC in listening mode (HSERIN).
  6. If no data arrives within one second, jump to the waiting message.
  7. Receive the data and display it on the LCD (PRINT).
  8. Send the received data back to the computer (HSEROUT).
  9. Restart the cycle.

These nine basic procedures create a data mirror: data comes in, appears on the display, and returns to its source, like an image reflected in a mirror. The illustration shows the nine stages.

BA3E.jpg
Example program and the nine key stages of the process

Data is transmitted and received at 38,400 bps. Check your operating system’s control panel and make sure the computer’s serial port is set to the same speed. It is usually set to 9,600 bps by default, so change it. Alternatively, set HSERIAL BAUD to 9600 in the microcontroller program. All other settings must match the software you use to send serial data.

In the final video, the 16-character text fits on the first LCD line. “Hola Neotenianos” is one example of a string that length. The second line is reserved for the waiting message in step 6. The video also shows the LEDs lighting up as data moves through the connection. They are especially useful when troubleshooting: are data arriving? After they appear on the display, are they being sent back, or has the program gone off track? The LEDs make these questions easy to answer—and look good, too.

In summary

We used the NeoTeo trainer to communicate between a PIC and a computer through its serial port. A MAX232 adapts the voltage levels between them. We loaded the PIC firmware with the Bootloader system used in earlier articles, then chose whichever serial communications software was most convenient. Set the same data rate on the computer and the PIC, and you are ready to experiment with serial connectivity. Software that monitors the computer’s ports in detail can help you test the application more thoroughly. For our next goal—implementing the MCP2200—this is the first step. We are walking now; don’t miss the race toward USB.