ATmega644 : SPI problem

Hello,

I'm unable to make master mode SPI working on my ATmega644.

Useful information: I program in c (with WINAVR) on an ATmega644 clocked at 20Mhz. I use an oscilloscope to visualize the signal.

On the oscilloscope, I only observe the PB4 pin going from 1 to 0. Nothing else happens ... even no clock!

The hardware part is correct (i've checked several times before posting).

Here is the code reduced to the essential :

#include <avr/io.h>

#define F_CPU 20000000UL

int main (void)
{

// make the MOSI, SCK, and SS pins outputs
DDRB |= ( 1 &lt;&lt; PB5 ) | ( 1 &lt;&lt; PB7 ) | ( 1 &lt;&lt; PB4 );
PORTB |= (1 &lt;&lt; PB4); // SS high 

// make sure the MISO pin is input
DDRB &amp;= ~( 1 &lt;&lt; PB6 );

// set up the SPI module: SPI enabled, MSB first, master mode,
//  clock polarity and phase = 0, F_osc/8
SPCR = 0b01010001;
SPSR = 0b00000001;     // set double SPI speed for F_osc/8

//------------------------------------------------------------------------
PORTB &amp;= ~( 1 &lt;&lt; PB4); 	//PB4 low
SPDR = 10;                            
while ( ! ( SPSR &amp; ( 1 &lt;&lt; SPIF )));		
PORTB |= (1 &lt;&lt; PB4); 		//PB4 high

while (1) 
{ 	
	
	
}
return 1;

}

Any idea ? Thank you!


 

SPI testing

  1. Move the data output instructions inside the while (1) loop. Perhaps add a small delay. Make it easier to capture signals (if and when it starts working).
  2. Add symbolic names to the port configuration & I/O settings for us mere humans.