Using SPI


Closed Thread
Results 1 to 7 of 7

Thread: Using SPI

  1. #1
    Join Date
    Jun 2007
    Location
    Mansfield, UK
    Posts
    697

    Default Using SPI

    Hi, Is there a good tutorial somewhere that shows how to use hardware SPI on PICs? I know PBP and Assembly so i dont mind if its for either. I've done a lot of googling and i think i understand how SPI works in general and ive seen a software version of it but i cant find any decent tutorials on how to do it with PICs using hardware.

  2. #2
    Join Date
    Oct 2004
    Posts
    440

    Default

    Learn hardware SPI routines:

    1.Read data sheet timing diagrams.

    2.Test with working SHIFTOUT and SHIFTIN program.
    Shiftout is identical to Shout etc.

    3.Bitbang above program and watch on a scope (with additonal delays).
    Code:
    SendData:
    'SHOut sDATA, sCLOCK, msbfirst, [255\1, vDAT\8] 
    
    nop  
    nop
    nop
    nop
    Low sCLOCK
    High sDATA  ' DATA BIT
    nop
    nop
    nop
    nop
    High sCLOCK
    nop
    nop
    nop
    nop
    Low sCLOCK
    
    For z = 7 To 0 Step -1   ' MSB FIRST
    nop
    nop
    nop
    nop
    Low sCLOCK
    e = GetBit vDAT,z 
    If e = 1 Then
    High sDATA
    Else
    Low sDATA
    EndIf
    nop
    nop
    nop
    nop                            
    High sCLOCK
    nop
    nop
    nop
    nop
    Low sCLOCK
    Next  
    
    Return

    4.Now sequence is known. Read PIC 452 data sheet page 130 for your SPI configuration of
    SSPSTAT = %01000000
    SSPCON1 = %00100010


    5. Hardware SPI is simply a 2 way simultaneous byte transfer.
    One of the bytes can be a dummy byte.
    SSPBUF = ySEND_BYTE 'SENDS 1 BYTE TO SD
    While SSPSTAT.0 = 0: Wend 'WHILE TX/RX COMPLETE
    yREAD_BYTE = SSPBUF 'READS 1 BYTE FROM SD

    Norm

  3. #3
    Join Date
    Jun 2007
    Location
    Mansfield, UK
    Posts
    697

    Default

    Thanks for the info. I keep reading that section of the datasheet. It does make more sense each time i read it. There are a few things i would like to check. I want to use SPI for reading data from SD cards. I have the protocol for them and it looks simple enough to use.

    By "bitbang" do you mean the software method where certain pins are turned on/off at the correct time?

    Using that method would i be able to communicate with an SD card really slow? Im thinking less than 1Hz while testing so i can see the data in realtime. I would like to use this idea for testing the hardware version too. The datasheet says the SPI clock can be controlled by timer2 which i could slow down. Would an SD card be alright with such a slow transfer or is there any kind of timeout on them?

    Ive read the bit about SSPSTAT and SSPCON1. The configuration i had is the same as what you showed. I understand what the options mean but do you know which ones i need to work correctly with and SD card?

    I understand #5 but one thing i cant seem to find out (even from the datasheet) is how the clock works. I assume that writing to SSPBUF will cause 8 cycles on the SPI clock pin then it stops instead of cycling constantly.

  4. #4
    Join Date
    Oct 2004
    Posts
    440

    Default

    By "bitbang" do you mean the software method where certain pins are turned on/off at the correct time?
    Yes.

    Using that method would i be able to communicate with an SD card really slow? Im thinking less than 1Hz while testing so i can see the data in realtime. I would like to use this idea for testing the hardware version too. The datasheet says the SPI clock can be controlled by timer2 which i could slow down. Would an SD card be alright with such a slow transfer or is there any kind of timeout on them?
    Slower but not sure how much.

    Ive read the bit about SSPSTAT and SSPCON1. The configuration i had is the same as what you showed. I understand what the options mean but do you know which ones i need to work correctly with and SD card?
    SSPSTAT = %11000000 or SSPSTAT = %01000000 Both work OK.
    I understand #5 but one thing i cant seem to find out (even from the datasheet) is how the clock works. I assume that writing to SSPBUF will cause 8 cycles on the SPI clock pin then it stops instead of cycling constantly.
    Yes. Repeat for Lowbyte and Highbyte.

    Norm

  5. #5
    Join Date
    Jun 2007
    Location
    Mansfield, UK
    Posts
    697

    Default

    Hi, Ive done a lot of reading and it seems that the clock and be slowed down as much as you want which is good for testing. Im getting a little confused by the actual SD protocol though. Different sites are saying different things.

    I have some example code in C. I dont completely understand C but it looks like its saying things should happen in this order:-
    Enable the SPI interface
    Send 80 clocks using dummy bytes (0xFF)
    Enable CS (low)
    Send another dummy byte
    Send the init command (0x40, 0x00, 0x00, 0x00, 0x00, 0x95)
    Repeatedly send a dummy byte until the card replies

    Another site says the CS pin should be asserted before sending the 80 clocks. This is confusing as the pin is active low. It did read as though "assert" means low but as this C example seems to be the oposite i assume it means high.

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924

    Default

    For SD card go here and look at SDFS3.zip
    http://www.melabs.com/resources/samples.htm
    It is so much easier than bit-banging. Added plus is the FAT16 file system.
    Dave
    Always wear safety glasses while programming.

  7. #7
    Join Date
    Jun 2007
    Location
    Mansfield, UK
    Posts
    697

    Default

    Hi, Since my last post Ive been doing some testing and i actually got a PIC to read data

    Ive been using the hardware SPI instead of bit-banging. Thanks for that link though because the next thing i need to do is figure out FAT16. Im working in ASM but having the example in PBP makes it easier to read :P Thanks!

Similar Threads

  1. Using SPI with External Interrupts
    By shaiqbashir in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 29th June 2008, 04:08
  2. 16-bit SPI problem
    By shaiqbashir in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 19th June 2008, 15:42
  3. SPI Interfacing
    By toofastdave in forum Serial
    Replies: 8
    Last Post: - 18th November 2007, 11:15
  4. SPI configuration PIC versus Atmel
    By Pedro Santos in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 10th July 2007, 19:17
  5. How to configure SPI in PICBASIC PRO?
    By moogle in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 15th April 2007, 18:31

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

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