Continue to Site

# SPI simulation help

Status
Not open for further replies.

#### bobtm

##### New Member
I’ve been trying to learn assembly for the pic with MPLAB and simulating the code. Right now I am trying to understand SPI. Let’s assume the ports are set up correctly and we are trying to send a single byte out from the PIC to a slave.

Code:
		bcf		SS		;enable chip select output low
movlw		0x07		;put in w
movwf		SSPBUF		;put in SSPBUFD
BANKSEL		SSPSTAT		;BANK1
Char1		btfss		SSPSTAT,BF	;data transfer complete?
goto		Char1
BANKSEL		SSPBUF		;BANK0
bsf		SS		;disable chip select high

I would like to know if I understand what is going on correctly.

SS is set low to tell the slave data is about to transfer.
The byte is sent to sspbuf and then sent automatically out to the slave.
The code waits for the sspstat buffer full flag.

At this point the slave sends data back to the master. During simulation the code is in an infinite loop. I was stuck on this for two days and finally the light started to glow. Is the best way to simulate this is to use register injection and send a byte to sspbuf on demand to simulate what the slave would do in a real circuit?

The sspbuf is read and this clears the BF flag.
SS is set high to indicate the transfer is completed.

More SPI questions.

The datasheet for the slave device I’m looking at states, data is clocked out of the master in slave out (MISO) pin on the falling edge of the SCLK signal. Does this mean that you have to toggle SS after the MOSI data to initiate the MISO data? Do you check the BF flag after toggling SS?

I would like to be able to send multiple bytes to initiate a function on the slave, and read multiple bytes from the slave. Any hints on how to do this in assemble and simulate using MPLAB would be appreciated.

#### Oznog

##### Active Member
I found SPI to be a somewhat poorly described module in the documentation.
Honestly? Best thing is to hook up the SPI pins to other PIC pins, use ICD, and step through your questions and see what ends up in the SPI buffer reg and what signals show up on the pins when. If you use an external SPI clock, then there's no speed requirement on the code that pulses the pins tied to the SPI pins.

It does yield some definitive results if you've got very particular questions.

Last edited:

#### bobtm

##### New Member
Wp100, Thanks for the comment and link. I've seen it and it is somewhat helpful

Oznog, Poorly described to say the least. I guess I will have to write some code and try it and educate myself on ICD.

Thanks again, Steve

Status
Not open for further replies.

Replies
4
Views
744
Replies
8
Views
838
Replies
0
Views
1K
Replies
10
Views
949
Replies
0
Views
655