PIC16F-4-UART-Data-Transmission&Reception

by Marwen Maghrebi
// Function to check if UART transmitter buffer is empty
uint8_t UART_TX_Empty()
{
    // Check if the output (transmitter) buffer is empty
    return TRMT;
}

// Function to transmit a single character over UART
void UART_Write(uint8_t data)
{
    // Wait until the transmitter buffer is empty
    while (!UART_TX_Empty());
    
    // Write data to the transmitter buffer
    TXREG = data;
}

// Function to transmit a string over UART
void UART_Write_Text(const char* text)
{
    while (*text != '\0') {
        UART_Write(*text++);
    }
}

// Function to receive a single character over UART
uint8_t UART_Read(void)
{
    // Wait until data is received
    while (!RCIF);
    
    // Read and return the received data
    return RCREG;
}

// Function to receive a string over UART until Enter key is pressed
void UART_Read_Text(char* buffer, uint8_t max_length)
{
    uint8_t i = 0;
    char received_char;
    
    // Receive characters until Enter key is pressed or buffer is full
    do {
        received_char = UART_Read();
        if (received_char != '\r' && received_char != '\n') {
            buffer[i++] = received_char;
        }
    } while (received_char != '\r' && received_char != '\n' && i < max_length - 1);
    
    // Null-terminate the string
    buffer[i] = '\0';
}
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00