Hello,
I can see two problems with your code . . .
As Peg has already said, to read a single byte value from the EEPROM, you need to send three bytes, (and receive three bytes) over the SPI.
1st byte: send READ command, receive garbage
2nd byte: send ADDRESS, receive garbage
3rd byte: send dummy value (say zero), receive value from EEPROM
The second problem is that receive overrun will be occurring, since the SPRF flag is not cleared for the first two bytes sent. This will cause loss of the later received bytes, i.e. the second garbage byte and the real EEPROM value. The flag must be cleared after both the first and second send bytes, even though the returned data is not used. You would wait until SPRF is set, and then read SPI1D, as is already done for the third byte.
A final comment - the MISO line will be high-Z during the sending of the first two bytes, and when the EEPROM device is not enabled. If the SPI master is to remain continuously active, there should be an external pull-up (or pull-down) resistor connected to this line to prevent the input to the MCU causing excessive current draw when at an intermediate voltage level.
Alternatively, if the SPI module is disabled when not in use, the underlying GPIO associated with all three SPI pins should be set for output.
Regards,
Mac
Message Edited by bigmac on
2007-07-17 03:09 AM