PIC 18F4520 SPI with Serial EEPROM

I am attempting to communicate with a 25LC256 serial (SPI) EEPROM with a

18F4520. I have successfully communicated with this same EEPROM with a 18F4431 using the same code.

The code I am using is as follows:

MEM_CS = 0;

SSPBUF = 0x02; while(SSPSTATbits.BF != 1); SSPBUF = AddressHigh; while(SSPSTATbits.BF != 1); SSPBUF = AddressLow; while(SSPSTATbits.BF != 1); SSPBUF = DataByte; while(SSPSTATbits.BF != 1);

MEM_CS = 1;

I have connected a scope on the SDO line of the microcontroller. I see the

0x06 clocked out followed by 3 zeros...no matter what the values of AddressHigh, AddressLow, and DataByte are. I notice from the In-Circuit Debugger that after this transfer, the collision bit gets set somewhere. I have tried replacing the 3 variables with fixed values and everything works fine (the correct fixed values are transmitted from the SDO pin).

I see in the dissassembly listing that the "SSPBUF = AddressHigh" instruction is represented using...

MOVFF {SSPBUF}, {AddressHigh} NOP

where the brackets represent the address of SSPBUF and AddressHigh.

Am I missing something here? Is this a problem with the microcontroller itself or with the compiler? I have ruled out a connection problem because everything works fine when using fixed values.


Reply to
Patrick Johnson
Loading thread data ...

Try using the SSPIF flag rather than BF. BF is only valid in SPI slave mode reception AFAUI.

Best regards, Spehro Pefhany

"it's the network..."                          "The Journey is the reward"
speff@interlog.com             Info for manufacturers: http://www.trexon.com
 Click to see the full signature
Reply to
Spehro Pefhany

Are you sure that writing to SSPBUF clears BF? I'm under the impression that happens only with a read, not a write. I would expect your code to fall through the second and subsequent while loops.

That instruction is backwards from your C statement.

John W. Temples, III
Reply to
John Temples

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.