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

Sending Buffers 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 2 Xpress boards to send and receive, respectively, buffers of characters via SPI

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. Configure the hardware

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

    b. Modify the MSSP1 Peripheral.
        Select MSSP1 from the peripherals list.
         - Change the "Mode" of the peripheral to "SPI Slave"

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

    c. Configure the pins used on the device.
        In the MSSP1 Module, Configure the following pins:
         - Port-B3 for SCK1
         - Port-B4 for SDO1
         - Port-B5 for SDI1
         - Port-C3 for SS1
        The SS pin is not used but must still be configured.
        In the EUSART Module, Configure the following pins:
         - Port-C0 for TX
         - Port-C1 for RX

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

    a. Write the main function
        Compared to the Master Device, the main function for the slave is rather simple, we just get Input from SPI
        This is what that code looks like:

void main(void) {
    SYSTEM_Initialize();

    while (1)
    {
        char buf[16] = { 0 };
        SPI1_Exchange8bitBuffer(NULL, 16, buf);
        printf("Recieved \"%s\"\r\n", buf);
    }
}

        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
        char buf[16] = { 0 };
            Allocate a buffer for the text from the master device.
        SPI1_Exchange8bitBuffer(NULL, 16, buf);
            Receive a buffer of length 16 from the SPI bus. This will happen at a rate controlled by the master.
        printf("Recieved \"%s\"\r\n", buf);
            Print the string we received to the serial console.

    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 them. For this project, You'll need to connect to each board with a serial console. For more details on how to do this, see the Interfacing with a Computer Via UART Example

Once that is running, you can send some text to the master and have it printed back out from the slave.

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:541
  • 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.