1. Introduction

SPI is one of the most used serial interfaces on PCB level. This tutorial explains how to use SPI to read or write data. For SPI the SPI library is required. It can be downloaded from our repository. The library provides commands for communicating over SPI.

2. Includes

A couple of includes are required, the libraries for the microcontroller itself and the SPI library. The gpio library and the timer library are needed to.

#include "LPC17xx.h"
#include "lpc_types.h"
#include "ssp.h"
#include "GPIO.h"
#include "timer.h"

3. Initializing

SPI uses 4 IO pins, 1 clock, 1 MOSI, data from master to slave, 1 MISO, data from slave to master and one Chip select pin. Every SPI IC has a Chip Select pin, when the pin is low that IC will be selected to communicate with. Every SPI IC that has a high value on the Chip Select pin will ignore all signals on their data pins. 1 normal IO pin is used for the Chip Select, the clock; MOSI and MISO are SPI pins. Any IO pin can be used for Chip Select, For the tutorial pin 0.0 is used.
The timer has to be initialized to use it for delays and the GPIO pin for Chip Select has to be initialized as output and made high, the code for that is:

     init_timer(0, 1000);
     GPIOSetDir(0, 0, 1);
     GPIOSetValue(0, 0, 1);
There are 2 SPI channels, SSP0 and SSP1. SSP0 uses pin 0.15 for the clock; pin 0.17 for MISO and pin 0.18 for MOSI. SSP1 uses pin 0.7 for the clock; pin 0.8 for MISO and pin 0.9 for MOSI. To initialize a SPI port the command SSP*channelno*init(); is used. To initialize SPI channel 1 the command is:

Some variables are also needed. SPI needs 2 array buffers, one for data that has to be send trough the SPI slave and one for data received from the SPI slave. Also its recommended to declare a variable with the value of the SPI channel that is used:

     uint8_t src_addr[16]; //16 byte Write buffer
     uint8_t dest_addr[16]; //16 byte Read buffer
     uint8_t portnum = 1;

4. Write data to SPI

First the Chip Select has to be made low with the GPIOSetValue command: GPIOSetValue(0, 0, 0;)
The data that has to be written should be placed in the src_addr array that was declared earlier. To write 5 bytes of data (0x48, 0x65, 0x6c, 0x6c, 0x6f the ascii code for Hello) place the hex data in the buffer array:

          src_addr[0] = 0x48;
          src_addr[1] = 0x65;
          src_addr[2] = 0x6c;
          src_addr[3] = 0x6c;
          src_addr[4] = 0x6f;

When the data is stored in the array all that is left is start the SPI data transmission with the SSPSend( portnum, (uint8_t *)src_addr, datanumbers); portnum is the used SPI port, (uint8_t *)src_addr is the send buffer, datanumbers is the amount of databytes to be send. The command needed to send the 5 databytes declared above is:
          SSPSend( portnum, (uint8_t *)src_addr, 5 );
After the data is send the Chip Select pin needs to be made high again with the command GPIOSetValue(0, 0, 1); . Also a small delay can be needed for some SPI IC’s, 1 millisecond is enough most of the time, the command for a small delay is: delayMs(0, 1);

5. Read data from SPI

To read data the Chip Select has to be made low with GPIOSetValue(0,0,0); . To read data from SPI the command SSPReceive(portnum, (uint8_t *)dest_addr, datanumbers); is used. Portnum is the used SPI port, (uint8_t *)dest_addr is the receive buffer, datanumbers is the amount of databytes that have to be read. A command to read 4 databytes is:
SSPReceive(portnum, (uint8_t *)dest_addr, 4);
The 4 databytes are now stored in dest_addr[0] to dest_addr[3]

Copyright 2011. Joomla 1.7 templates - Joomla template maker. The Simplecortex is developed by BRC-Electronics