SPI communication between PICs

From Mech
Jump to navigationJump to search

Overview

SPI or Serial Peripheral Interface is a communication method that was once used to connect devices such as printers, cameras, scanners, etc. to a desktop computer. This function has largely been taken over by USB, but SPI can still be a useful communication tool for some applications. SPI runs using a master/slave set-up and can run in full duplex mode, meaning that signals can be transmitted between the master and the slave simultaneously. There is no standard communication protocol for SPI.

SPI is still used to control some peripheral devices and has some advantages over I2C (another type of serial data communication). SPI can communicate at much higher data rates than I2C. Furthermore, when multiple slaves are present, SPI requires no addressing to differentiate between these slaves. Compared to parallel buses, SPI has the additional benefit of requiring only simple wiring.


Peripheral devices that still use SPI:

• Converters (ADC and DAC)

• Memories (EEPROM and FLASH)

• Real Time Clocks (RTC)

• Sensors (temperature, pressure, etc.)

• Others (signal mixer, potentiometer, LCD controller, UART, CAN controller, USB controller, amplifier)


Basic Operation

SPI requires four lines, and is therefore often termed the “four wire” serial bus. These four lines are described in the table below.

Line Name Description
SCLK Serial Clock Output from master
MOSI/SIMO Master Output, Slave Input Output from master
MISO/SOMI Master Input, Slave Output Output from slave
SS Slave Select Output from master (active low)


The master, as its name suggests, controls all communication. By controlling the clock, the master decides when data is sent and received. Within each clock cycle a full duplex communication is carried out; each side sends and receives one bit of information. Because there is no standard communication protocol, the master can either send data or both send and receive data, depending on the needs of the application. Likewise, the slave can either receive data or both receive and send data back to the master.

Using the “Slave Select” line, the master chooses which slave with which to communicate. Note that more than one slave may be selected, simply by applying a logic low to the desired SS lines, as illustrated in the schematic diagram shown above. If a given slave is not selected (its SS is high) it disregards signals sent by the master.


References

SPI Background(www.totalphase.com)

SPI Wikipedia Article (www.wikipedia.org)

More Information (www.mct.net)

Circuit

Master connected to three slaves:

Spi-diagram.png

Here each slave must be enabled through the slave select pin in order to communicate with the Master.


One PIC master and one PIC slave:

Spi.jpg

The master, on the left, and the slave, on the right, are connected as shown above. Be sure you also connect grounds!


Code

SPI communication on the PIC is accomplished via the functions spi_read() and spi_write(), which read and write one byte at a time over SPI. In the example below, the master reads an 8-bit value from the analog-to-digital converter and sends it to the slave via SPI. The slave reads the value and displays it on an LCD display.

Note: The code given below does not work perfectly: the slave failed to received 10-20% of the messages sent by the master. In this example that failure rate was not critical, as the slave just missed an update of the ADC reading when it missed a message. If more accurate data transmission is required, the failure rate inherent in this system may be unacceptable.


SPI Mode Numbers

There are four SPI "modes" which describe the relationship between the phase of the clock line and the phase of the MISO/MOSI lines. In order to successfully communicate using SPI, the master and slave must operate using the same mode. Wikipedia has a good explanation of these mode numbers.


The Slave Select Line

In addition to being used to select which slave is active during a communication, the slave select (SS) line, is vital for synchronization. A slave's SS line should be high except when the master is talking to it. When the SS line is pulled low, the slave knows that a new communication is starting.

If a slave fails to receive a message properly, it will be reset when the SS line goes high at the end of the message and the slave will be prepared for a new message when the SS line goes low again. If the SS line is not used (e.g. by disabling it or running it directly to ground) there is a risk of the slave becoming out of sync with the master; if the slave misses a bit, it will always be one bit off in the future. Effective use of the SS line allows the slave and master to realign themselves at the beginning of each communication, in case of a transmission error.


Master Code:

#include <18f4520.h>
#fuses EC,NOLVP,NOWDT,NOPROTECT
#device ADC=8
#use delay(clock=40000000)

//These define the different SPI modes in terms of constants the compiler knows about
//NOTE: our PICs only seemed to work in modes 1 and 3, though they are supposed to work with any modes
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H) 

#define SS_PIN PIN_D0         //this can be any output pin on the master

void main()
{
   int val;

   //Set up the ADC to read from A0
   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);

   //The next statement sets up the SPI hardware, with this PIC as a master using mode 1
   //SPI_CLK_DIV_64 sets the speed of the SPI clock pulses--this is the slowest speed
   setup_spi(SPI_MASTER | SPI_MODE_1 | SPI_CLK_DIV_64); 
   
   while(true) {
       val = read_adc();      //read the value to be sent
       
       output_low(SS_PIN);    //pull the slave select line low to select the slave
       delay_us(10);          //give the slave time to notice this (may be unnecessary)
       
       spi_write(val);        //send the value
       
       delay_us(10);          //(may be unnecessary)
       output_high(SS_PIN);   //deselect the slave. 
       
       delay_ms(10);
   }
}


Slave Code:

#include <18f4520.h>
#fuses EC,NOLVP,NOWDT,NOPROTECT
#use delay(clock=40000000)

#include <LCD.C>

#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)

void main()
{
   setup_spi(SPI_SLAVE | SPI_MODE_1); //set up SPI hardware as a slave in mode 1
   
   lcd_init();
   
   while(true) {
       val = spi_read(0); //spi_read must be passed an argument. The argument value is sent
                          //back to the master whenever the master sends us a message again. 
                          //This allows two-way communication, but here the master ignores
                          //whatever the slave sends back, so just send a 0.

       //display the value read:
       lcd_gotoxy(1, 1);
       printf(lcd_putc, "Pot at: %u   ", val);
   }
}


Alternative Slave Code, Using Interrupts:

#include <18f4520.h>
#fuses EC,NOLVP,NOWDT,NOPROTECT
#use delay(clock=40000000)

#include <LCD.C>

#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)

#int_ssp //this interrupt will occur whenever we receive an SPI message (or an I2C message, actually)
void message_receieved() {
    unsigned int val;

    val = spi_read(); //When you don't pass an argument, spi_read just returns the most
                      //recently received SPI message. We can do this here because we know
                      //that we just received a new message.

    //display the value:
    lcd_gotoxy(1, 1);
    printf(lcd_putc, "Pot at: %u   ", val);
}

void main()
{
   setup_spi(SPI_SLAVE | SPI_MODE_1); //setup the PIC as a slave in mode 1
   enable_interrupts(INT_SSP); //enable interrupts on SPI messages
   enable_interrupts(GLOBAL);
   
   lcd_init();
   
   while(true); //sit and wait for a message
}


Two-way communication [This code has not been successfully tested!]

Though not shown above, it is possible to use SPI for two-way communication between master and slave. If the slave calls

message_from_master = spi_read(123);

It will wait for the master to send some clock pulses; when the clock pulses come the slave will simultaneously read the message being sent by the master and send its own message (the number 123). The master, for its part, will call

message_from_slave = spi_read(22);

This will cause the master to send the number 22 to the slave and simultaneously read the message sent back by the slave (in this case the number 123). So the master and slave have exchanged one byte of information. If you are the master and you just want to get a byte from the slave without sending one, just send a junk value, which the slave will discard:

message_from_slave = spi_read(0);


Useful resources

The CCS user manual

CCS forum

Wikipedia article