Hi,
I'm working with a SPI flash memory device (SST25VF080B) and I'm having some difficulties with MSSP module.
I'm trying to write to the status register of the device and this is the subroutine I'm using for that purpose:
Code:
SST_WriteStatus:
PORTB.7 = 1 ' Logic analyzer trig
PAUSEUS 5
' First we execute the EWSR instruction to enable writing to the status register.
SST_CE = 0
SSPBUF = SST_Cmd_EWSR
GOSUB SST_WaitForSPI
SST_CE = 1
' Then we execute the WRSR command to actually write the new value to the statusregister.
SST_CE = 0
SSPBUF = SST_Cmd_WRSR
GOSUB SST_WaitForSPI
SST_Status = 101010
'SSPBUF = SST_Status ' Thru variable
SSPBUF = 101010 ' Hardcoded
GOSUB SST_WaitForSPI
SST_CE = 1
PORTB.7 = 0
RETURN
The subroutine SST_WaitForSPI consists of the following code:
Code:
SST_WaitForSPI:
While SSPSTAT.0 = 0 : WEND
RETURN
I've marked the lines of interest in red in the code above. Here's what's happening:
When executing the code as it's shown above, ie writing the value 170 (101010) directly to the SSPBUF register everything works as expected which is shown in the following screenshot:

As you can see here the ENABLE line is pulled low then the first command (80) in sent. Then there's a pulse on the ENABLE line after which a new command (1) and finally the data (170) is sent. When the last bit is out the ENABLE line goes high. This is all as expected and it works.
The problem comes when instead of writing 170 directly to SSPBUF I assign it to the variable SST_Status and then write THAT to SSPBUF, what's happening then can be seen here:

Here, for whatever reason the ENABLE line goes high BEFORE the data is completely sent. This obviosuly makes the write invalind and not accepted by the device. I admit defeat - I have no idea what's going on.
The SSPSTAT.0 which I poll in the WaitForSPI subroutine indicates when 8 bits have been shifted IN from the slave device. In this particullar case nothing is, or should be comming in, the MISO-line is idling high. But why does it make a difference if I write a constant or a variable to the SSPBUF register? Why does it think it's received 8 bits? The MISO-line idles high in both cases and there are, as far as I can see, no other difference than SSPBUF being given a value either literally or thru a variable.
I've tried other values and it behaves exactly the same - when given the value as a constant it works but "thru" a variable it doesn't.
Any ideas?
/Henrik.
EDIT: And again the bloody editor is messing with the code - it's getting REALLY frustrating posting here. The binary values in the code should be 101010 (170) but the editor apparently decides something else for me and I can't change it.
EDIT2: I can't even write it in the edit comment above it's [Percent sign] [one] [zero] [one] [zero] [one] [zero] [one] [zero].
Bookmarks