Summary of UART Example for PIC16F887 microcontroller using CCS PIC C compiler
This article demonstrates using the PIC16F887 built-in USART as a UART with CCS C. It shows required hardware (PIC16F887, MAX232, capacitors, COM port, breadboard, 5V, jumpers), wiring notes (internal oscillator, MCLR disabled, RS232 COM connection), example CCS C code configuring rs232 at 9600 baud, and explains functions printf, putc, kbhit, getc. The author programmed the PIC with PICkit3 and verified operation with the CCS IDE serial monitor.
Parts used in the PIC16F887 UART Example:
- PIC16F887 microcontroller
- MAX232 level shifter
- 4 x 10uF polarized capacitor
- Female COM port
- Breadboard
- 5V power source
- Jumper wires
- PICkit 3 programmer (used to burn HEX)
- RS232 male-female cable (PC COM to female COM)
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:
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.
- What is the baud rate used in the example?
9600 baud as configured by #use rs232(UART1, baud = 9600). - Which pins are used for hardware UART on PIC16F887?
Hardware UART uses pins RC6 (TX) and RC7 (RX) by default. - Can I use software UART instead of hardware UART?
Yes, the compiler can generate a software UART using #use rs232(xmit = PIN_D0, rcv = PIN_D0, baud = 9600). - Which functions send data over UART in the example?
printf and putc are used to send strings and characters respectively. - How does the code check for received data?
It uses if(kbhit()) to test if a character is ready, then getc() to read it. - What oscillator and fuse settings are used?
Internal oscillator at 8MHz with fuses NOMCLR, INTRC_IO, NOBROWNOUT, NOLVP; setup_oscillator(OSC_8MHZ) is called. - What hardware is required to convert PIC TTL UART to PC RS232 levels?
The MAX232 with capacitors is used to convert TTL to RS232 levels for the PC COM port. - How was the program loaded into the PIC16F887?
The HEX file was burned into the PIC using a PICkit 3 programmer.
