RS232 Communication with PIC Microcontroller
This article shows how to do a simple communication via a RS232 interface with a PIC microcontroller. RS232 is a standard for a serial communication interface which allows to send and receive data via at least three wires. With the RS232 interface it is possible to setup a connection between a microcontroller and a PC (via PC’s COM port) or between two microcontrollers.
The RS232 interface can be used for many purposes like sending commands from a PC to a microcontroller, send debug information from a micontroller to a terminal, download new firmware to the microcontroller and many other things.
In this tutorial I will show how to link a PIC microcontroller to a standard PC. On the PC we will use a termial program to send and receive data. Data sent by the microcontroller will be shown in the terminal window and any key pressed inside the terminal will send the corresponding key code to the microcontroller. We will use this simple configuration to test and understand the RS232 communication.
Note that modern PCs don’t have a serial port so you need to get a USB to serial converter. They are available at low cost.
The following block diagram shows the whole setup:
For serial communication the line used to transmit data is called TX and the line used to receive data is called RX. The level converter is required to translate the voltage level of the microntroller to RS232 voltage level. The microntroller operates at TTL level (0V = logic 0, +5V logic 1) whereas RS232 uses around +/-12V. A very famous RS232 level converter is the MAX232 chip.
In the schematic below a PIC microcontroller is connected to the RS232 level converter chip. A PIC18F2620 micocontroller is used, but it will also work with any other microcontroller which has a built-in UART.
The PIC is running at 10MHz. This will be important later when we configure the baudrate for the serial communication.
Ther RS232 level converter uses the famous MAX232 chip, but any other MAX232 compatible chip will also work. It just requires 4 capacitors to do its job. These external capacitors are required for the charge pump inside the chip which generates the required voltage levels.
The connections on the DB9 connector between pins 1,4,6 and 7,8 are required to satisfy the RS232 hardware handshake signals which we will not use here.
I have developed a RS232 module which allows direct connection to the microcontroller. It consists of a DB9 Female connector, a MAX232 compatible RS232 level converter and the capacitors. You can find the RS232 module here.
To connect the above circuit to the PC we need a RS232 cable. The below picture shows the necessary connections.
Now since the hardware is ready we have to write the software for the PIC microcontroller. The different compiler vendors provide different ways to setup the UART in the PIC. So I will show how to use the UART for different compilers.
RS232 communication with CCS C compiler
The CCS C compiler provides a very simple way to do serial communication via RS232. It hides all the register settings for the user. Only the some parameters have to be provided, the rest is done by the compiler. By the way, the CCS C compiler also allows to do RS232 communication via general I/O pins, i.e. software based RS232 communication instead of using the built-in UART. That is a really great feature of the CCS C compiler.
Here the code lines which are required to setup the UART for RS232 communication.
For more detail: RS232 Communication with PIC Microcontroller
EasyEDA: Ideas for Circuit Design, Innovation for Electronics Access
Free Circuit Design: Schematic – Simulation – PCB Layout – Gerber Viewer
Cheapest PCB Prototype: Only $2 for 10pcs 10×10cm PCBs, 24 hours Quick Turn, DHL Delivery in 3 Days
Current Project / Post can also be found using:
- communication cable for pic microcontroller
- rs 232 connection for pic microcontroller