• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

What are TXREG and RCREG in UART code



I have been gone through page https://exploreembedded.com/wiki/Serial_Communication_with_PIC16F877A

sample code


#define SBIT_TXEN 5
#define SBIT_SPEN 7
#define SBIT_CREN 4

void UART_Init(int baudRate)
TRISC=0x80; // Configure Rx pin as input and Tx as output
TXSTA=(1<<SBIT_TXEN); // Asynchronous mode, 8-bit data & enable transmitter
RCSTA=(1<<SBIT_SPEN) | (1<<SBIT_CREN); // Enable Serial Port and 8-bit continuous receive
SPBRG = (20000000UL/(long)(64UL*baudRate))-1; // baud rate @20Mhz Clock

void UART_TxChar(char ch)
while(TXIF==0); // Wait till the transmitter register becomes empty
TXIF=0; // Clear transmitter flag
TXREG=ch; // load the char to be transmitted into transmit reg

char UART_RxChar()
while(RCIF==0); // Wait till the data is received
RCIF=0; // Clear receiver flag
return(RCREG); // Return the received data to calling function

int main()
char i,a[]={"Welcome to Pic Serial Comm, Type the char to be echoed: "};
char ch;

UART_Init(9600); //Initialize the UART module with 9600 baud rate
UART_TxChar(a[i]); // Transmit predefined string

ch = UART_RxChar(); // Receive a char from serial port
UART_TxChar(ch); // Transmit the received char
I wonder for TXREG and RCREG, what is that in code. they haven't defined or declare anywhere. Is it variable ?


Look in the data sheet or follow the link that you posted ... it tells you on the first page
I know they both are the name of register but I am asking in the context of code. They have not been defined or declared any where in the code. if we want store the value in c programming then we have to declare name of variable and type of variable.

Nigel Goodwin

Super Moderator
Most Helpful Member
I know they both are the name of register but I am asking in the context of code. They have not been defined or declared any where in the code. if we want store the value in c programming then we have to declare name of variable and type of variable.
They are defined in the code:


What did you think the header file was for?. You should also be aware that in MPLAB you don't use specific device headers, they are included automatically from the processor setting in MPLAB.


Well-Known Member
TXREG and RCREG are registers, but they are special function registers. The actual addresses of TXREG and RCREG are in the .h file, and for the 16F877A they are 0x19 and 0x1A in bank 0. Broadly, special function registers do something physical as well as, or instead of, storing a number. The ports are special function registers, so when you write:-
PORTA = 0;
all the pins that make up port A, that are outputs, will go to a low voltage. Similarly PORTA can be read by the code, and it's value will change depending on the logic levels applied to the pins.

TXREG and RCREG are the special function registers that are used for sending and receiving data on the USART. You cannot store data in them in the normal way.

Where the code has:-
the value of ch is written into TXREG, and the action of writing causes one byte to be transmitted by the USART, starting immediately. The TX pin, pin 25 on a DIP40 pic16F877A, will be high when nothing is being transmitted. It will go low for 1 bit period to send the start bit, then it will send the 8 data bits, LSB first, by being low for 1 bit period if the bit is 0, and high for one bit period if the bit is 1. At the end of the 8 bits, there is a stop bit, which is high for 1 bit period and then the USART is ready to transmit another byte. (The stop bit is high. At the end of the stop bit the TX pin remains high. The end of the stop bit is just the point in time when another byte can be transmitted.

If the program includes:-
some code that waits for 10 bit periods or more
then the value of ch will be sent twice.

RCREG is the receiving register.
When data arrives on the RX pin, pin 26 of the DIP40 pic16F877A, in the same format that the USART uses to transmit, when the whole byte has finished, the value of the byte becomes available to be read on RCREG. The program has to read the RCIF bit to see if data is available. When data is available, RCREG can be read.

As with writing to TXREG, reading from RCREG causes something to happen. Reading RCREG causes the USART to consider the register as empty, so that RCIF is cleared by the USART, and the USART can put more data into RCREG if more arrives. Note that empty does not mean equal to 0x00. Any value from 0x00 to 0xFF can arrive at the RX pin and the USART will put the value into RCREG, which makes it full, and RCIF is 1. Reading the register makes the register empty and RCIF becomes 0.

If too much data arrives and the program never reads it, the USART turns on the overrun flag.

The code sample you have has an error, but not one that matters.
is incorrect, because the RCIF flag cannot be written by the program in a pic16F877A. It can only be read by the program. Trying to write it will do nothing.

Jon Wilder

Active Member
Nowadays with the new XC compilers, you simply include xc.h with #include <xc.h>. That file includes the correct processor specific header file into your code based on the processor type set in the MPLAB project properties.

All SFR's are defined within the Microchip supplied header files. Most compilers have header files for every processor they support, which makes coding using SFR names loads easier.

TXREG and RCREG are labels defined in the header file that link to the physical address of the TXREG and RCREG registers. The data sheet lists the physical address of all SFR registers.

TXREG is the UART transmit buffer. Writing a value to it triggers the UART hardware to begin the serial transmit process. The transmit interrupt flag bit TXIF in PIR1 goes low when there is a byte waiting in the TXREG buffer, then will go high once the byte is transferred into the transmit shift register (TSR).

RCREG is the UART receive buffer. The UART hardware stores received bytes in this register, then sets the RCIF interrupt flag bit in the PIR1 register.

When this flag bit goes high, reading RCREG will get you your received byte. The RCIF flag will go high each time a byte is transferred from the receive shift register (RSR) to RCREG, then will self clear once RCREG is read.

RCREG is a two level FIFO buffer (First In First Out). The UART can receive and hold 2 bytes in RCREG while shifting a 3rd byte into the receive shift register. If RCREG is not read prior to the completion of shifting in the 3rd byte, this triggers an overrun condition, in which the overrun flag bit OERR in the RCSTA register will be set. You will be required to clear the CREN bit in RCSTA, read RCREG twice to clear the buffer, then reset the CREN bit in RCSTA in order to clear the overrun condition.

If you're polling the RCIF flag bit or you have receive interrupts enabled, you should be reading that register long before running into an overrun condition.

Latest threads

EE World Online Articles