
Pic16f690 Serial Communication C Codes
USART Serial Communication; Controlling an Analog Servo. Programming Examples using the PIC16F1xxx MCU Summary. This page contains examples of four program tasks using an enhanced mid-range PIC ® MCU. Each example page shows the C language code needed to complete the task.
- PIC16f877 Microcontroller
- 16×2 lcd
- MAX232
- PC with DB-9 or DB-25 Port
- Potentiometer (For Setting LCD Contrast)
- Crystal 20MHz
- Connecting Wires & Power Supply
Microcontrollers works on TTL(Transistor Transistor Logic) wave form & standard PC(Personal Computers) works on RS-232 level wave form. Serial Data Transmitted by PC is in RS-232c level wave form. We have to convert this RS-232c wave form data in TTL form, to pass it to PIC16f877. The best way is to use MAX-232 ic. MAX-232 converts RS-232c level wave form data in TTL & TTL data in RS-232c level. Below is a simple Diagram of both the wave forms.
Serial communication using Pic microcontroller – Circuit diagram
USART of PIC 16f877 is present at Pin 25 & 26 of PIC 16f877 Microcontroller. USART Include Port-C Pin#6 & 7. RC6 can be used as TX(Transmission) and RC7 can be used as RX(Reception). Since we are receiving data from PC and then displaying it on 16×2 lcd so we are only concerned with RX(Reception) pin of USART. This RX pin is connected to Pin#12(R1OUT) of MAX-232. Pin#13(R1IN) of MAX-232 is connected to PIN#3 of DB-9 Port.
Both the tutorials above are very important for you, if you are new and didn’t use the stuff before in any project. I recommend you to go through them it will help you a lot in understanding the circuit diagram and working of both the things. Circuit diagram of the project is given below.
Pic microcontroller serial communication – Project code
In the main function first TRISC7=1 is initializing RX pin as input. TXSTA(Transmit Status and Control Register) is initializing USART in Asyncronous Mode with High Speed Baud Rate. 0x04 is loaded in TXSTA.
The data received at RX pin is placed in RCREG register. When any byte is received, RCREG register becomes full and it raises RCIF(Receive interrupt flag) flag of PIR1 register. When RCIF flag becomes high i pick data from RCREG register. This logic is implemented in while(1) loop. While one continuously receive data from PC.
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.
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 PortThe fugees the score full album. |
Fig. Serial Port Connected |
Level Conversion
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.
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.

Complete Schematic for USART Demo
Fig. Schematic for USART Demo.(Click to Enlarge …) |
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
JLCPCB Prototype: $2 for 10 pcs PCBs, 48 Hours Quick Turn
Facing problem with your embedded, electronics or robotics project? We are here to help!
Post a help request.