Comunicacion Serial Rs232 Pic Ccs
Pin RC6 (TX) and pin RC7 (RX) are used for the UART (serial) communication between the microcontroller and the computer. To change between TTL logic levels (5V) and RS232 signals (+/-12V), an IC is needed which is max232. Don’t connect TX and RX pins directly to an RS232 serial port which may damage your microcontroller. CCS's powerful #use rs232 library has now added transmit buffering, receive buffering and flow control. API for the serial library remains unchanged ( getc, putc, printf ), existing code that uses this API can add buffering and flow control by simply changing the #use rs232 configuration.
The microcontroller PIC16F84A has no hardware UART module, but we can use software UART to send/receive data to/from the PC via RS232 cable (COM port). This topic shows a simple example for the use of the UART protocol with the PIC16F84A MCU.
Hardware Required:
- PIC16F84A microcontroller
- 8MHz crystal
- 2 x 22pF ceramic capacitors
- 10K ohm resistor
- MAX232 — datasheet
- 4 x 10uF polarized capacitor
- Female RS232 connector
- Breadboard
- 5V power source
- Jumper wires
UART Example for PIC16F84A microcontroller circuit:
To interface the PIC16F84A MCU with the PC we need MAX232 signal level converter as shown in the circuit diagram. There are 2 lines between the PIC16F84A and the MAX232 and also two lines between the MAX232 and the female COM connector. One line for data transmit and the other one for data receive.
The female COM connector is connected to the PC using RS232 cable, this cable has to be male-female because the PC RS232 port type is male.
In this example the PIC16F84A MCU runs with 8MHz crystal oscillator.
UART Example for PIC16F84A microcontroller C code:
The function #use rs232(xmit = PIN_B4, rcv = PIN_B5, baud = 2400) is used to configure the UART protocol. Here software UART is used.
where:
xmit is the transmit pin (RB4)
rcv is the receive pin (RB5)
2400 is the baud rate.
The functions used in the C code are:
printf: sends a string of characters over RS232 transmission pin (TX).
putc: send a character over RS232 transmission pin (TX).
getc(): read character if available.
Complete C code for CCS C compiler is below.
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 | #fuses HS, NOWDT, PUT, NOPROTECT #use fast_io(B) #use rs232(xmit = PIN_B4, rcv = PIN_B5, baud = 2400) constcharmessage[]='***** PIC16F84A microcontroller UART example *****'; voidmain(){ delay_ms(2000);// Wait 2 seconds // Print text printf('nr');// Start new line while(message[i]!='0'){// Loop until the end of the string delay_ms(100);// Wait 100 ms } printf('nr');// Start new line printf('%unr',i);// Print i and start new line } // Receive and send character via UART j=getc();// UART read character } |
This article series aims at teaching serial communication between a PIC microcontroller and a PC. We first introduce you with what is serial communication is and how it can used. Then we tell you how to perform serial communication using PIC microcontroller and how we use the USART peripheral for the purpose. We will tell you how our usart library for PIC16F series can be used for easy serial communication, in this part we also discuss how to set up a MPLAB X project for using the USART library. After that we will build a demo project to explore the library. Finally we will burn this demo in a PIC16F877A and establish a serial communication with PC.
Serial Communication
Their are several serial communication standards like RS232, SPI, I2C etc. Of which RS232 is a asynchronous method. That means it does NOT have a synchronizing clock line. One way data requires only one conductor line. Since it is a two way communication their are two lines between the two device. One for sending data called the Tx and one for receiving data called the Rx.
The communication is full duplex, that means data can be sent at the same time data is being received.
Comunicacion Serial Rs232 Pic Ccs 2017
generally other serial communication like SPI and I2C are used for short range communication like between two ICs placed on same board or system. While RS232 based serial communication is used for short range as well as long range communication (cables length longer than 2 meters).
High voltage input output and requirement of fewer wires makes it ideal for long range communication.
PC’s Serial Port
In old days PC’s were equipped with a serial port since on those days many peripherals like modems and mice were connected using serial ports. But now a days they are replaced by USB ports which are much more advanced. But working with serial port is much easier from a designers point of view.
But their is no need to worry with the absence of serial port from PCs. You can purchase a USB to Serial Adapter to connect all your serial devices with PC using USB port!
Fig. USB to Serial Converter |
Although as you can see from the image above a standard serial port connecter is a 9 pin d type male connecter, but only three pins are used for our purpose. You can see their position in the image below.
Fig. DB9 Male Pin out |
Connecting to DB9 Male
To connect your hardware with DB9 male of serial port you need a DB9 female connecter as shown in the image below.
Fig. DB9 Female |
You can solder this connecter on your PCB to be able to connect with serial port easily.
Fig. DB9 Female on PIC 40 PIN Development Board |
Images below shows how easy it is to connect a USB to Serial Converter to the board.
Fig. Connecting with Serial Port |
Fig. Serial Port Connected |
Level Conversion
Comunicacion Serial Rs232 Pic Ccs En
At this point you should become familiar with a point that voltage level on RS232 line is different from the voltage levels generally used in microcontrollers and other ICs. So to interface RS232 level signals to our MCUs we need a 'Level converter'.
What a level converter will do is to convert RS232 level signals (HIGH=-12V LOW=+12V) from PC to TTL level signal (HIGH=+5V LOW=0V) to be fed to MCU and also the opposite.
Comunicacion Serial Rs232 Pic Ccs De
Fig. Working of Level Converter |
As RS232 is such a common protocol there is a dedicated IC designed for this purpose of 'Level Conversion'. This IC is MAX232 from Maxim. By using charge pumps it generates high voltages(12V) and negative voltages(-12V).
Schematic For Level Converter
Fig. Schematic for Level Converter. |
Bill of Material for Level Converter
Reference | Value | Quantity |
C1,C2,C3,C4 | 1uF 25 Electrolytic Capacitor | 4 |
C5 | 10uF 25 Electrolytic Capacitor | 1 |
U1 | IC MAX232 | 1 |
J1 | DB9 Female R/A PCB Mountable | 1 |
PIC16F877A’s Serial Port
The serial communication pins of PIC16F877A is shown in the image below.
Fig. PIC16F877A’s Serial Port. |
You can see that pin 26 is RX pin while pin 25 is TX pin.
Serial Rs232 To Usb
Complete Schematic for USART Demo
Serial Rs232 Cable
Fig. Schematic for USART Demo.(Click to Enlarge …) |
Comunicacion Serial Rs232 Pic Ccs De
This article described only the hardware setup required for interfacing PIC16F877A with PC using USB to serial converter. The software part consisting of the USART library for PIC, its setup and use with MPLAB X IDE and XC8 compiler are described in a separate article.
By Avinash Gupta
www.avinashgupta.com
Facing problem with your embedded, electronics or robotics project? We are here to help!
Post a help request.