EmbeddedRelated.com
Forums
Memfault Beyond the Launch

PIC 18F4520 SPI with Serial EEPROM

Started by Patrick Johnson May 17, 2006
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.

Thanks!


On Wed, 17 May 2006 21:40:57 -0500, the renowned "Patrick Johnson"
<kirilian@bellsouth.net> wrote:

>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. > >Thanks!
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 Embedded software/hardware/analog Info for designers: http://www.speff.com
On 2006-05-18, Patrick Johnson <kirilian@bellsouth.net> wrote:
> 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: > > SSPBUF = 0x02; > while(SSPSTATbits.BF != 1); > SSPBUF = AddressHigh; > while(SSPSTATbits.BF != 1); > SSPBUF = AddressLow; > while(SSPSTATbits.BF != 1); > SSPBUF = DataByte; > while(SSPSTATbits.BF != 1);
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.
> 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.
That instruction is backwards from your C statement. -- John W. Temples, III

Memfault Beyond the Launch