This code example demonstrates how to communicate as host and client using the Serial Peripheral Interface (SPI) protocol in Interrupt and Polling modes on a PIC18F47Q10 microcontroller with MPLAB® Code Configurator (MCC) Melody. The SPI Host demo writes data to a seven-segment display upon the press of a hardware button. The SPI Client demo exchanges data between two PIC18F47Q10 microcontrollers and displays the exchanged data on a serial terminal.
- MPLAB® X IDE v6.15 or newer
- MPLAB® XC8 Compiler v2.45 or newer
- MPLAB® Code Configurator (MCC) v5.4.1 or newer
- Download the ZIP file containing the source code and extract it.
- Connect the 7seg Click board to Curiosity Nano Base on mikroBUS™ port 1.
- Connect the Curiosity Nano board to the Curiosity Nano Base.
- Connect the Curiosity Nano board to the system.
- Open MPLAB X IDE.
- Click File>Open Project.
- Navigate to the extracted source code folder and select both projects with
spi-host
in the project names. - Click the Open Project button in the window.
- The following configuration was made in MCC Melody UI for SPI Host Driver polling project with Interrupt Driven toggled on for the interrupt project:
- The following pin configurations were made in MCC Melody UI for both SPI Host Driver projects:
- From the Projects window on MPLAB, right click the
pic18f47q10-spi-host-polling
project and select Make and Program Device to program the code to the device. - A dialog box will pop up to select the tool to be programmed. Select the connected tool from the drop-down box. Click OK.
- The seven-segment display will now show
--
once the programming is complete. - Press SW0 on the Curiosity Nano. The display will now show
00
. - Press SW0 again to increment the number showed on the seven-segment display.
- From the Projects window on MPLAB, right click the
pic18f47q10-spi-host-interrupt
project and select Make and Program Device to program the code to the device. - A dialog box will pop up to select the tool to be programmed. Select the connected tool from the drop-down box. Click OK.
- The seven-segment display will now show
--
once the programming is complete. - Press SW0 on the Curiosity Nano. The display will now show
00
. - Press SW0 again to increment the number showed on the seven-segment display.
-
Download the ZIP file containing the source code and extract it.
-
Connect the Serial Data Out (SDO), Serial Data In (SDI), Serial Clock (SCK), Chip Select 1 (CS1) and Ground (GND) pins of two Curiosity Nano boards to each other. The following pinouts are required for the PIC18F47Q10 demonstration:
Pin Function Hardware Pin SDO RC4 SDI RC5 SCK RC6 CS1 RD4 GND GND -
Open MPLAB X IDE.
-
Click File>Open Project.
-
Navigate to the extracted source code folder and select all three projects with
spi-client
in the project names. -
Connect one development board to the system. Make note of the serial number on the back of the development board.
-
From the Projects window on MPLAB, right click the
pic18f47q10-spi-client-tester
project and select Make and Program Device to program the code to the device. -
A dialog box will pop up to select the tool to be programmed. Select the connected tool from the drop-down box. Click OK.
-
After programming the device, the on-board LED0 will flash once every second indicating that it is working as expected. The setup for testing client projects is now complete. Disconnect the tester for now. The
spi-client-polling
andspi-client-interrupt
projects are preconfigured as mentioned in the next two points. -
The following configuration was made in MCC Melody UI for SPI Client Driver polling project with Interrupt Driven toggled on for the interrupt project:
-
The following pin configurations were made in MCC Melody UI for both SPI Client Driver projects:
- Open MPLAB Data Visualizer from the toolbar above. Alternatively, go to Window>Debugging>Data Visualizer.
- With the Data Visualizer open, connect the other development board to the system. Make note of the COM port assigned to the connected device. See example below:
- Hover the cursor over the COM port of the device just connected and select the option Display as Text in the Terminal.
- Now connect the tester development board used in Setup for Client Demo to the system. The client tester will start working automatically.
- From the Projects window on MPLAB, right click the
pic18f47q10-spi-client-polling
project and select Make and Program Device to program the code to the device. - A dialog box will pop up to select the tool to be programmed. From the drop-down box select a different tool from the one selected before in Setup for Client Demo. The serial number can be found on the back of the development board. Click OK.
- Once the programming is complete, the terminal window in Data Visualizer will display the results. If the data exchange happens as expected,
SUCCESS
will be displayed in the terminal. See the example below:
- Open MPLAB Data Visualizer from the toolbar above. Alternatively, go to Window>Debugging>Data Visualizer.
- With the Data Visualizer open, connect the other development board to the system. Make note of the COM port assigned to the connected device. See example below:
- Hover the cursor over the COM port of the device just connected and select the option Display as Text in the Terminal.
- Now connect the tester development board used in Setup for Client Demo to the system. The client tester will start working automatically.
- From the Projects window on MPLAB, right click the
pic18f47q10-spi-client-interrupt
project and select Make and Program Device to program the code to the device. - A dialog box will pop up to select the tool to be programmed. From the drop-down box select a different tool from the one selected before in Setup for Client Demo. The serial number can be found on the back of the development board. Click OK.
- Once the programming is complete, the terminal window in Data Visualizer will display the results. If the data exchange happens as expected,
SUCCESS
will be displayed in the terminal. See the example below:
The demo provides examples of both host and client communication using the SPI protocol in Interrupt and Polling modes of operation. The demo utilizes firmware code generated using MCC Melody.