Consider we have two AVR microcontrollers, we want to transmit data from one microcontroller to other microcontroller. The first thing that we need to learn is protocol. What is a protocol?A protocol is some set of rules which should be obeyed. In AVR microcontroller we have certain protocols, SPI is one of them. In SPI protocol, we transmit data from one microcontroller to other using 4/3 pins. This protocol is wired protocol that means, data transmitted through wires. Suppose there are two AVR microcontroller, with one of them microcontroller LCD is connected and with other microcontroller we connected keypad. To avoid confusion let we give names to these microcontroller as-‘M’ the microcontroller in which keypad is connected and ‘S’ the microcontroller in which LCD is connected. Suppose we press an key on keypad connected to M and we want the number written on the key is to be displayed on LCD connected to S. Let here M transmit data to S using SPI protocol. So to build this kind of projects we now study SPI protocol. At last of this section, we study how to build this project.
CONCEPT OF MASTER AND SLAVE ?
Master is device which generates clock pulses and slave receives clock generated by master. But we haven’t discuss the role of clock pulses so don’t confuse just remember definition of master and slave.
SPI BUS PROTOCOL-
The SPI bus was originally started by MOTOROLA corporation (now FREESCALE), but in recent years had become a widely used standard adapted by many semiconductor chip companies. SPI devices use only two pins for data transfer, called MOSI (master out slave in) and MISO (master in slave out), instead of 8 or more pins used in traditional buses. MOSI pin used to transfer data from master to slave and MISO pin used to transfer data from slave to master. So, there are only two pins for data transfer, this reduction of data pins reduces package size and power consumption drastically. Now we can use this protocol in those devices in which size is major concern. The SPI bus has SCK (clock pin) pin used to synchronize the data transfer between different chips. The last pin used in SPI is SS (slave select) pin used to initiate and terminate data transfer. We know that there are three pins used in SPI protocol so SPI bus is also called 4-bus interface. If instead of MOSI and MISO pins we have only one pin for data transfer than this is called 3-wire interface bus. This is important for use know the difference between 4-wire and 3-wire interface bus. Now we studied all pins used in SPI protocol, its time to how SPI works.
HOW SPI WORKS-
Each microcontroller consists of shift register in SPI module. So if we make one microcontroller as master and other as slave so, one shift register consist of each master and slave side. We also know that master is one which generates clock pulses. So, in master side we have clock generator.
The serial-out pin of the master shift register connected to serial-in pin of the slave shift register by MOSI pin (master out slave in) and serial-in pin of the master shift register connected to serial-out pin slave register by MISO pin (master in slave out). We know that shift register only works if clock is given to it. So, the clock generator present in master side generates the clock which is given to all shift registers present in slaves connected to master.
In SPI all register are of 8-bit long. We know that in shift registers, each bit is shifted with one clock pulse i.e. after every 8 clock pulses the content of two shift registers interchanged. Now suppose master wants to send a byte of data to slave, so master writes the data in its shift register and after 8 clock pulses the content of master shift register goes to slave shift register. And if slave wants to send the data to master then, it writes the data to its shift register and generates 8 clock pulses. After 8 clock pulses master receives the data in its shift register. In this, way we can transmit and receive data from at same time so, SPI is FULL DUPLEX.
Now we will study architecture of SPI. In SPI architecture, we consider there are two microcontrollers, one is master and other is slave. We connected these microcontrollers using SPI protocol. Now we focus on the diagram below:
CLOCK POLARITY AND PHASE IN SPI-
In SPI communication, the master and slave must agree clock polarity and phase with respect to data.
- CPOL (clock polarity).
- CPHA (clock phase).
When CPOL=0, the base value of clock is low (0) and when CPOL=1, the base value of clock is high (1). At CPHA=0 means sample on the leading clock edge, while CPHA=1 means sample on the trailing clock edge.
- If base value of clock is zero then, leading clock edge is rising edge.
- If base value of clock is one then, leading clock edge is falling edge.
|CPOL||CPHA||Data Read and Change Time||SPI MODE|
|0||0||Read on Rising Edge, changed on a Falling edge||0|
|0||1||Read on Falling Edge, changed on a Rising edge||1|
|1||0||Read on Falling Edge, changed on a Rising edge||2|
|1||1||Read on Rising Edge, changed on a Falling edge||3|
SPI PROGRAMMING IN AVR-
We have studied, basic idea behind SPI protocol. Let we recall what is going on in SPI protocol- there is one microcontroller which generate clock pulses that is connected to all shift registers. And there are devices connected to these master called slave. The data transfer between master and slave occurs with each clock pulse generated by master. The data which we have to transmit to slave from master is placed in shift register which after 8 clock pulses transferred to slave shift register and this shift register connected by MOSI pin. And the transfer from slave to master is placed in shift register of slave which is after 8 clock pulses transferred to shift register of master, these shift registers connected by MISO pin. Now we will study the registers used in SPI. There are mainly three registers used-
- SPCR (SPI control register).
- SPSR (SPI status register).
- SPDR (SPI data register).
SPSR (SPI status register):
SPCR (SPI control register):
|1||0||0||Fosc/2 (Not Recommended)|
SPDR (The SPI data register):
The SPI data register is read/write register. We have talk about shift register in SPI above. Suppose we want to transmit data from master to slave then, we write data into shift register. But to write data into shift register we have to write it into SPDR register. So we can say that SPDR works as shift register. As soon as we write into SPDR register the data transmission started. And to read data transmitted from slave we read the content of SPDR register.
SS PIN IN AVR:
We mentioned before, the slave select pin of SPI bus is used to initiate and terminate the data transfer. There are some important points about this pin given below:
- When we are in master mode, we can choose this pin either input or output. If we make it output then, SPI circuit of AVR not control the function of SS pin then, we can make it either 1 or 0 doesn’t matter. But if we make SS pin as input, it will control the function of SPI. We should always externally make SS pin high when we make it as input pin to ensure master SPI operation. If an external device makes SS pin low then, SPI module stops working in master mode and switches to slave mode by clearing the MSTR bit of SPCR register, and then sets SPIF bit in SPSR register. So it is recommended that we have to make SS pin as output so no external device interrupt the SPI master mode operation.
- When we are in slave mode, the SS pin should always be input and we cannot control it by software. So we have to hold the SS pin low externally. Now when SS pin is driven high, SPI is disabled and all pins of SPI are input. Also SPI module, clears all partial data received in SPDR register.
- When we are working in slave mode and SS pin is driven high by an external device, the SPI is module reset but not disabled and it is not necessary to enable it again.
SPI PROGRAMMING IN AVR-
We have studied, registers used in SPI protocol and its description. Now we can start programming of SPI.
- MASTER MODE PROGRAMMING:
Following are steps to be executed in master mode programming-
- Set SS pin (PINB4) as output pin. As we know that when SS pin is output than we can work in master mode uninterrupted from external device.
- Set MOSI pin (PINB5) as output because data is moved from master to slave through this pin.
- Set MISO pin (PINB6) as input because data is moved from slave into master through this pin.
- Set SCK pin (PINB7) as output because master generate clock pulses. These clock pulses given to slave. So, we can connect the SCK pin of master to SCK pin of slave.
- The data to be transmitted from master is written in SPDR register and then, we wait till SPIF flag to become ‘1’.
- To transmit data again we again place data in SPDR register and wait till SPIF flag is set.
- SLAVE MODE PROGRAMMING:
- Set SS (PINB4) pin as input. As we know that in case of slave mode there is only one option for SS pin that is input. And we drive this pin low externally.
- Set MOSI (PINB5) pin as input. We are doing slave programming and at MOSI pin the data transfer from master is coming to MOSI pin so status of this pin should be input to get data into shift register.
- Set MISO (PINB6) pin as output. At MISO pin, the data from slave is moved towards master so status of this pin should be input.
- Set SCK (PINB7) pin as input. The clock pulse coming from master is connected to SCK pin of slave so, status of SCK pin should be input as this pulses are to be coming inside the slave microcontroller.
- We have to wait till SPIF flag in SPSR register is set. As when SPIF flag is set then, data from master is completely transferred to slave.
- Then, the data received in slave can be displayed on any PORT of slave microcontroller. Here , we displayed data on PORTD.
EXAMPLE: Write a program in which we connected keypad to one microcontroller i.e. act as MASTER and LCD to other i.e. act as SLAVE. And we want that, when we press key in keypad then, the number written on it is displayed on LCD connected to slave. The data from MASTER to SLAVE transfer using SPI protocol.
ANSWER:FOR MASTER MODE PROGRAMMING-
- Initialize keypad connected to master microcontroller.
- Initialize SPI module by setting SPE, SPR0, MSTR, SPI2X etc bits.
- Declare “KEYSCAN()” function in which we check whether an key is pressed or not. And if key pressed then, the number written on it is transferred to slave microcontroller using SPI protocol.
- To transfer data from master to slave we make a function i.e. transfer().
- In transfer function we transferred string to slave microcontroller.
- But using SPI protocol we can only transfer one character at a time then, we transferred string as one character at a time.
PROGRAM – MASTER MODE
FOR SLAVE MODE PROGRAMMING-
- Initialize LCD connected to slave microcontroller.
- Initialize SPI protocol by setting SPR0, SPE, SPI2X bits.
- Wait till data transmitted from master to slave i.e. SPIF flag is set.
- If SPIF flag is set then, call “lcd_data()” function and display the data on LCD.
- If data completely displayed on LCD then, clear the LCD and wait till next data is come.