SPI Port Atmega1280 TQFP100

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

I am trying to output a byte consitantly out of the SPI port on the uC.

	serial_enable();

	txchar_serial('1');
	

	DDRK |= 0x80;
	PORTK &= ~0x80;
	DDRB |= 0x06;
	DDRB &= ~0x09;
	PORTB &= ~0x0F;

	SPCR = (1 << SPE) | (1 << MSTR);
	SPSR = (1 << SPI2X);

	while(1)
	{
		if(SPSR & (1 << SPIF))
		{
			SPDR = 0xAA;
			txchar_serial('2');
		}
	}

I only see it output 12 to the serial port, instead of 12222222222222.... like I expect.

Any ideas?

Port K is for a Chip select pin. It's like the SPI port isn't working.

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

I changed the code to make SS an output so the Master mode is not switched off and also clear the SPI bit in the power reduction register. I only get 1 outputed now. Still no luck.

	serial_enable();

	txchar_serial('1');
	
	PRR0 &= ~(1 << PRSPI);

	DDRK |= 0x80;
	PORTK &= ~0x80;
	DDRB |= 0x07;
	DDRB &= ~0x08;
	PORTB &= ~0x0F;

	SPCR = (1 << SPE) | (1 << MSTR);
	SPSR |= (1 << SPI2X);

	while(1)
	{
		while(!(SPSR & (1 << SPIF)))
		{}
		
		SPDR = 0xAA;
		txchar_serial('2');
	}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okay, it works now, a code sequence problem.

	serial_enable();

	txchar_serial('1');
	
	PRR0 &= ~(1 << PRSPI);

	DDRK |= 0x80;
	PORTK &= ~0x80;
	DDRB |= 0x07;
	DDRB &= ~0x08;
	PORTB &= ~0x0F;

	SPCR = (1 << SPE) | (1 << MSTR);
	SPSR |= (1 << SPI2X);

	while(1)
	{
		SPDR = 0xAA;
		txchar_serial('2');
		
		while(!(SPSR & (1 << SPIF)))
		{}
		

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

Hehe robinsm is correct, your code waits for transmittion to complete before it is initiated...

As you see on the code he pasted. the line

while(!(SPSR & (1 << SPIF))) 

has to be placed below the transmitt.

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

And then again I read a bit to fast :) Seems like robinsm is you :) well you got the problem solved :)