New to SPI, some questions

Discussion in 'Embedded Systems and Microcontrollers' started by wannaBinventor, Aug 30, 2010.

  1. wannaBinventor

    Thread Starter Member

    Apr 8, 2010
    179
    4
    I also posted this on Microchip's forum, but I wanted to see what response I'd get here:

    I've got a PIC16F1934 that I'm planning to use to communicate with an MRF49XA radio transceiver using the built in SPI.

    Data sheet links:
    PIC16F1934
    http://ww1.microchip.com/downloads/en/DeviceDoc/41364D.pdf
    MRF49XA
    http://ww1.microchip.com/downloads/en/DeviceDoc/70590b.pdf

    From the 16F1934 datasheet, it appears that data written to the SSPBUF register is mirrored in the SSPSREG (shift register) register and then clocked into the MRF49XA upon selecting it and enabling the SPI by setting the SSPEN bit of the SSPCON1 register. The datasheet says that an interrupt flag, SSPIF, and Buffer Full bit are both set in the SSPSTAT register when 8 bytes are received. So, I'd imagine I'd just test for this flag, read any data in the SSPBUF register into a user register, and then rewrite it with the next byte I need to transmit. Is this correct? Will writing a new byte ti the SSPBUF require me to disable the SPI and re-enable it. Would this disrupt the clock cycles that the save is expecting?

    Here's how I'm envisioning the code if some1 can confirm my thinking please:

    Code ( (Unknown Language)):
    1.  
    2.  
    3. ;;;code to transmit a 10101010 and then a 11111111 byte over SPI
    4.  
    5. MOVLW  b'10101010
    6. MOVWF  SSPBUF
    7. BSF    PORTB,0      ;select the slave
    8. BSF    SSPCON1,SSPEN    ; enable the SPI
    9. waitloop
    10. BTFSS   SSPSTAT,BF    ; test the bit full bit to see if 8 bytes have been clocked in
    11. GOTO   waitloop
    12. MOVF     SSPBUF,w     ;move SSPBUF register into W
    13. MOVWF  RECVD1        ; move what was in the SSPBUF into user file RECVD1
    14. MOVLW  b'11111111  
    15. MOVWF  SSPBUF         ;load above literal into SSPBUF
    16. waitloop2
    17. BTFSS  SSPSTAT,BF     ;test the bit full bit to see if 8 bits have been clocked in
    18. GOTO  waitloop2        ; dont proceed if 8 bits have bit been clocked in
    19. MOVF   SSPBUF,w      
    20. MOVWF  RECVD1      ;move what was just in SSPBUF into user file RECVD1
    21. .
    22. .
    23. .
    24. end
    Is the above more or less correct code?
    Should I expect the MRF49XA (slave) to always be shifting something into the PIC's SDI whenever I'm clocking data out of the SDO?

    Thanks for the help.
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,789
    The MRF49XA will always be shifting data back to the PIC whenever the PIC is shifting data to the device. Consult the datsheet to see if the data is meaningful in a particular context. As I said before this is an inherent feature of the SPI protocol
     
  3. eblc1388

    Senior Member

    Nov 28, 2008
    1,542
    102
    To correctly communicate with a SPI slave, you must also take into account the polarity of the SPI clocking mode.

    Pick the one (out of 4 possible modes) that the Slave is designed to work with.
     
Loading...