SPI Motor Controller (PIC)

Introduction

This is a basic tutorial for the steps required to get SPI working on the PIC18F47Q10 Curiosity Nano

Resources

Instructions

  1. Prepare your IC

    1. Surface-Mount Solder: If you have not yet surface-mount soldered your IC, you will need a DIP adapter board and 12 male header pins

      ALERT: If you are using the DIP adapter boards supplied in Peralta 109, you should not drive a high current because the ground plane is not connected to a heat-sink pad.

    2. Test: ensure connectivity to all pins after soldering with no connectivity between neighboring pins

    3. Mount your DIP adapter board on a breadboard

      1. Connect ALL pins according to the motor controller datasheet. For the purposes of this assignment, use two different-colored LEDs mounted in opposite directions between OUT1 and OUT2 (with an appropriate current-limiting resistor) to emulate a bidirectional motor.

        Note: All pins must be connected to SOMETHING in order for the IC to work. The datasheet explains connection requirements in various sections.

  2. Set up MCC

    1. System Module: this has been tested as working with:
      • Clock Source: HFINTOSC @64 MHz
      • Clock Divider: 1
    2. Add EUSARTX for communicating to your PC
      • Select your desired baud rate
      • ensure “Redirect STDIO to USART” is checked
    3. Add MSSPX. Ensure MSSPX does not conflict with existing I2C devices
      • Serial Protocol: SPI
      • Mode: Master
      • SPI Mode: (please determine from datasheet)
      • Input Data Sampled At: (please determine datasheet)
      • Clock Source Selection: FOSC/64
    4. Pin Manager
      1. Set up the MSSPX Pins according to the primary pins given by the PIC18F47Q10 Curiosity Nano Hardware Manual

        Note: Chip Select is not a part of the MSSP system and should be set as a GPIO Output Pin

      2. Set the EUSARTX Pins according to the dedicated pins given in the PIC18F47Q10 Curiosity Nano Hardware Manual

    5. Pin Module
      1. Deselect “Analog” on all the output pins.
      2. Give your chip select pin a custom name
    6. Generate your MCC Configuration
  3. Code

    1. Open the SPIX.h file generated by MCC
      1. Identify the enumerated “spix_modes_t typedef that sets the default operation of the subsystem
      2. Identify the functions that
        • Initialize the subsystem
        • Open the subsystem(with a configuration)
        • Exchange multiple bytes of data (not read, not write, read AND write).
    2. Open main.c and do the following things:
      1. Create an array of X bytes for data exchange over SPI
      2. Ensure your SYSTEM_Initialize() function initializes SPIX.
      3. Before the while loop, open SPIX using one of the functions identified above
      4. Within the while loop,
        1. Set the N bytes to be written to the motor controller according to the data sheet
        2. Activate the chip-select pin for the motor driver
        3. delay X ms (see the data sheet)
        4. exchange the data
        5. delay Y ms (see the data sheet)
        6. read the N bytes that were received in the exchange and save to a temporary variable.
        7. print the formatted temporary variable to EUSARTX for confirmation / debugging.
        8. repeat the process, updating your command data if necessary