please help, what is wrong with my SPI codes for Atmega8?

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

what is wrong with my SPI codes for Atmega8,please ? I could not get the data to be transmitted.
program cannot end the while-loop in "SPI_MasterTransmit".

is "SPSR" initially 0x00?, but I found its value is 0x128, why ? code as following........Many thankz

//***********************************
//to initialize the SPI as a Master
//***********************************
void SPI_MasterInit(void){

	// Set MOSI and SCK output for atmega8 , all others input
	DDRB = (1<<DDB3)|(1<<DDB5);

	// Enable SPI, Master, set clock rate SCK=fck/16
	SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}

void SPI_MasterTransmit(char cData){

	/* Start transmission */
	SPDR = cData;

	/* Wait for transmission complete 1<<SPIF(Interrupt Flag) == 0x80*/
	while(!(SPSR & (1<<SPIF)));
}
//***********************************
//to initialize the SPI as a Slave
//***********************************
void SPI_SlaveInit(void){

	//Set MISO output (= 1), all others input
	DDRB = (1<<DDB4);
	// Enable SPI , set sPE =logic ONE, turns system on,
	SPCR = (1<<SPE);
}

char SPI_SlaveReceive(void){

	// Wait for reception complete
	while(!(SPSR & (1<<SPIF)));
	// Return Data Register 
	return SPDR;
}
Last Edited: Thu. Jul 15, 2010 - 04:19 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is this a trick question?

What is the observed problem and why do you think it is in the above code?

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

program cannot end the while-loop in "SPI_MasterTransmit".

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
   // Set MOSI and SCK output for atmega8 , all others input 
   DDRB = (1<<DDB3)|(1<<DDB5); 

Wrong. /SS must also be an output in master mode or stay high at all times if an input.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

cheers