Using SPI


Closed Thread
Results 1 to 7 of 7

Thread: Using SPI

Hybrid View

  1. #1
    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

  2. #2
    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.

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