Speeding up the PIC SPI


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2004
    Location
    nebraska
    Posts
    79

    Default Speeding up the PIC SPI

    Hello,

    I am trying to speed up my project and make it more efficient. I am using a Pic18F4585 for testing purposes and will switch to a 18F8722 for my final project. Anyways, my pic is hooked up to a RS-485 Modbus network. I am receiving data via the usart using hardware and not software. I think this part of the code is running as efficiently as possible using PBP and On Interrupts. Eventually I would like to figure out Darrels Instant Interrupts but not right now. Forgive me if I butchered that.
    So right now I have a MCP3202 hooked up and I am using Shiftout and ShiftIn commands to talk to it. My question is, would using the hardware SPI save me a lot of processor time? Also I see that there is 4 different SPI modes, do any of these modes allow multiple devices on the buss like I2C?
    Here is my test code for interfacing the MCP3202.

    Include "modedefs.bas" ' Include serial modes
    DEFINE OSC 10 ' Define clock freq. 20 MHz

    TrisB = 0

    CS Var PortC.0 ' Chip Select (MCP3202.1).
    Clock Var PortC.1 ' Clock (MCP3202.7).
    DataIn VAR PortC.2 ' --> Data Out (MCP3202.6).
    DataOut Var PortC.5 ' <-- Data In (MCP3202.5).
    LCD Var portC.3 ' LCD Data debuging pin.
    Result0 Var Word ' ADC result


    PortB.0 = 0
    Pause 500 'Wait for LCD
    serout LCD,6,[12,"MCP3202AD"]
    Pause 4000

    Main:
    PortB.0 = 1
    LOW CS ' Enable ADC
    SHIFTOUT DataOut, Clock, MSBFIRST, [%1101\4] ' Select CH0, Single-Ended
    SHIFTIN DataIn, Clock, MSBPOST, [result0\12] ' Read ADC
    HIGH CS ' Disable ADC
    PortB.0 = 0
    serout2 LCD,16468,[12,Dec Result0]

    goto Main

    I hooked a scope up to Portb.0 and found the HIGH time to only be 200uS. That doesn't seem to be much time at all but I was reading another post and the guy said that by using Hardware SPI it would be up to 10x faster.

    Also I hooked up a 20Mhz crystal and the time went down to 100uS. This tells me that almost all the time is spent in the statements and not the sending of data. I did not think changing the crystal would cut the time in half.

    If I looked at the low part of PortB on the scope 10Mhz was 5mS and 20Mhz was 4.6mS. This find kind of made sense since it takes almost 1mS to transfer a byte at 9600K.

    Sorry for rambling my brain kind of goes a hundred different ways while doing stuff. If anyone could post some links to hardware SPI samples, code, or explanation it would be much appreciated. Also is it worth using the hardware or is that 10x faster not relevant since I am not sending much data out or receiving much data in.

    Ok since I'm rambling, what do ya all think of this logic. My thought is that a pic running at 20Mhz can in a sense execute 5 million instructions a second because of the whole 4 clock cycles per instruction thing. So this means that an instruction is executed every 200nS. Am I close so far. If it takes 100uS to execute the shiftOut and ShiftIn statements in the above program, it basically took 500 instructions to accomplish this, minus the actual time to clock out the 4 bits of data and clock in the 12 bits of data.

    That leads me to my final question for now. What baudrate does the shiftout and shiftin commands use? Ok just read 50kHZ. Can hardware SPI run faster.

    Thanks in Advance
    AND WAKE UP
    Shawn

  2. #2
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Speeding up the PIC SPI

    shawn , If you read the data sheet for the processor you are using, it normally refers to FOSC/4 as the fastest speed for the hardware SPI function. This is MUCH faster than using the software routines within PBP. The advantage of the routines is you can select ANY port pin to be used for the function as well as have the function available even if the particular processor does not have a SPI module.

    Dave Purola,
    N8NTA

  3. #3
    Join Date
    Oct 2005
    Location
    Loveland CO USA
    Posts
    83


    Did you find this post helpful? Yes | No

    Default Re: Speeding up the PIC SPI

    I think the 18f4585 can run at 40mhz with the PLL on. The SPI should run at 10mhz.

  4. #4
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    637


    Did you find this post helpful? Yes | No

    Default Re: Speeding up the PIC SPI

    Shawn,

    Here is a link for a post with some code for a hardware SPI. I haven't tried it yet but it is definitely in my to do list. If you try this code please let me know if you see an improvement in communication speed.

    http://www.picbasic.co.uk/forum/show...5442#post95442
    "No one is completely worthless. They can always serve as a bad example."

    Anonymous

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