Understanding UART Initialization for PIC16F877A

 

Understanding UART Initialization for PIC16F877A

In this blog post, we will break down the UART (Universal Asynchronous Receiver Transmitter) initialization code for the PIC16F877A microcontroller. UART is a widely used protocol for serial communication, enabling devices to exchange data using just two lines: TX (Transmit) and RX (Receive).


Code Explanation

void UART_Init(void) {
    SPBRG = 31;               // Baud rate for 9600 at 20 MHz clock
    TXSTAbits.BRGH = 0;       // Low speed
    TXSTAbits.SYNC = 0;       // Asynchronous mode
    RCSTAbits.SPEN = 1;       // Enable serial port
    TXSTAbits.TXEN = 1;       // Enable transmission
    RCSTAbits.CREN = 1;       // Enable continuous reception
    PIE1bits.RCIE = 0;        // Disable UART RX interrupt
}

This code initializes the UART module of the PIC16F877A to communicate at a 9600 baud rate, which is a common speed for serial devices like GPS modules, GSM modules, or debugging terminals.


Step-by-Step Notes

  1. SPBRG = 31;

    • This sets the baud rate.
    • Here, we configure the baud rate for 9600 bps, which is suitable for many peripheral devices.
  2. TXSTAbits.BRGH = 0;

    • This sets low-speed mode for the baud rate generator.
    • Low-speed mode uses the formula above; high-speed mode would use a divisor of 16 instead of 64.
    • Since SPBRG=31SPBRG = 31 is calculated for low-speed mode, BRGH is set to 0.
  3. TXSTAbits.SYNC = 0;

    • Configures the UART module in asynchronous mode.
    • In asynchronous mode, no external clock signal is needed, and communication is based solely on the configured baud rate.
  4. RCSTAbits.SPEN = 1;

    • Enables the serial port (TX and RX pins become active).
    • This allows the microcontroller to send and receive data through the UART module.
  5. TXSTAbits.TXEN = 1;

    • Enables the transmitter (TX pin).
    • Data can now be sent from the PIC microcontroller to an external device.
  6. RCSTAbits.CREN = 1;

    • Enables continuous reception of data on the RX pin.
    • This means the UART module will always be ready to receive incoming data.
  7. PIE1bits.RCIE = 0;

    • Disables the UART Receive Interrupt.
    • This means data reception must be handled manually by polling the receive flag (RCIF) in your main code.

Why This Initialization?

  • 9600 Baud Rate: A commonly supported speed by devices like GPS modules, GSM modems, and PCs for serial communication.
  • Asynchronous Mode: Eliminates the need for an external clock, simplifying hardware connections.
  • Manual Data Handling: By disabling interrupts, the system uses polling, which is easier for simpler applications.

How to Use This Code?

  1. Call UART_Init() in the main() function to initialize the UART module.
  2. Use TXREG to send data and check RCREG for received data:
    void UART_Write(char data) {
        while (!TXSTAbits.TRMT);  // Wait until the transmit buffer is empty
        TXREG = data;            // Load data into the transmit register
    }
    
    char UART_Read(void) {
        while (!PIR1bits.RCIF);  // Wait until data is received
        return RCREG;            // Return the received character
    }
    

Applications

  • GPS Data Retrieval: Read NMEA sentences from GPS modules via UART.
  • GSM Communication: Send AT commands to GSM modules.
  • PC Communication: Send debugging messages to a terminal on a PC.

Key Points to Remember

  1. The baud rate must match between the microcontroller and the connected device.
  2. Ensure UART pins (RC6 for TX, RC7 for RX) are properly connected to the external device.
  3. If using interrupts, set PIE1bits.RCIE = 1 and handle the receive interrupt in an ISR.

By following these steps, you can easily set up and use UART communication on the PIC16F877A for a variety of applications!

Comments

Popular posts from this blog

PIC16F877A - UART Registers

Introduction to SPI Communication on the PIC16F877A

Introduction to UART Communication