Using harware SPI


+ Reply to Thread
Results 1 to 6 of 6

Thread: Using harware SPI1001 days old

  1. #1
    Senior Member SimonJA's Avatar
    Join Date
    Mar 2006
    Posts
    827
    Thumbs Up
    Received: 14
    Given: 16
    Total Downloaded
    889.46 MB

    0 Not allowed!

    Default Using harware SPI

    I am using an external 16 bit ADC and at the moment I am using a very simple software routine to toggle the clock and then read in the state of the data out line to set the bits of a word variable.

    This does work ok but I am trying to eliminate some errors which are only small and may just be noise but I want to be sure that I am reading the data correctly in the first place before doing that.

    I would rather use hardware SPI but I'm not quite sure how to do that, I have used the SPI include routines (SPI_24.inc) that Les has written for sending out data which is fine but I'm not sure about reading data in.

    There is a Read_SPI procedure but that just seems to retrieve the contents of the buffer as far as I can tell and I'm not sure how the data is clocked into the buffer to start with. I can't see any examples of reading in data so would appreciate any help.

Attention

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

The advertisements we display are relevant to this web site and your browsing history

Please consider supporting us by disabling your ad blocker.


Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues


Thank you for your attention.

  • #2
    Senior Member barak's Avatar
    Join Date
    Apr 2008
    Posts
    724
    Thumbs Up
    Received: 30
    Given: 11
    Total Downloaded
    1.45 GB

    0 Not allowed!

    Default Re: Using harware SPI

    Example:
    Code:
    SPI_WR:       
        Pop SSPBUF
        While SSPIF = 0 : Wend  ' wait for end of transaction
        SSPIF = 0               ' clear the SSP flag
        Return SSPBUF       
        
    yourcode:
        While 1 = 1
            GoSub SPI_WR [writebyte], readbyte  ' send writebyte and recieve readbyte
            'do something with readbyte
        Wend

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #3
    Senior Member SimonJA's Avatar
    Join Date
    Mar 2006
    Posts
    827
    Thumbs Up
    Received: 14
    Given: 16
    Total Downloaded
    889.46 MB

    0 Not allowed!

    Default Re: Using harware SPI

    I really need a bit of an explanation in simple terms.

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #4
    Senior Member Stephen Moss's Avatar
    Join Date
    Jan 2006
    Posts
    505
    Thumbs Up
    Received: 30
    Given: 7
    Total Downloaded
    3.72 GB

    0 Not allowed!

    Default Re: Using harware SPI

    Obviously you will have to set up the SPI related registers for it operate the way you want it, but after that the following has worked for me in the past on an 18F2410, it may be different on PIC24 but if you can't use it directly it might point in the right direction.
    Code:
    SSPBUF = Send_data Rev 8  'Send outgoing data to SSP Buffer (also initiates transmission/reception)
    Repeat : Until BF = 1     'Wait until Buffer full Status Flag is set (incoming data received)
    X = SSPBUF Rev 8           'Move received data to a variable for manipulation
    If I recall correctly the SPI buffer transmits data on one clock edge and receives data on the other (transmit/receive are essentially simultaneous) as set in the related registers, in the device I mentioned the outgoing data is sent from the SSPBUF MSB first. Harms example was waiting for the SSP interrupt to indicate data reception which is an alternate way of knowing if data has been received but in my application I did not need the PIC to do other things during transmission which is why I just polled the BF status.

    As the requirements for the device I was communicating with were send/receive data LSB first I hade to use the Rev command (Rev is now #, I think) to get the LSB of the outgoing data into the MSB of SSPBUF for transmission and the LSB of the recieved data from the MSB of SSPBUF to the LSB of the variable as the SSPBUF on the device used was basically an 8 bit serial FIFO buffer.
    Last edited by Stephen Moss; 24th February 2017 at 12:49.

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #5
    Senior Member SimonJA's Avatar
    Join Date
    Mar 2006
    Posts
    827
    Thumbs Up
    Received: 14
    Given: 16
    Total Downloaded
    889.46 MB

    0 Not allowed!

    Default Re: Using harware SPI

    What I didn't understand was what was clocking the data in as there didn't seem to be anything doing that.

    If I understand correctly sending out data is what toggles the clock and reads in the data at the same time.

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #6
    Senior Member barak's Avatar
    Join Date
    Apr 2008
    Posts
    724
    Thumbs Up
    Received: 30
    Given: 11
    Total Downloaded
    1.45 GB

    0 Not allowed!

    Default Re: Using harware SPI

    Correct. As soon as you put data in the buffer (SSBUF) it is being clocked out and "simultaneously" the data from the slave is clocked into the buffer.

  • Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

    Thread Information

    Users Browsing this Thread

    There are currently 1 users browsing this thread. (0 members and 1 guests)

       

    Similar Threads

    1. SPI - (Can't belive a search on SPI yields no results?)
      By Peter Truman in forum Proton Plus Compiler v3
      Replies: 12
      Last Post: 30th September 2012, 10:12
    2. Harware AND software rs232 at the same time? any issues?
      By Kapitan in forum Proton Plus Compiler v3
      Replies: 4
      Last Post: 11th January 2009, 01:13
    3. SD and second SPI
      By gtv_pic in forum SWright's Feedback Forum
      Replies: 0
      Last Post: 14th January 2008, 00:05
    4. Spi
      By nuclear__ in forum Proton Plus Compiler v3
      Replies: 14
      Last Post: 7th September 2007, 07:51
    5. Spi
      By dmTulsa in forum Proton Plus Compiler v3
      Replies: 1
      Last Post: 6th July 2006, 20:38

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts