Hello everyone,
I just learnt how the SPI protocol works, and wanted to try it out on a simulator before doing it on hardware. So, I wrote some code for communicating between two UNOs and tried to run it on Tinkercad.
The master sends an integer '10' and the slave sends an integer '20' back (well, at least that's what I want them to do). Then both of them print their received values over Serial. The SS pin (pin 10) on the master is set as an output and the one on the slave is hooked to ground (as an input), as explained in this application note.
But the problem is, it does not seem to work. Looks like it gets stuck in this line :
while(!(SPSR & (1 << SPIF)));
I guess this means that a transaction never takes place. What's wrong?
Here are the codes for the two devices:
Here is the Tinkercad simulation.
I just learnt how the SPI protocol works, and wanted to try it out on a simulator before doing it on hardware. So, I wrote some code for communicating between two UNOs and tried to run it on Tinkercad.
The master sends an integer '10' and the slave sends an integer '20' back (well, at least that's what I want them to do). Then both of them print their received values over Serial. The SS pin (pin 10) on the master is set as an output and the one on the slave is hooked to ground (as an input), as explained in this application note.
But the problem is, it does not seem to work. Looks like it gets stuck in this line :
while(!(SPSR & (1 << SPIF)));
I guess this means that a transaction never takes place. What's wrong?
Here are the codes for the two devices:
C++:
//Master device
byte val;
void setup()
{
SPI_initMaster();
Serial.begin(9600);
Serial.println("Ready...");
}
void loop()
{
val = SPI_transact(10);
Serial.println(val);
}
void SPI_initMaster(){
DDRB |= (1<<PB2)|(1<<PB3)|(1<<PB5); //Configure MOSI, SCK and SS pins as output
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0); //Enable SPI as Master, Prescaler = 16
}
byte SPI_transact(byte data){
SPDR = data;
while(!(SPSR & (1 << SPIF)));
return SPDR;
}
C++:
//Slave device
byte val;
void setup()
{
SPI_initSlave();
Serial.begin(9600);
Serial.println("Ready...");
}
void loop()
{
val = SPI_transact(20);
Serial.println(val);
}
void SPI_initSlave(){
DDRB |= (1<<PB4); //Configure MISO as output
SPCR = (1<<SPE); //Enable SPI as Slave
}
byte SPI_transact(byte data){
SPDR = data;
while(!(SPSR & (1 << SPIF)));
return SPDR;
}
Here is the Tinkercad simulation.
