Hi,
This is my first time using the hardware SPI on an ATmega, although
I've bit banged SPI plenty of times. I want to use the hardware SPI
since all the shifting happens in the background.
What I've found is that while the data seems to be being shifted out
and the clock generated OK, nothing seems to be being sensed on the
MISO input of the master. I.e., I send commands, but responses all
come back as 0x00 or 0xff depending on whether I have the internal
pull-up enabled on the MISO pin.
Is this normal for the hardware SPI? While the data sheet is not
perfectly clear to me, it seems to imply that when you write data to
the SPDR register, the clock begins and the data is shifted out. Once
shifting is complete as signaled by the SPIF flag in the SPSR
register, you are then supposed to read the SPDR register which should
contain the previous response from the slave device. Is this correct?
Just to double check the slave, I implemented a simple bit bang SPI
using the same pins that are used by the hardware SPI and I then get
the expected responses from the slave. Perhaps I've misconfigured the
SPI for what I want to do? Can anyone offer a suggestion?
Here's my setup and byte transfer code for an ATmega128:
void spi_init(void)
{
DDRB = 0x07; /* /SS, CLK, & MOSI are outputs */
PORTB |= 0x01; /* deselect slave */
PORTB |= 0x08; /* enable MISO pull-up */
/*
* set up for master, lsb first, clock idle high, sample on trailing
* edge, freq=fosc/128 (125,000 bps @ 16 MHz)
*/
SPCR = BV(SPE)|BV(MSTR)|BV(DORD)|BV(CPOL)|BV(CPHA)|BV(SPR1)|BV(SPR0);
}
uint8_t spi_tx_rx(uint8_t c)
{
uint8_t b;
SPDR = c;
while (!(SPSR & BV(SPIF)))
;
b = SPDR;
return b;
}
For sanity's sake, I've tried all variations of DORD, CPOL, and CPHA,
with no affect. All received data comes back as 0xff.
Any ideas?
Thanks,
-Brian
--
Brian Dean, bsd@bdmicro.com
BDMICRO - Maker of the MAVRIC ATmega128 Dev Board
http://www.bdmicro.com/Message
SPI on an ATmega not quite working
2004-02-03 by Brian Dean
Attachments
- No local attachments were found for this message.