SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By Mustafa81
I have two of the following boards:

I am trying to see if I can get the SPI's to communicate with each other by setting one board as a master and the other as a slave.

Here is the code I am using for the functions to initialize and control the SPI:

/********************** SPI Initialization*********************/
//Sets the SPI on the microcontroller to be the master
void SPI_MasterInit(void)
// set PB1(/SS), PB3(MOSI), PB2(SCK) as output
DDRB = DDRB | 0b00000111;

// enable SPI Interrupt and SPI in Master Mode with SCK = CK/16
SPCR = SPCR | 0b11010001;
void SPI_MasterTransmit(char cData)
/* Start transmission */
SPDR = cData;

/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)));

void SPI_SlaveInit(void)
/* Set MISO output, all others input */
DDRB = DDRB | 0b00001000;

/* Enable SPI */
SPCR = SPCR | 0b01000000;

char SPI_SlaveReceive(void)
/* Wait for reception complete */
while(!(SPSR & (1<<SPIF)))
/* Return data register */
return SPDR;

I call the initialization functions on each board respectively and on the transmitter I send a defined single char through the SPI_MasterTransmit();. On the slave side I set the SPI_SlaveRecieve(); to a deined char as well and send that recieved char to the LCD.

The LCD is working fine since I have tested it using other display code. Can anyone tell me if my code looks sound?

Also note, on my boards I connected the MISO's, MOSI, SCK and SS to each other on the boards (this could also be an issue). Any help would be greatly appreciated. If you need more information let me know.

Thanks in advance.
By Mustafa81
Is there anyone willing to help out?
By Mustafa81
Can someone help me please?
By n1ist
This comment doesn't match the code. Since you don't say which processor you are using, I can't tell which is correct...
// set PB1(/SS), PB3(MOSI), PB2(SCK) as output
DDRB = DDRB | 0b00000111;

Make sure nothing is accidentally setting *SS hi as that will turn off the slave's SPI port.


[Edited to remove wrong comment about crossing MOSI/MISO - I've been spending too much time with conventional serial...]
Last edited by n1ist on Thu Aug 14, 2008 8:54 am, edited 1 time in total.
By Lajon
MOSI from the master must connect to MISO on the slave.
No! MOSI connects to MOSI and MISO to MISO.
By stevech
MOSI = Master-out, Slave In
MISO is the opposite
SCLK is sourced by the master.

I guess it's not possible to have a node change roles from master to slave, unless the interconnections swap MOSI/MISO for role reversal. But doing this is rare.

I suppose this leads to why we have a no-master bus like CAN
By Mustafa81
Thanks for the responses everyoen, but I figured it out.

My hardware was connected correct which is:

I was doing two things wrong. The first is when I transmit I need to toggle SS low and put it high when I was done. The second thing I was doing wrong was I was sending a single character but when I was trying to display it on the LCD I was using my string display function instead of character display. Also I defined my char as unsigned just incase.

After I did this, the SPI worked fine. Now my next step is to get the nordic transievers to work with the SPI.