SPI Between Two PIC18F27J13 Microcontrollers

Master and slave SPI code for the PIC18F27J13, written for the NIBP blood pressure device and tested in Proteus.
Healthcare
Signal Processing
Author
Published

December 24, 2020

The non-invasive blood pressure (NIBP) device I worked on at CMC Vellore split its work across two PIC18F27J13 microcontrollers, and they needed a simple, fast way to talk to each other. SPI was the natural choice: it’s built into the chip, it’s quick, and it only needs a few wires.

This post covers the bare-minimum master and slave code I wrote to get the link working. The blood pressure algorithm itself is in a separate post.

Code and Proteus project: Zilch123/SPI-PIC18f27j13

SPI in one paragraph

SPI has one master and one or more slaves. The master drives the clock (SCK), and on each clock pulse one bit goes out on its SDO line into the slave’s SDI line. The slave select (SS) line tells a slave that it’s being talked to. There’s no addressing and no acknowledgement, which keeps it simple and fast.

Line Master (PIC 1) Slave (PIC 2)
SCK RC3, output RC3, input
Data RC5 (SDO), output RC4 (SDI), input
SS not used here RA5, tied low

Proteus simulation: the master (left) sends a byte every second, and the slave (right) toggles the LED

The master

On the PIC18, SPI runs through the MSSP module. The SSPM bits pick the mode, and all zeros means SPI master with the clock at Fosc/4:

void SPI_init()
{
// this setting selects master mode with frequency fosc/4
SSPCON1bits.SSPM0 = 0;
SSPCON1bits.SSPM1 = 0;
SSPCON1bits.SSPM2 = 0;
SSPCON1bits.SSPM3 = 0;
// Enable SPI Port
SSPCON1bits.SSPEN = 1;
// Configure The Clock Polarity & Phase
SSPCON1bits.CKP = 0;
SSPSTATbits.CKE = 0;
SSPSTATbits.SMP = 0;
// Set SPI pins as digital I/O
TRISC5 = 0; // SDO -> Output
TRISC4 = 1; // SDI -> Input
TRISC3 = 0; // SCK -> Output
}

CKP and CKE set the clock polarity and phase. They have to match on both sides, or the slave reads bits at the wrong moment and gets garbage.

Sending a byte is just writing it to the buffer register. The hardware clocks it out by itself:

void SPI_write(char data)
{
SSPBUF = data;
}

The test loop sends two different bytes, one second apart:

while(1)
{
delay(1000);
SPI_write(0x01);
delay(1000);
SPI_write(0x02);
}

The slave

The slave uses SSPM = 0100, which is SPI slave mode with the SS pin enabled. SCK and SS become inputs, because the master drives them:

SSPCON1bits.SSPM0 = 0;
SSPCON1bits.SSPM1 = 0;
SSPCON1bits.SSPM2 = 1;
SSPCON1bits.SSPM3 = 0;
SSPCON1bits.SSPEN = 1;

TRISC5 = 0; // SDO -> Output
TRISC4 = 1; // SDI -> Input
TRISC3 = 1; // SCK -> Input
TRISA5 = 1; // SS -> Input

Rather than checking the buffer in a loop, the slave uses an interrupt. When a full byte arrives, the MSSP sets its interrupt flag, and the interrupt routine copies the byte out:

INTCONbits.PEIE = 1; // Peripheral Interrupt Enable bit
INTCONbits.GIE = 1;  // Global Interrupt Enable bit
PIE1bits.SSP1IE = 1; // MSSP1 interrupt enable

void __interrupt() ISR(void)
{
if(SSP1IF)  // check if interrupt occurs due to SPI module
{
    data = SSPBUF; // copy data from receive buffer
    SSP1IF = 0;    // reset the interrupt flag
}
}

The main loop then acts on whatever byte came in last, turning an LED on for 0x01 and off for 0x02:

while(1)
{
    if (data==0x01)
        RB0=1;   // LED on
    else if (data==0x02)
        RB0=0;   // LED off
}

Testing in Proteus

Before touching hardware, I tested both chips together in Proteus. It runs the compiled firmware on simulated PICs, so you can wire them up, watch the LED blink once a second, and check the SPI lines, all without a soldering iron. The Proteus project is in the repo.

Things I’d add for real use

  • Make the shared variable volatile. data is changed inside the interrupt, and without volatile the compiler may assume it never changes in the main loop.
  • Check the buffer-full flag. Before writing the next byte, the master should wait for SSPSTATbits.BF so it doesn’t overwrite a transfer in progress.
  • Drive SS from the master. In the simulation SS is tied low, which works for one slave. With more slaves, or to resync after noise, the master should pull SS low only while it’s sending.
  • Use a small packet format. Single bytes are fine for a test. For real readings, a start byte, the data and a checksum make the link much more robust, the same idea as the encoder packets in the rehab game controller.