UART Example for PIC16F887 microcontroller -CCS C compiler

The microcontroller PIC16F887 has a build in USART (Universal Synchronous/Asynchronous Receiver/Transmitter) module. This module can be used as UASAT or UART.
This small post shows an example for the usage of the UART protocol with PIC16F887 microcontroller.

Hardware Required:

  • PIC16F887 microcontroller
  • MAX232 — datasheet
  • 4 x 10uF polarized capacitor
  • Female COM port
  • Breadboard
  • 5V power source
  • Jumper wires

UART Example for PIC16F887 circuit:

PIC16F887 UART example with max232

In this example the microcontroller PIC16F887 uses its internal oscillator and MCLR pin function is disabled.
The female COM port is connected to the PC using RS232 cable, this cable has to be male-female because the PC COM port is male.

UART Example for PIC16F887 CCS C code:
The code used in this example is shown below.
The function #use rs232(UART1, baud = 9600) is used to configure the UART protocol. Here the hardware UART module is used. If the pins TX and RX (RC6 and RC7) are used by an other application we can use software UART. Software UART is generated by the compiler with the same previous function. For example TX is mapped to pin RD0 and RX to pin RD1:
#use rs232(xmit = PIN_D0, rcv = PIN_D0, baud = 9600)
where 9600 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).
if(kbhit()): test if a character is ready for getc() function.
getc(): read the character.

After I build the circuit and burned the HEX file into the PIC16F887 using PICkit 3 programmer, I got a result as shown in this video (using CCS C Compiler IDE Serial Monitor tool):


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

1 thought on “UART Example for PIC16F887 microcontroller -CCS C compiler”

  1. // Receive and send data via UART
    while(TRUE){
    if(kbhit()){ // If a character available
    j = getc(); // UART read
    putc(j); // Send it back
    }
    }

    not works!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top