RS232 is a asynchronous serial communication protocol widely used in computers and digital systems. It is called asynchronous because there is no separate synchronizing clock signal as there are in other serial protocols like SPI and I2C. The protocol is such that it automatically synchronize itself. We can use RS232 to easily create a data link between our MCU based projects and standard PC. Excellent example is a commercial Serial PC mouse (not popular these days, I had got one with my old PC which I bought in year 2000 in those days these were famous). You can make a data loggers that reads analog value(such as temperatures or light using proper sensors) using the ADC and send them to PC where a special program written by you shows the data using nice graphs and charts etc.. actually your imagination is the limit!
Basics of Serial Communication.
In serial communication the whole data unit, say a byte is transmitted one bit at a time. While in parallel transmission the whole data unit, say a byte (8bits) are transmitted at once. Obviously serial transmission requires a single wire while parallel transfer requires as many wires as there are in our data unit. So parallel transfer is used to transfer data within short range (e.g. inside the computer between graphic card and CPU) while serial transfer is preferable in long range.
As in serial transmission only one wire is used for data transfer. Its logic level changes according to bit being transmitted (0 or 1). But a serial communication need some way of synchronization. If you don’t understand what I mean by "synchronization" then don’t worry just read on it will become clear.
The animation below shows you how a serial transmission would look like (if you can see electricity).
Fig- A Serial Line.(HIGH=RED & LOW=WHITE)
Can you make out what data is coming? No because you are not synchronized. You need a way to know when a new byte start and when a bit ends and new bit start. Suppose the line is low for some time that means a ‘0’ but how many zeros? If we send data like 00001111 then line is first low for some time and high after that. Then how we know it is four ‘0’s and four ‘1’s?
Now if we add another line called the clock line to synchronize you then it will become very easy. You need to note down the value of data line only when you see the "clock line" high. Lets understand this with help of an animation.
Fig- A Serial Line.(HIGH=RED & LOW=WHITE)
Now you can see how the "clock" line helps you in "synchronizing" the incoming data. In this way many serial busses like SPI and I2C works. But USART is different in USART there is no clock line. So it is called UART – Universal Asynchronous Receiver Transmitter. In USART a start bit and stop bits are used to synchronize the incoming data the.
In RS232 there are two data lines RX and TX. TX is the wire in which data is sent out to other device. RX is the line in which other device put the data it need to sent to the device.
Fig- RS232 transmission. The arrows indicates the direction of data transfer. In addition to RX/TX lines there is a third line i.e. Ground (GND) or Common.
One more thing about RS232. We know that a HIGH =+5v and LOW=0v in TTL / MCU circuits but in RS232 a HIGH=-12V and LOW=+12V. Ya this is bit weird but it increases the range and reliability of data transfer. Now you must be wondering how to interface this to MCUs who understand only 0 and 5v? But you will be very happy to know that there is a very popular IC which can do this for you! It is MAX232 from Maxim Semiconductors. I will show you how to make a level converter using MAX232 in next tutorial.
As there is no "clock" line so for synchronization accurate timing is required so transmissions are carried out with certain standard speeds. The speeds are measured in bits per second. Number of bits transmitted is also known as baud rate. Some standard baud rates are
- … etc
For our example for discussion of protocol we chose the speed as 9600bps(bits per second). As we are sending 9600 bits per second one bits takes 1/9600 seconds or 0.000104 sec or 104 uS (microsecond= 10^-6 sec).
To transmit a single byte we need to extra bits they are START BIT AND STOP BIT(more about them latter). Thus to send a byte a total of ten bits are required so we are sending 960 bytes per second.
Note: The number of stop bits can be one or two (for simplicity we will be using single stop bit)
There is one more bit the parity bit but again for simplicity we would not be using it)
RS232 Data Transmission.
The data transfer is done in following ways
- When there is no transmission the TX line sits HIGH (-12V See above para) ( STOP CONDITION )
- When the device needs to send data it pulls the TX line low for 104uS (This is the start bit which is always 0)
- then it send each bits with duration = 104uS
- Finally it sets TX lines to HIGH for at least 104uS (This is stop bits and is always 1). I said "at least" because after you send the stop bit you can either start new transmission by sending a start bit or you let the TX line remain HIGH till next transmission begin in this case the last bit is more than 104uS.
Fig- Data Transmission on RS232 line.
- The receiving device is waiting for the start bit i.e. the RX line to go LOW (+12V see above para).
- When it gets start bit it waits for half bit time i.e. 104/2 = 51uS now it is in middle of start bit it reads it again to make sure it is a valid start bit not a spike.
- Then it waits for 104uS and now it is in middle of first bit it now reads the value of RX line.
- In same way it reads all 8 bits
- Now the receiver has the data.
Fig- How the Receiver receives the data on RS232 RX line
To be continued …
Ok friends that’s it for now meet you in next tutorial. Till then good bye!
If you are facing any difficulties in understanding the topic, feel free to post a comment and I will surly help you as soon as possible.
Other Parts of this Tutorial
- RS232 Communication – The Basics
- RS232 Communication – The Level Conversion
- Using the USART of AVR Microcontrollers.
- Using the USART of AVR Microcontrollers : Reading and Writing Data
- Visualize ADC data on PC Screen using USART – AVR Project
We highly recommend EasyEDA for PCB Design and low cost PCB Prototype.
Facing problem with your embedded, electronics or robotics project? We are here to help!
Post a help request.