PIC16F877A - UART Transmitter (with code)

  CODE TO TRANSMIT STRING (Pic16f877A) -Xc8 Compiler


#include <xc.h>

#define _XTAL_FREQ 8000000  // Define the oscillator frequency (e.g., 8 MHz)

void main()

{

    TRISC6 = 0;  // TX pin (RC6) set as output

    TRISC7 = 1;  // RX pin (RC7) set as input

    // UART initialization

    SPBRG = 51;             // Baud rate set to 9600 for 8 MHz

    TXSTAbits.BRGH = 1;     // High Baud Rate Select bit

    TXSTAbits.TXEN = 1;     // Enable transmitter

    RCSTAbits.SPEN = 1;     // Enable UART (serial port)

    // Transmit the text repeatedly

    while (1)

    {

        const char* message = "Hello, UART!\r\n";  // Message to transmit

        const char* ptr = message;  // Pointer to the message


        while (*ptr != '\0')  // Transmit characters until null terminator

        {

            while (!TXSTAbits.TRMT);  // Wait until the transmit buffer is empty

            TXREG = *ptr;             // Load the character into the transmit register

            ptr++;                    // Move to the next character

        }

        __delay_ms(1000);  // Wait for 1 second before sending the message again

    }

}


Comments

Popular posts from this blog

PIC16F877A - UART Registers

Introduction to SPI Communication on the PIC16F877A

Introduction to UART Communication