Microchip | The Embedded Control Solutions Company Register Login
Browse All
Microchip Technology
 367 Code Snippets

Sending Single Bytes Via UART - Part 1 (Master)

Microchip Technology

Check out this project's MPLAB Xpress Webpage to view the complete code example along with a detailed step by step write-up and description, or visit the PIC16F18855 Device Product Page for more information.

About this Code Example

Preface

It is recommended that you view the Hello World Example to familiarize yourself with the Xpress IDE before continuing.

In this example we will create a simple program to send single bytes from one board to another via UART.

Background

    UART, or Universal Asynchronous Receiver-Transmitter, is one of the simplest and most common serial interfaces in use today. You will hard-pressed to find a microprocessor that doesn't support a form of UART. UART can also be relatively easily replicated in most microprocessors via bit-banging.

    UART takes the form of 2 wires connected between 2 devices. these wires are unidirectional in transmission as for each each device, one wire is for Tranmission, and the other is for Reception. These roles are more commonly known as RX and TX. The RX of one device is connected to the TX of the other and vice-versa. This can be shown by the picture below:

    UART works by sending single bytes though the TX port. For each bit in a byte, the transmission line is pulled high to indicate a '1' and low to inidicate a '0'. These bits are sandwiched by a Start bit and End bit to signal the start and end of a transmission to the receiver. These bits are sent over the line at a transmission rate that is preconfigured on both devices called the baud rate. The baud rate can be configured to any value but is usually set to one of a number of standard speeds, the most common being: 4800, 9600, 19200, 57600, and 115200.

Directions

1. Start by creating a new Project

    a. Go to File > New Project
    b. Select Microchip Embedded > Standalone Project
    c. Enter the model number of your device.
        In this case we are using the PIC16F18855
    d. Name the project

2. Open the MPLAB-Xpress Code Configurator

    a. Add peripherals to the project.
        Add the EUSART peripheral to the project

    b. Modify the EUSART Peripheral.
        Select EUSART from the peripherals list.
         - Check "Enable Transmit"
         - Check "Enable Receive"

    c. Configure the pins used on the device.
        In the EUSART Module, Select pin Port-C6 for RX and Port-C5 for TX.
        You will also configure pin Port-A5 as a GPIO input and Port-A0 as a GPIO ouput. These are the button and first LED on the
        device, respectively.

    d. Generate the Project files.
        Click the Generate button next to the project Resources heading to send the configured code back to MPLAB Xpress

3. Configure the software.

    a. Write the main function
        The main function in this example is rather simple. All we have to do is read the state of the button and send either 0x00 or
        0xFF to the slave.

void main(void) {
    SYSTEM_Initialize();
    while (1)
    {
        if(!IO_RA5_GetValue()) {
            EUSART_Write(255);
            __delay_ms(1);
        } else {
            EUSART_Write(0);
            __delay_ms(1);
        }
    }
}

        Note that a couple lines in the main.c file in the project have been ommitted here as they do not contribute core functionality.

        A Line-by-Line explanation follows:
        SYSTEM_Initialize();
            This call is to initilize all the peripheral interfaces generated by MCC
        while (1) {...}");
            We don't ever want our main function to end so we run our code in an infinite loop
        if(!IO_RA5_GetValue()) {...} else {...}
            Get the current state of the button on the board. this function returns 1 if the button is not pressed so we invert our logic with
            !. If the button is pushed, we execute the first block of code, if it is not, we execute the second.
        EUSART_Write(...)
            This function will write an 8-bit integer to the Serial Peripheral to be sent via UART to the other device.
        __delay_ms(1);
            We should limit the number of bytes we send just in case the other side may take a while. This will also make them easier to
            read

    b. Build the project
        Now that the software is written you can build the code and program it to the board.

3. Configure the Slave Device.

    See here for Part 2 of this tutorial.

Categories

Release History

  • Dec 4, 2018

    Version: 1.0.0

    Initial Release

Download Contact the Developer

*Required Fields


  • Current Version:1.0.0
  • Created:Dec 4, 2018
  • Updated:Dec 4, 2018
  • Downloads:361
  • MPLAB Version:MPLABX IDE
  • C Compiler:XC8 1.45
  • Development Tools:Xpress Board
  • Supported Devices:PIC16F18855

Corporate News

Resources

Stay Connected

Stay up to date with the latest news from Microchip Technology.