SPI protocol tutorial in ARM7 Microcontroller

Share on FacebookShare on Google+Tweet about this on TwitterShare on RedditShare on StumbleUponPin on Pinterest

SPI-protocol-tutorial-ARM-microcontroller

SPI ( Synchronous Peripheral Interface) is one of the important protocols used in Microcontrollers to establish serial communication between two devices. This protocol was widely used in many devices since the data transfer rate is pretty high than other protocols such as UART and I2C. This article deals with SPI protocol tutorial using ARM 7 Microcontroller. 


The above design shows two controllers one of which is Master which transmits the data and the other one is the slave which receives the data and exhibit it its port1. The port 1 is provided with 8 LED’s which indicate represents the received data from the master controller.

SPI PROTOCOL:

SPI is a synchronous, full duplex protocol which basically requires three communication lines to establish the connection between the devices. Hence it was also called as 3 wire interface protocol. A controller should act as a master and other should act like slaves. A master can have multiple slaves also both the master and slave can transmit/receive data. ARM 7 Microcontrollers possess two SPI  controllers which controls SPI0 and SPI1. 

PIN FUNCTIONS:

As i said above three communication line forms the backbone of this protocol, however a fourth one was also used to select the slaves by the master. The function of the Pins used in our controller is as follows.

 

MOSI – Master Output Slave Input pin serves as output pin for the Master and input for Slave.

 

MISO – Master Input Slave Output serves as input pin fro Master and output for the slave.

 

SCK –  This pin generates the required clock for the communication to takes place. Master generates clock and feed the pulse to the slave device.

 

SSEL – This pin is used to select the slave device when multiple slave devices are used along with a single master. The SSEL is an active low pin, so to select a particular slave this pin should be pulled low.

REGISTERS USED IN SPI PROTOCOL:

In ARM7 controllers we use four registers to control the operation of the SPI they are as follows.

SPCR REGISTER:

SPI control register is an 8 bit register which is used to set the modes in which we SPI to operate. I have added a snippet of the bit functions in the register for better understanding.

SPCR-register-lpc2124-microcontroller

SPSR REGISTER:

SPI status register is an 8 bit register which reveals the status of the SPI communication such as completion of transmission/ reception, mode fault etc. 

SPSR-register-lpc2124-microcontroller

SPDR REGISTER:

This register holds the data for transmission and reception. Data can be transmitted by writing in this register and data received can be read from this register. 

SPCCR REGISTER:

This register controls the frequency of a master’s SCK. The value of this register must be an even number and should be greater than or equal to 8. The SPI clock frequency is governed by the formula 

SPI rate = PCLK (Processor Clock rate) / SPCCR value 

STEPS TO PROGRAM SPI IN ARM CONTROLLERS:

MASTER MODE:

  1.  Select the functions of the pins MISO, MOSI, SCK using the PINSEL0 register.
  2. Set the direction of the SSEL pin for selecting the slave connected to the controller.
  3. Load the appropriate calculated value in the SPCCR register to generate the required clock frequency.
  4. Select the master mode and set the clock phase, polarity using the SPCR register.
  5. Select the slave by pulling the SSEL pin low.
  6. Write the data in the SPDR register for transmission.
  7. Check the status of the transmission by reading the SPIF bit in the SPSR register.

SLAVE MODE:

  1. Select the slave mode using the SPCR register.
  2. Wait until the SPIF flag in the status register is set.
  3. Read the data from the SPDR register in the slave controller.

CODE:

This code was built using Keil uVision 4 compiler. The code was built in such a way to transmit data from a Master controller to the slave controller where the data is then sent to PORT 1.

#include<lpc21xx.h>
int main(void)
{
 PINSEL0=(1<<8)|(1<<10)|(1<<12); //Selecting SPI pins
 IODIR0=(1<<7);
 IOSET0=(1<<7);
 S0SPCCR=8;                         //SPI frequency
 S0SPCR=0x38;                     //SPI master mode
 IOCLR0=(1<<7);
   while(1)
   {
     S0SPDR=0xff;                 //Data transmission
     while(!(S0SPSR&(1<<7)));   //Status check
     S0SPDR=0x0f;
     while(!(S0SPSR&(1<<7)));
   }
}

NOTE:

  • Make changes in the above code as stated in the Programming steps of the Slave controller to obtain the code for the slave device.
  • You can use any pin to select the slave by pulling it low.
  • Multiple slaves can be connected to a single master by assigning each slave select that is SSEL pins from the master for the slaves.
 

Related Designs and Schematics

Share on FacebookShare on Google+Tweet about this on TwitterShare on RedditShare on StumbleUponPin on Pinterest
The following two tabs change content below.

Frank Donald

Junior Network Engineer at Tata Communications
Frank Donald is an Electronics and Communication Engineer who is very passionate about his subject. Reading, Surfing and Blogging are the things he loves.

Ask your query

1 Comment on "SPI protocol tutorial in ARM7 Microcontroller"

Notify of
avatar
Sort by:   newest | oldest | most voted
Goldy
Guest

Could you please elaborate how the status check works in your code?

wpDiscuz