My ChipCenter Knowledge Center Guides & Experts Product Reviews Ask Us Online Tools Circuit Cellar Resources Webcasts Careers


Data Sheets App Notes Ask Us
Search Type:
Search for:






Email this Article   |  Printer Friendly Page
RS-232 Fulfilling All Your Communication Needs

RS-232: FULFILLING ALL YOUR COMMUNICATION NEEDS

RS-232 is the most universal serial communication in the computer world. It has been used in many embedded systems as a way to communicate information back to a computer. Although there are some speed and length constraints, RS-232 often has what it takes to fulfill your communication needs.

We are going to explore the rudimentary fundamentals of how RS-232 works and give some good pointers on how to "bit bang" some RS-232 back to your computer. For more information on the actual protocol standards, I recommend that you check the EIA (Electronics Industry Association) RS-232-C standard. This page isn't intended to be a complete source on how to make RS-232 work with your system. If you have any additional questions and don't want to wait for additional articles, mail me and I will help with what I can.

Communications with peripherals or between embedded systems often runs into one overriding restriction, pin count. It is often desirable to reduce the pins necessary for communication between devices. In the embedded world, three forms of communication are used over and over between embedded devices: RS-232, I2C, and Microwire. Each of these protocols has their own advantages and disadvantages.

RS-232 is a great choice if you want to include a computer in your communication loop since the 16550 UART chip or something equivalent has been in every computer since the 80's. If you have a computer that was made earlier, you may consider an upgrade. Another advantage is scalability. The RS-232 protocols can use several pins for flow control, initialization, and data flow, or in its simplest form, just use three (transmit, receive and ground reference). This serial communication is available in several small microcontrollers to aid the transfer of data with minimal overhead. Usually, they aren't listed as "RS-232 built in", but rather as UART, or shift register. Let's look a little into how to make this protocol work for your device.

First, we will establish a few ground rules when we are talking about our devices. There are two devices that are mentioned in the standard, Data Terminal Equipment (DTE) and Data Communications Equipment (DCE). The DTE could be considered the master device i.e. the computer and the DCE would be your peripheral or embedded system. The logic "1"; and logic "0" are now called mark and space respectively. A logic "1" or mark ranges from -3 to -25 volts while a logic "0" or space is from +3 to +25 volts. You only need to follow this definition if you are going to try to talk to a computer. It may not be necessary if you are controlling both ends of the communication. If you are including a computer in your communication, you will need to meet these voltage constraints. I would recommend using a voltage converter chip to handle altering the voltages for you. I recommend MAXIM 233, or National DS14C232 chips. I have found the national part to be much cheaper. You might even be able to get a free sample or two at http://www.national.com.

We will only concentrate on the data communication rather than the framing of data or flow control via the other pins of the standard 9-pin connector. Each chunk of data is framed by a start bit(mark) and a stop bit(space). The data chunks can vary in length, but 8 bits seems the most common by far. Stop bits can also vary in length. The length of 1 (meaning the stop bit is the same length as a data bit or the start bit) is also very common. There are some provisions made for parity checking etc., but we will leave parity checking for another article. The remaining characteristic is that data is sent with the least significant bit first.

So a byte of data would be sent as follows:

Start bit — bit0 — bit1 — bit2 — bit3 — bit4 — bit5 — bit6 — bit7 — bit8 — Stop bit

Now sending out the bits should just be simple to write into your code. Load a register with the byte to be sent, send the start bit, then loop until all bits of data has been sent, followed by a stop bit. For an easy way to manipulate bits on ports or in registers, refer to my article "Back to Basics".

Seemingly simple right? Well, it is, but I also neglected to stress the importance of timing. RS-232 is an asynchronous communication, meaning there isn't a separate clock pin to let you know when the line carrying the data is valid . This requires a set bit rate that needs to be understood by the receiver and sender. If you've played around with your computer's serial ports at all, you've probably noticed that all the available baud rates are multiples of 300 bits per second. I don't know when or why that was established, but it makes it very difficult to divide a 4, 8, 12, etc MHz crystal down to update the data pin state at the appropriate time to achieve what ever multiple of 300 bps you are shooting for. Save yourself a lot of time and just get a crystal that is made for this type of baud rates. They are the 1.8432 MHz crystals and multiples of that frequency i.e. 3.6864, 11.0592, 14.7456, etc. Those frequencies will give you an easy path to set up an internal timer within your microcontroller to get your multiple of 300 bps. Now the thing that remains is to set up your output structure, defined above, to update each timeout of your timer rather than in a quick loop.

Being on the receiving end is only slightly more difficult. The preferred option is to set up an interrupt to trigger on the start bit and then time off that that moment for the next data bit, and so on. You will need to leave a small delay after the start bit to assure that your next check on the receive line isn't near a transition edge but rather towards the middle of the bit where the data line is stable.

Let me know if you would like some additional information about RS-232 protocol. I have included some great links with good diagrams and explanations about the serial port on a computer.

http://www.geocities.com/SiliconValley/Bay/8302/serial.htm
http://www.geocities.com/SiliconValley/Bay/8302/serial1.htm
http://www.camiresearch.com/Data_Com_Basics/RS232_standard.html

Embedded Engineering

Guides and Experts   Analog Avenue   PLD EDA Tools   PLD   DSP   EDA   Embedded Systems   Power   Test


Email this Article   |  Printer Friendly Page

Copyright ©2001 eChips
About ChipCenter  Contact Us  Privacy Statement   Advertising Information