Pic16f690 Serial Communication C Codes

Here is a simple tutorial on how to receive serial data from PC(Personal Computer) Hyper-terminal and display it on 16×2 lcd using PIC16f877 microcontroller. Its not much difficult you just need to know how to use USART(Universal Synchronous-Asynchronous receiver transmitter) of PIC 16f877. Serial data is transmitted and received by PC using DB-9 OR DB-25 port of PC.

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

Port-B of PIC 16f877 is connected to data pins of 16×2 lcd. RS(Register-Select) Pin of lcd is connected to Port-D Pin#6. En(Enable) Pin of lcd is connected to Port-D Pin#7. RW(Read-Write) Pin is grounded Because we only want to write to lcd. You can also connect RW pin to Port-D Pin#5 Because Rw is also programmed in software burned in PIC 16f877.
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.

Serial Data Received from PC and displayed on 16×2 Lcd using Pic16f877A USART

Pic microcontroller serial communication – Project code

Code is written in c language using MPLAB-ide and High Tech C compiler is used to compile the code. First High Tech C-compiler header file htc.h is imported in the project. Then Crystal frequency is defined which is 20MHz. RD7,RD6 & RD5 pins of Port-D are initialized as EN, RS & RW, Control pins for 16×2 lcd. lcdcmd() function is sending commands to lcd and control these commands. display() function is sending data to lcd, controls this data by controlling EN,RW & RS pins of lcd. lcdint() function is initializing our lcd and Microcontroller ports.

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.

RCSTA(Receive status and control register) Register is used for enabling USART of PIC 16f877. To enable USART make bit#7 (SPEN) high. I loaded 0x90 in RCSTA. Which enables our serial port and continuously receives data from PC.
Set the Baud Rate in SPBRG Register. Their are two formulas for Baud Rates Calculation. Depending on the BRGH bit of TXSTA Register. Formula for BRGH=1 is Different for BRGH=0. Since this bit BRGH selects High or Low Baud Rates thats why Baud Rate depends on this bit.
I loaded 0x81 hexadecimal in it. Its equivalent binary is 10000001 and decimal value is 129. Since mine BRGH bit is high so by using the formula for BRGH=1 the Baud Rate value for 9600 is 129. Loading the value 129 in SPBRG and keeping BRGH=1 sets our Baud Rate to 9600.

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.

Download the project files, simulation and Code(c,hex). Code is written in C Language. MPLAB-Ide is used for writing code and High Tech C-Compiler is used for compiling code. Simulation is made in Proteaus 8.0. Plz Give us your feed back on the post.

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

ReferenceValueQuantity
C1,C2,C3,C41uF 25 Electrolytic Capacitor4
C510uF 25 Electrolytic Capacitor1
U1IC MAX2321
J1DB9 Female R/A PCB Mountable1

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.

Communication

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.