As an Amazon Associate we earn from qualifying purchases.
The PIC16F877A has a built-in hardware serial port called the USART. In asynchronous mode it works as a standard UART, so the chip can exchange bytes with a PC, a GSM module, a Bluetooth module or another microcontroller using only two wires. This article shows how to set up the UART with the HI-TECH C compiler, then gives a working transmit example and a working receive example.
How the PIC16F877A UART works
On the PIC16F877A, the UART uses two pins of PORTC:
- TX is on RC6 (pin 25).
- RX is on RC7 (pin 26).
Both pins must be configured as inputs in TRISC. This is already the case after reset, so the examples below do not change it. When SPEN is set, the USART takes control of the pins.
The pins use logic levels (0 V and 5 V). A PC serial port uses RS-232 levels, so connect it through a level converter such as a MAX232, or use a USB-to-TTL serial adapter. Do not wire the PIC pins directly to an RS-232 port.
Registers you need
- SPEN (RCSTA) turns the serial port on.
- SYNC (TXSTA) selects the mode. 0 means asynchronous, which is the normal UART mode.
- BRGH (TXSTA) selects the high or low baud rate range.
- SPBRG holds the baud rate generator value.
- TXEN (TXSTA) enables the transmitter, and CREN (RCSTA) enables continuous reception.
- TX9 and RX9 select 9-bit mode. Keep them at 0 for ordinary 8-bit data.
- TXREG is where you write the byte to send, and RCREG is where you read the received byte.
- TRMT (TXSTA) is 1 when the transmit shift register is empty.
Calculating the baud rate
For asynchronous mode, the SPBRG value comes from these formulas, where X is the value in SPBRG:
- BRGH = 0: Baud rate = Fosc / (64 × (X + 1))
- BRGH = 1: Baud rate = Fosc / (16 × (X + 1))
With a 16 MHz crystal, BRGH = 0 and SPBRG = 25 give 16,000,000 / (64 × 26), which is about 9615 baud. That is close enough to 9600 for reliable communication. With BRGH = 1 at the same clock, SPBRG = 103 also gives about 9600 baud and a slightly smaller error.
If you change the crystal, recalculate SPBRG. Also set the oscillator configuration bits to match your hardware, and update _XTAL_FREQ.
Transmission
The program below sets up the UART at 9600 baud and sends the letter A over and over. It checks TRMT so that a new byte is only loaded when the transmitter is free.
#include<pic.h>
#define _XTAL_FREQ 16000000
void main() {
SPEN=1; //enables serial port
SYNC=0; //setting in asynchronous mode
BRGH=0; //selects low speed baud rate
SPBRG=25; //setting baud rate @ 9600
TXEN=1; //enables transmission
TX9=0; //sets transmission in 8-bit mode
while(1) {
if(TRMT==1) //check status of transmit shift register status for empty
{
TXREG='A'; // writes ascii value of letter A
}
}
}
Open a serial terminal on the PC at 9600 baud, 8 data bits, no parity and 1 stop bit (8N1). You should see a continuous stream of the letter A.
Reception
Note– i have used PORTB to see incoming ascii value
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
SPEN=1; //enables serial port
SYNC=0; //setting in asynchronous mode
BRGH=0; //selects low speed baud rate
SPBRG=25; //setting baud rate @ 9600
CREN=1; //enables reception
RX9=0; //sets reception in 8-bit mode
TRISB=0;
PORTB=0;
while(1)
{
PORTB=(RCREG-0x30); // shows the received digit on PORTB (ASCII value minus 0x30)
}
}
The program subtracts 0x30 from the received byte. The ASCII codes for the characters ‘0’ to ‘9’ are 0x30 to 0x39, so typing a digit in the terminal shows its numeric value on the PORTB LEDs. For example, typing 5 shows binary 00000101. Other characters give values that do not correspond to a digit.
A safer receive loop
The loop above reads RCREG continuously, even when no new byte has arrived. It works for a quick test, but it is better to act only when data is available. The RCIF flag in PIR1 is set when a byte is waiting, and reading RCREG clears it. You should also recover from an overrun error, which stops reception until it is cleared.
#include<pic.h>
#define _XTAL_FREQ 16000000
void main()
{
SPEN=1;
SYNC=0;
BRGH=0;
SPBRG=25;
CREN=1;
RX9=0;
TRISB=0;
PORTB=0;
while(1)
{
if(OERR) // overrun error: reset the receiver
{
CREN=0;
CREN=1;
}
if(RCIF) // a new byte is waiting
{
PORTB=(RCREG-0x30);
}
}
}
Common problems
- Garbage characters: The baud rate or the crystal frequency does not match. Check SPBRG, BRGH and the terminal settings.
- Nothing received: Check that TX and RX are crossed. The PIC TX pin goes to the RX of the other device, and the PIC RX goes to the TX of the other device. Make sure both devices share a common ground.
- Reception stops after a while: This is usually an overrun error. Clear it by toggling CREN as shown above.
- Nothing works on real hardware: Check the configuration bits, especially the oscillator type, and confirm that the chip is running at the clock speed you assumed.
Related tutorials
If you have no spare hardware port, or need more than one serial channel, a software UART can be built from ordinary I/O pins and timing delays. If you prefer a different compiler, see how to do UART in mikroC on the same chip. For a combined tutorial on UART and EEPROM, there is a separate part covering both.
The same compiler setup also applies to other peripherals. You can read analog values with the ADC and print them over the serial port, or practise the basics with digital output and input before adding serial communication.
see software UART at http://jimmyjosep.blogspot.in/2013/11/under-standing-uartsoft.html
FAQ
Which pins does the PIC16F877A use for UART?
TX is RC6 (pin 25) and RX is RC7 (pin 26). Once SPEN is set, the USART controls these pins.
What SPBRG value gives 9600 baud at 16 MHz?
With BRGH = 0, use SPBRG = 25, which gives about 9615 baud. With BRGH = 1, use SPBRG = 103.
Can I connect the PIC directly to a PC serial port?
No. RS-232 voltage levels can damage the PIC pins. Use a MAX232-type converter or a USB-to-TTL adapter.
Why does my UART stop receiving data?
The most likely cause is an overrun error (OERR) after bytes arrive faster than the program reads them. Clear it by setting CREN to 0 and then back to 1, and read RCREG promptly.