Ok, first of all to read a byte you have to send a byte to clock the chip. If you have to send a byte, I am suggesting that it be FF. I the case of this chip it does not matter, because the spec says the SI line is "don't care" when the chip is sending data.
It would make life easier if you created a function to send/recieve a byte.
byte SendRecieveSPI( byte data )
{
SPID = data;
while(!SPIS_SPRF) ;
data = SPID;
return data;
}
Also as peg pointed out, you should do this in your init code,
before you start using the SPI:
(void)SPIS; // Read the status register
(void)SPID; // Read the device register
You should also declare temp as
volatile in case the compiler decides to optimize away the code that in it's mind is not doing any thing useful.
You need to send a 24 bit address.
Quoting the spec.
The device is selected by pulling CS low. The 8-bit read instruction is transmitted to the 25XX1024
followed by the 24-bit address, with seven MSBs of the address being don’t care bits. After the correct read instruction and address are sent, the data stored in the memory at the selected address is shifted out on the SO pin. The data stored in the memory at the next address can be read sequentially by continuing to provide clock pulses. The internal address pointer is automatically incremented to the next higher address after each byte of data is shifted out. When the highest address is reached (1FFFFh), the address counter rolls over to address 00000h allowing the read cycle to be continued indefinitely.
The read operation is terminated by raising the CS pin (Figure 2-1).
So what you need to do is:
Raise Chip Select do this to be sure you have cleared the chip
Lower Chip Select
Send Read Command
(Send address >> 16) & 1 Send the highest byte - only the lsb counts here.
(Send address >> 8) & 0xff Send the middle
(Send address) & 0xff Send the lo byte.
Now you can loop on reading back as many bytes as you need.
Note however, you must NOT raise the CS line until you are done reading.
Raising the CS line signals you are done and resets the chip to receive another command.
However, when you are done reading and wish to start another command, you MUST raise the CS line.
Also, don't forget you must erase a sector or a block before writing.
Message Edited by JimDon on
2008-02-04 08:45 PM