SPI confussion

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey guys,

This is the first time im going to use the SPI inteface and i have some confusion with th ^SS pin.

let me explain what im doing..
I have 2 identical PCB boards. The boards can be configured as either a Master or Slave by setting a jumper(possibly ^SS).
The Master Board only transmits while the slave board only receives data. (uni-directional)
The code on both the boards is the same. but runs routines based on the selection of the master/slave jumper.

i will be writing the code shortly.. but please correct me if things cannot be done as mentioned above.

Thanks

Rodney Almeida

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

_SS is used to re-synchronize the SPI logic in two devices. It's very unwise to hard-wire it because then you lose the ability to re-synch if a clock pulse is ever missed (or noise creates a spurious one).

BTW this thread has nothing to do with GCC so moving to AVR Forum.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

oh pls dont move it.. my code will follow soon

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But SPI is generic whichever version of C you use. Just because you happen to use GCC to write some code that operates an SPI does not make the question about the use of GCC in particular.

Moderator.

PS If this does head off in the direction of being something that could only possibly apply to GCC I will move it back but there are a number of vociferous members here who complain when generic AVR questions get "hidden" in the GCC Forum.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

my code is here... please correct me

int main (void)
{
char choice =0;
char datain;
DDRD &= ~(1<<2);     
PORTD |= (1<<2);     //switch connected to PD2 which sets the board to Master or Slave

if (!(PIND & (1<<PD2)))
	{
	choice=1;
	}

while(1)
	{
	if (choice==0)
		{
		//master code
		SPI_MasterInit();
		SPI_MasterTransmit('A')
		}
	if (choice==1)
		{
		//Slave code
		SPI_SlaveInit();
		datain = SPI_SlaveReceive();
		}
	}
}



void SPI_MasterInit(void)
{
DDRB = ((1<<DDB5)|(1<<DDB7)|(1<<DDB4)); //spi pins on port b MOSI SCK,SS outputs 
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);// Enable SPI, Master, set clock rate fck/16
}


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 = (1<<DDB6);
/* Enable SPI */
SPCR = (1<<SPE);
}
char SPI_SlaveReceive(void)
{
/* Wait for reception complete */
while(!(SPSR & (1<<SPIF)))
;
/* Return data register */
return SPDR;
}