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

Sending Single Bytes via SPI 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 receive characters via SPI 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 MSSP1 peripheral to the project

    b. Modify the MSSP1 Peripheral.
        Select MSSP1 from the peripherals list.
         - Change the "Mode" of the peripheral to "SPI Slave"
        You may leave "Slave Select" here unchecked as we only have 1 slave on the bus and don't need to select it before using it.

    c. Configure the pins used on the device.
        In the MSSP1 Module, Configure the following pins:
         - Port-B3 for SCK1
         - Port-B4 for SDI1
         - Port-B5 for SDO1
         - Port-C0 for SS1
        The SS pin is not used but must still be configured.
        You will also configure pin Port-A5 as a GPIO input and Port-A0 as a GPIO output. 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 set an LED
        accordingly.
        This is what that code looks like:

void main(void) {
    SYSTEM_Initialize();

    while (1)
    {
        unsigned char buf = SPI1_Exchange8bit();
        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 omitted here as they do not contribute core functionality.

        A Line-by-Line explanation follows:
        SYSTEM_Initialize();
            This call is to initialize 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 = SPI1_Exchange8bit();
            Get the next byte from the SPI Buffer 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 4 pieces of wire to the devices as follows:
     - SCK to SCK
     - MISO to MISO
     - MOSI to MOSI
    Make sure you 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:334
  • 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.