Tuesday, July 06, 2010

Serial Port Interface Communication (SPI)

Theory of Operation

The Serial Peripheral Interface Bus or SPI bus is a synchronous serial data link used to communicate between two or more microcontroller and devices supporting SPI mode data transfer. Devices communicate in master/slave mode where the master device initiates the data frame. Multiple slave devices are allowed with individual slave select (chip select) lines.

This communication protocol consists of folliwng lines or pins,

  1. MOSI : Master Out Slave In (Tx for Master and Rx for Slave)
  2. MISO : Master In Slave Out (Rx for Master Tx for Slave)
  3. SCK : Serial Clock (Clock line)
  4. SS : Slave Select (To select Slave chip) (if given 0 device acts as slave)

Master: This device provides the serial clock to the other device for data transfer. As a clock is used for the data transfer, this protocol is Synchronous in nature. SS for Master will be disconnected.

Slave: This device accepts the clock from master device. SS for this has to be made 0 externally.

avr spi

avr spi













Setting up SPI in Microcontroller

avr spi

Master Microcontroller

Open Code Wizard and go to SPI tab. Enable SPI and choose Master in SPI Type.

Select clock rate depending upon your data transfer speed requirement.

Keep other settings as default.

avr spi

Slave Microcontroller

Open Code Wizard and go to SPI tab. Enable SPI and choose Slave in SPI Type.

Select clock rate depending upon your data transfer speed requirement.

Keep other settings as kept in the master microcontroller.

Data Transfer Functions

You can transmit or receive 1 byte of data at a time.

Transmit Data

spi(1 byte data);

Example: spi(‘A’);

Receive Data

c = spi(0); // c is 1 byte variable

Example: char c = spi(0);

6 comments:

  1. on what basis i should select clock rate??

    ReplyDelete
  2. u can set it by what pulse u want it actually to set. calc the time period....

    ReplyDelete
  3. Hello,
    One question: If the master sends one byte to the slave then the master would execute:
    spi('A');
    I supose that in this moment in interrupt called in slave, and in that interrupt we can read inside the slave the 'A' byte; But if the slave wants to answer with some byte what and when should the slave do ?
    When and by who (master/slave) "c = spi(0)" will be called?

    Thank you !
    Adi.

    ReplyDelete
  4. SPI = serial port communication?? haha you're funny :P

    ReplyDelete
  5. that 2 things are written differently read it proper u have taken it another way

    ReplyDelete
  6. Hi Ankush,
    Your answer is related to my previous question ?

    Thank you,
    Adi.

    ReplyDelete