NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » SPI transfer delay

April 17, 2011
by Hexorg
Hexorg's Avatar

Hello, While trying to read some information from spi interface, I got puzzled... Here's my SPI function:

uint8_t SPI(uint8_t d)
{
uart_write('T');
uart_write('X');
uart_write(' ');
SPDR = d;
uart_write('W');
uart_write('A');
uart_write('I');
uart_write('T');
uart_write(' ');
LED_ERR = 1;
loop_until_bit_is_set(SPSR, SPIF);
LED_ERR = 0;
//while (!(SPSR & (1<<SPIF)));
uart_write('R');
uart_write('X');
uart_write('\r');
uart_write('\n');
return SPDR;
}

I've added uart output to trace the error better. And here's what UART says:

TX WAIT RX
TX WAIT

How is that possible? O_o I thought SPI just outputs the data, and as soon as the data is out, it sets that bit, well, apparently it doesn't >_<

Sorry, i'm a little grumpy, was fishing for this error for past 13 hours...

April 18, 2011
by bretm
bretm's Avatar

Are you still having this problem?

If the SPI interrupt is enabled (even if you didn't provide an ISR) it could clear the flag before you had a chance to look at it. That's all I can think of.

April 18, 2011
by Hexorg
Hexorg's Avatar

I solved it. I guess I had a lot of noise in my circuit. I forgot to set SS pin as output, so I guess uC was becoming a slave at that point.

Post a Reply

Please log in to post a reply.

Did you know that spinning a permanent magnet motor makes a voltage? Learn more...