Reg: SPI communication using interrupts

Hi guys,
I need some help with SPI communication using interrupts. I am trying to transmit and receive data from 8051 microcontroller. I am able to send the data from Adruino Uno board to the 8051 microcontroller, but when the 8051 replies with a data my atmega328 fails to generate the interrupt. I have attached the code in the post. Kindly do the needful. Thx in advance.

NOTE: Using polling method I am able to send and receive the data

SPI_Interrupt.ino (2.48 KB)

seldom used SPI but I recognize blocking code

ISR(SPI_STC_vect)
{    
     while(!(SPSR & (1<<SPIF))); <<<<<<<<<<< can block in theory and therby explain why you do not receive anything,

That line just doesn’t feel right to me.

Furthermore it is considered not good practice to have serial print statements in an ISR. (depends heavily on the data rate, but still)

I have written my code by reading the document attachment.

SPI - Atmega tutorial.pdf (256 KB)

Anybody who has worked with SPI interrupts, kindly post a link or code where I can go ahead. I am stuck… :cold_sweat:

I'm trying to get my head around what the interrupts are needed for. Are you implementing an SPI [u]slave[/u] device?

In my application the master cannot be directly connected to the wireless communication device hence, I have two micrcontroller where the slave is interfaced with the wireless device and the data will be sent or received at any point of time through SPI protocol.

The slave is interfaced with wireless communication device and the master has to receive the data from the slave. And if the master wants to sent the data through wireless it has to send it to the slave. All the sequences happen asynchronously i.e. the data can be sent or received at anytime.

I am able to send the data from Adruino Uno board to the 8051 microcontroller, but when the 8051 replies with a data my atmega328 fails to generate the interrupt.

Which micro is the master? ALL SPI communication comes from the master. The slave replies on the same bytes the master sends. The slave cannot initiate data.

Yadav: In my application the master cannot be directly connected to the wireless communication device hence, I have two micrcontroller where the slave is interfaced with the wireless device and the data will be sent or received at any point of time through SPI protocol.

The slave is interfaced with wireless communication device and the master has to receive the data from the slave. And if the master wants to sent the data through wireless it has to send it to the slave. All the sequences happen asynchronously i.e. the data can be sent or received at anytime.

I don't really understand what's going on, but if what you want is bidirectional communication between two devices which may be initiated by either side, wouldn't some form of duplex serial interface be more suitable? If you're trying to make an SPI slave initiate a message to the SPI master, I can't see that working.

As an aside, how certain are you that the 'master' can't be connected directly to the 'wireless communication device'? You seem to be facing considerable complexity and I wonder whether it's necessary.

If you’re trying to make an SPI slave initiate a message to alert the SPI master, I can’t see that working.

I have done this method. You can add another line for a pin change interrupt to the master. The master then sends out a stream of zeros that the slave fills in. The usual reason for SPI is speed. A pair of 328 Arduino’s can run 2Mhz SPI.

Yadav has not told us if the Arduino is master or slave. Does he really need the speed? And to your point, does he really need 2 microcontrollers?

Joe

Thx for your interest guys. Arduino is the master and 8051 micro controller from Phillips is the slave. The speed is not an issue.

I can change my configuration any time since its just a prototype I am building. But I am just curious to find out why the slave cannot send the data to the master without any request from the master. If SPI protocol does not allow this type of communication, then what is the use of interrupts in SPI communication. The communication can be performed using polling method only rite???

The data depends on the clock line. The clock is generated by the master, 8 cycles (1 byte) at a time. On each clock cycle the master has set its data line MOSI and it reads the slave line MISO. The 8 cycles are only generated when the master transmits a byte. This very simple protocol allows for high speed. I can move data between 2 16Mhz Arduino's as 2Mhz. Some claim even 4Mhz. I2C and serial are much slower.

As I stated before, the slave could always raise a separate interrupt on another master input line. The master could then send out an acknowledge byte and a stream of 0's until a slave 'end of message' byte is received. This is just an example, you can invent any protocol you wish.

Thx Joe, I think now I have a grip on SPI protocol. I was always wondering how can the slave send the data independently when the master controls the clock :cold_sweat:. Joe Ur method wrks for me and my project. Thx again to Joe and all the others who hav taken interest in this topic. Cheers!!!!!!!! XD