SPI, SCK Pin set up


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2007
    Posts
    59

    Default SPI, SCK Pin set up

    I'm using a slave module on a project and so I need to tie my SCK pin on my pic (18F6520) to the other device. I was using the pin originally as a standard variable port.

    I was looking through the PicBasic Pro Compiler manual and cannot find any instructions on how to set up this pin to use as its serial clock pin.

    Can someone help me out?

    Thanks,
    Hylan

  2. #2
    Join Date
    Sep 2007
    Posts
    59


    Did you find this post helpful? Yes | No

    Default Re: SPI, SCK Pin set up

    Also, just found some info on the slave unit. It says it uses (mode 1) for clock phase and polarity. So the SCKline idles high and data is setup on the falling edge of the clock and latched on the rising edge.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: SPI, SCK Pin set up

    Hi,
    The PBP command SHIFTOUT can only work in master mode, ie the PIC is what "drives" the communications and therefor provides the clock. Is that what you're trying to do or is your external device providing the clock which the PIC is supposed to "use"?

    Many PICs have an MSSP module which is much like an USART but for syncronous type commuinications like SPI and I2C. It can operate in either master or slave mode but PBP does not have any built in commads to handle it - you'd need to drive it "manually".

    Please elaborate a bit and I'm sure someone will come to the rescue.

    /Henrik.

  4. #4
    Join Date
    Sep 2007
    Posts
    59


    Did you find this post helpful? Yes | No

    Default Re: SPI, SCK Pin set up

    Henrik,
    Yes, the pic will be the master and the other device will be the slave. I'm hoping someone has worked this out and can explain how to do this.
    Hylan.

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: SPI, SCK Pin set up

    In that case you should be able to use either the bit-banged routines (SHIFTIN/SHIFTOUT depending on which direction the data is going) or the hardware MSSP module. PBP does not have any built in commads for handling the MSSP module but it's not that hard to do "manually". Have a look at the SPIMAST.pbp example on the examples pages at MELABS.

    SHIFTIN/SHIFTOUT, being bit-banged, works on "any" pins. You just set you TRIS registers normally and make sure that comparators and ADC are turned off if they happen to be on the same pin. (Actually, PBP may handle the TRIS bit but I'm not sure.)

    /Henrik.
    Last edited by HenrikOlsson; - 25th December 2011 at 09:53.

  6. #6
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: SPI, SCK Pin set up

    I just finished getting this going for a project. You do need to set the TRIS registers. Note the data output of the master goes to the data input of the slave and vice versa, and the chip enable of the slave needs to be addressed as well. Finally you write to the SSPBUF, give it enough time for the clocking to have taken place, then read the reply from the SSPBUF. Don't forget to clear flags and interupts before moving on. My biggest pain was getting the clock edges, idles, and data positions right. After a few hours of cut and try frustration, I sat down with the timing diagrams in the datasheets and then it worked first try. When all else fails... RTFM, LOL.

  7. #7
    Join Date
    Sep 2007
    Posts
    59


    Did you find this post helpful? Yes | No

    Default Re: SPI, SCK Pin set up

    So I'm finally back home and somewhat on a regular schedule and digging back into this. I like the idea of using the shiftin/shiftout command as I could use existing pins and maybe be able to use an existing board. However, for the proof of concept I'm first trying to do it with the least number of variables. I've been looking at the SPIMAST.pbp sample as Henrik suggested and have identified the SCK pin (portC.3), the SDI pin (portC.4), the SDO pin (portC.5) and I've assigned my 'active low chip select' output to B.0.

    I have changed my TRISB & C settings accordingly.

    Now I'm starting to look at the sample code and figure out how to incorporate this into my code and how much of it I will need if I use the Shiftin/Shiftout. I don't understand these settings:

    SSPEN VAR SSPCON.5 ' SSP Enable bit
    CKP VAR SSPCON.4 ' Clock Polarity Select
    SMP VAR SSPSTAT.7 ' Data input sample phase
    CKE VAR SSPSTAT.6 ' Clock Edge Select bit
    SSPIF VAR PIR1.3 ' SPI interrupt flag

    I can't find any information on what these settings mean and how to make sure I have them right for my set up. I've been looking in the compiler manual and can't find anything.
    Once I have everything setup. What I want to do is fairly simple. When a switch on a different port changes, I will write 16 bytes of data (actually 2 bytes over and over, 8 times) to the slave and then read back 16 bytes. I don’t need an interrupt or anything fancy.
    Is there some more documentation on the SPI or other help is appreciated.
    Thank you,
    Hylan

  8. #8
    Join Date
    Sep 2007
    Posts
    59


    Did you find this post helpful? Yes | No

    Default Re: SPI, SCK Pin set up

    I was doing some more reading on the shiftin/shiftout on the forum and it looks like that replaces the hardware pins and you can’t use them together. Since I don’t need interrupts, it looks like this might be easier and I don’t have to configure as much.
    Any words of advice are appreciated.
    Thank you,
    Hylan

  9. #9
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: SPI, SCK Pin set up

    Hi,
    The datasheet have quite a bit of information on the MSSP module, have a look there.

    If you're going to use SHIFTIN/SHIFTOUT you don't need any of the code for MSSP module. The "settings" you talk about depends on the slave device you're going to communicate with. The CKP, SMP & CKE bits controls how the MSSP module operates, much like the MODE parameter for the SHIFTIN/SHIFTOUT command does, there's info in the datasheet for the PIC and there should be info in the datasheet for your slave device as to which mode it uses - the PIC needs to be setup to match the slave device or the data may being sent/received may be misinterpreted at either end.

    By the way, the actual lines you posted just creates aliases to individual bits in the registers so you can write CKE=1 instead of SSPSTAT.6=1 etc. If you look at the SSPSTAT register in the datasheet you'll see that bit 6 is called CKE and you'll also see what it does when either set or reset.

    With the MSSP module you MUST use the designated pins (RC3,4,5 in this case). With SHIFTOUT you can use (almost) any pins you wish since it is a bit-banged firmware routine and doesn't use the hardware peripheral provided by the PIC. SHIFTOUT might be easier to get going but it's kind of slow, the MSSP module can run spit out the bits ~20 times faster... You can definitely use the MSSP module and SHIFTOUT in the same program, just not on the SAME pins - at the same time....but again, MSSP module only on RC3-5, SHIFTOUT on "any" pin since it doesn't USE the MSSP-module.

    /Henrik.

Members who have read this thread : 1

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

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