Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Use Serial Communication in Microchip Environments

Serial communication is a fundamental aspect of embedded systems, allowing microcontrollers to communicate with other devices such as computers, sensors, and other microcontrollers. One common function used in many embedded systems, particularly in Arduino environments, is Serial.println. This function sends data over a serial port and appends a newline character at the end, making it useful for debugging and data logging.

However, in the context of microchip environments, such as those using Microchip's PIC microcontrollers, the Serial.println function is not directly applicable. Instead, Microchip environments typically use different libraries and functions to achieve similar serial communication. This article will guide you on how to set up and use serial communication in a Microchip environment, specifically using MPLAB X IDE and XC8 compiler.

Examples:

  1. Setting Up Serial Communication on a PIC Microcontroller:

    To begin, you need to configure the UART (Universal Asynchronous Receiver/Transmitter) module of the PIC microcontroller. Below is an example code for setting up UART on a PIC16F877A microcontroller.

    #include <xc.h>
    
    // Configuration bits
    #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
    #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
    #pragma config LVP = OFF        // Low-Voltage (single-supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    #pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
    #pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off)
    #pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
    
    #define _XTAL_FREQ 20000000     // Define the system clock frequency
    
    void UART_Init(void) {
       TRISC6 = 0; // TX Pin set as output
       TRISC7 = 1; // RX Pin set as input
       SPBRG = 31; // Baud rate 9600 for 20MHz clock
       TXEN = 1;   // Enable transmission
       SPEN = 1;   // Enable serial port pins
       CREN = 1;   // Enable reception
       TX9 = 0;    // 8-bit transmission
       RX9 = 0;    // 8-bit reception
    }
    
    void UART_Write(char data) {
       while (!TRMT); // Wait until the transmit buffer is empty
       TXREG = data;  // Transmit data
    }
    
    void UART_Write_Text(char* text) {
       while (*text) {
           UART_Write(*text++);
       }
    }
    
    void main(void) {
       UART_Init(); // Initialize UART
       while (1) {
           UART_Write_Text("Hello, World!\r\n"); // Send text
           __delay_ms(1000); // Wait for 1 second
       }
    }
  2. Reading Data from UART:

    To read data from the UART, you can use the following code snippet. This example reads a character from the UART and echoes it back.

    char UART_Read(void) {
       while (!RCIF); // Wait until data is received
       return RCREG;  // Return received data
    }
    
    void main(void) {
       char received;
       UART_Init(); // Initialize UART
       while (1) {
           received = UART_Read(); // Read data from UART
           UART_Write(received);   // Echo received data
       }
    }

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.