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.
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.
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
movf SSPBUF,W ;read sspbuf register
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.