Home > Data Acquisition System > (3) Hi-TECH C code for USART of PIC16F877A – Serial Communication

(3) Hi-TECH C code for USART of PIC16F877A – Serial Communication

Connections for serial communication

The PIC16F877A has one USART module which is used for serial communication. The PIC16F877A is working at 0 and 5 Volt logic level whereas the serial port on PC communicates at logic level +/-15 Volts. So we need a logic level converter and that is MAX232 here. The connections of MAX232 with PIC16F877A and PC serial port is shown in above figure.

The following program is compiled in MPLAB with Hi-Tech C compiler. This program initially configures the UART module with 115200 baud rate, no parity, no flow control and one stop bit. It enables the receive interrupt. When a byte is received by UART module it is stored in RXREG register, a RXIF flag rises which gives interrupt and interrupt subroutine “void interrupt receive(void)” is automatically called up on this interrupt.

To test this program whether it is able to communicate via serial we will transmit the received byte. The transmission begins when the TXREG register is loaded. TRMT bit is the status bit of transmission. if it is ‘0’ the transmission is in process and if it is ‘1’ then it indicates that transmission is completed and it is ready to transmit new byte.

So friends just have a look at this program. It will be easier to understand if you had gone through datasheet of PIC16F877A once.





void uart_init(void);
void TX(unsigned char TX_BYTE);

unsigned char a;
void main()

void uart_init(void)
TRISC7 = 1;
TRISC6 = 0;
SPBRG = 5;     // 115200 BRGH = 1; 11.0592 Mhz
//    SPBRG = 129; // 9600 brgh = 1; 20 Mhz
//    SPBRG = 64;  // 19200 brgh = 1; 20 Mhz
TXSTA = 0x24;
RCIE =1 ; GIE =1; PEIE =1;
RCSTA = 0x90;

void interrupt receive(void)
if(RCIE && RCIF)
RCIF = 0;

void TX(unsigned char TX_BYTE)




Project Wizard

  • Start MPLAB IDE.
  • Click Project menu and open Project wizard. click Next.
  • Select PIC16F877A as your Device.
  • Select HI-TECH Universal Tool Suite in front of Active Toolsuite. (Note : Hi-TECH compiler must be installed in your system.)
  • Give a name of project and select the location of project.

Project is successfully created.!!

Project view

Now you will be able to see project window. if it is not shown than you can make it visible by clicking View / Project.

  • Add serial.c to source file.
  • Build the serial.c by clicking the black square with red spot as shown in the figure.
  • The Output window showing how much memory is used for the program will appear.

When you build any program it will create a .HEX file and its name will be same as your project name in the same directory where the project is saved. Here in my case my project name is Bharat.mcp is my project so when i build my project it generates Bharat.hex file. This hex file is to loaded in PIC16F877A. Here i will just show the simulation result in PIC Simulator IDE.


Simulation Results


Here is the simple steps to simulate programs.

  • Install PIC Simulator IDE on your system. Open PIC simulator IDE.
  • Goto File/Load Program Load the .HEX file which was created by building the project.
  • Now open HardWare UART Simulation Interface from Tools menu.

PIC simulator IDE

  • Select Microcontroller–> PIC16F877A and Clock Frequency –> 11.0592 Mhz
  • Start Simulation from Simulation/start.

Now you can use Send Byte(Hex) and Send String to test the program. The window will show the received bytes. You can see received bytes in ASCII characters or in HEX format.

So, Now we successfully done the serial communication between PIC16F877A and PC. Next we will see how to use ADC module.

<<previous                                                                                             Next>>

  1. roshieldr
    16/05/2012 at 9:41 PM

    hello, i am an undergraduate student working microprocessor integrate with bluetooth for ECG monitoring system. your post has helped me a lot. i just have one question, can this be used with bluetooth module? i assume that it is the same because bluetooth module also have serial configuration. can you please explain? thank you very much!

    • 17/05/2012 at 5:05 AM

      i dont know much about bluetooth serial devices because never used them before. sorry!

      • jeyshree
        07/01/2013 at 12:22 AM

        hai sir,
        what software did you use for drawing the circuit?i am using proteus.but the code does not work for me?i use a clock of 20MHz.instead of connecting to max232 i connected the transmitter of pic16f877a to the hyper terminal block.when i set the baud rate as 19200 and SPBRG as 64,instead of transmitting ‘b’ it transmits some garbage value.

      • 09/01/2013 at 3:22 PM

        I drew it with Microsoft Visio, But hey just to be clear i checked this code with actual hardware and PIC simulator IDE. Dont know if it will work with proteus software. I never used proteus software before.

      • 16/01/2013 at 7:05 PM

        In serial comm. the frequency is most and 1st important factor so, You must have to replace crystal. the given code is works fine as per the configuration of manf.(microchip) device data sheet. it happening due to the improper frequency generated by crystal.

        chk it, if you select another baud rate in terminal or if you change the baud rate value in code you can see the proper output in terminal and also in hyper terminal.or change the crystal w.r.t random values of Baud rate also see the crystal is as closed as of MUC.
        and also remember to remove soldered flux around from the crystal.

  2. 01/06/2012 at 6:00 PM

    Hello, roshieldr I’a Interested in ECG.I Can Help U .I’ve AUBTM-20 used that BT Module but not in audio enable BTM. It is coming with other version of BTM. See AUBTM-23 Data Sheet.

  3. i.Wus
    18/11/2012 at 8:46 AM

    How I can write some text to serial LCD (2×16) using only TX pin for pic16f688?
    I want create the program for metal detector (pulse induction). But I have difficulties with serial lcd code. Program is created without LCD EUSART code.
    Thank You.

  4. Rohan
    10/04/2013 at 7:56 AM

    sir,just want to bring the binary data obtained on reciever
    pin of pic 16f877a to my pc hyperterminal,,will this code work,,

    • 10/04/2013 at 6:32 PM

      Above code is just echoing your data. Whatever serial 8 bit data PIC16F877A gets through its RX pin is transmitted back via TX pin. So if I understood your question correctly, that is what you want to do. so this code will work for you.

  5. Robert
    05/11/2013 at 12:45 AM

    Good day sir!
    This is a great code and i am thinking of how i can modify it so that when i send a certain string, it will echo back a pre-determine string. Like if i send 1234, it will echo “hello” or do something..
    I’ve been playing with these code for a while but i can’t seem to get it right. Please give me some tips. Thanks 🙂

  6. 30/11/2013 at 11:49 AM

    Hi Robert,

    First thing you try is check your crystal frequency, depending upon crystal frequency baudrate may defer from stardard baudrate that is supported by PC.

    and Divide ans solve your task part by part.
    First just program your PIC to transmit some string/char repeatedly.
    Then try receiving and sending.
    Then receive compare and send.


  7. 28/02/2014 at 4:41 PM

    Spot on with this write-up, I absolutely believe that this website needs far more attention.

    I’ll probably be returning to see more, thanks for the

  8. 16/08/2014 at 4:40 AM

    I have been exploring for a bit for any high-quality articles or weblog
    posts in this sort of area . Exploring in Yahoo I at last stumbled
    upon this website. Reading this information So i am satisfied to show
    that I’ve a very just right uncanny feeling I found out exactly what I needed.
    I such a lot for sure will make certain to do not forget this
    website and give it a look regularly.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: