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

Sending Single Bytes Via UART on the PIC16F18855 - Part 2 (Slave)

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

In this example we will configure an express board to recieve characters over UART and turn on/off an LED respectively.

Please make sure you have completed Part 1 of this example.

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 get input from the serial interface, then send it right
        back. This is what that code looks like:

void main(void) {
    SYSTEM_Initialize();

    while (1)
    {
        unsigned char buf = EUSART_Read();
        if(buf == 0xFF) {
            IO_RA0_SetHigh();
        }
        else {
            IO_RA0_SetLow();
        }
    }
}

        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
        unsigned char buf = EUSART_Read();
            Get the next byte from the Receive register and save it to buf
        if(buf == 0xFF) {...} else {...}
            Check the value of the received UART byte. From the master we are only sending 0xFF or 0x00, so if it isn't 0xFF then
            the light should not be turned on.
        IO_RA0_Set...()
            Turn the LED on or off(High and Low respectively)

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

4. Wire the Devices.

    Connect 3 peices of wire to the devices such that the TX of one device is connected to the RX of the other, and vice-versa. The third
    piece of wire should connect the grounds of the devices together to prevent floating logic levels.
    See the following picture for how I connected my 2 Xpress Evaluation Boards:

    Note that in this photo I have not connected the grounds together. This is because the boards are grounded together via their USB
    cables to the hub they are both connected too.

5. Test the project

     Once the boards are programmed and wired, you can test the project. Press the S2 Button(not the RESET button) on the device
    programmed as master. This should light up the first LED on both boards. Fun Right?

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.