I have been trying to understand SPI Interrupt i.e. what does it do and why is it used ? Reading about it on the web has not helped me. Can some one please explain it in a simple terms ?
SPIE or "SPI Interrupt Enable" is used to enable the interrupt.
(Assuming you are in Master mode) There are two ways of transferring data using SPI:
1.Polling: Here you write data in SPI's data register (SPDR) and then poll on the flag for SPI Transmission complete (SPIF).
2.Interrupt based: In this case, when you enable Interrupt for SPI, you write data in SPDR. Controller will transmit the data and will raise an interrupt when transmission is completed.
A question may arise that why there is interrupt for transmission but not for the reception, I am tempted to answer but It would be better if you figure that out! :-)
Assuming you are talking about AVR, the SPIE (SPI Interrupt Enable) bit is to enable SPI related interrupts.
The number of interrupts an SPI peripheral can have will vary depending on the device, but if we take the Atmega8L as an example, it only has one interrupt.
Looking at the datasheet for what SPIE does, it says the following :
• Bit 7 – SPIE: SPI Interrupt Enable This bit causes the SPI interrupt to be executed if SPIF bit in the SPSR Register is set and the if the global interrupt enable bit in SREG is set.
Basically when SPIE AND SPIF AND the global interrupt enable bit is set, you will get get an interrupt.
What's the interrupt ? Well lets see what SPIF does. In the datashet, it says the following:
• Bit 7 – SPIF: SPI Interrupt Flag When a serial transfer is complete, the SPIF Flag is set. An interrupt is generated if SPIE in SPCR is set and global interrupts are enabled. If SS is an input and is driven low when the SPI is in Master mode, this will also set the SPIF Flag. SPIF is cleared by hardware when executing the corresponding interrupt Handling Vector. Alternatively, the SPIF bit is cleared by first reading the SPI Status Register with SPIF set, then accessing the SPI Data Register (SPDR).
The text here (I think) is pretty self explanatory, so I won't repeat it.
If you don't understand what a particular register or bit does, flip through the datasheet. Sometimes you might have to jump from bit to bit in order to get the full picture of how it works or what its supposed to be doing.
In a typical application that uses SPI communications, the microcontroller needs to receive data at some point. To receive data, the microcontroller has to read and write the SPI signals in a specific way as dictated by the protocol.
Now since you can't have the microcontroller constantly busy doing this, microcontrollers sometimes are equipped with a hardware module solely responsible for the SPI communications. This allows the microcontroller to work on other stuff instead of handling SPI.
The interrupt is raised by the SPI module and it is used to notify the microcontroller that a byte (or more) was received and it is ready for processing.