How can I speed this code up? SHIFTOUT is slowing it down and I need a faster way.


Closed Thread
Results 1 to 30 of 30

Hybrid View

  1. #1
    sinoteq's Avatar
    sinoteq Guest


    Did you find this post helpful? Yes | No

    Default Hpwm pbp

    Hi
    @30Mhz you can get way faster HPWM from the chip BUT you can NOT use the HPWM command in PBP. You need to get inside the car and drive yourself. Set up the HPWM module manually, examples on Page 87 in the datasheet Table 9-3

    Hope this helps

  2. #2
    Join Date
    Mar 2010
    Location
    Minnesota, USA
    Posts
    41


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sinoteq View Post
    Hi
    @30Mhz you can get way faster HPWM from the chip BUT you can NOT use the HPWM command in PBP. You need to get inside the car and drive yourself. Set up the HPWM module manually, examples on Page 87 in the datasheet Table 9-3

    Hope this helps
    HAHA well sure I could use the formula to find out if it will be fast enough but one needs to know what "log, log2 and bits" are first. I wish these manuals would explain the formulas a little better.

    How fast is "way faster"? keep in mind the 16f88 will be running at 20MHz but the external chip I am interfacing with can be driven with up to 30MHz.

    See I was thinking the fastest way would be to use some simple ASM to turn a Pin HIGH then turn Pin LOW and was thinking this could possibly achieve maybe a 5MHz signal.

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


    Did you find this post helpful? Yes | No

    Default

    Dave
    Always wear safety glasses while programming.

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default

    Hi,
    I think that the highest frequency you can get out of the CCP module is 2.5Mhz when the chip running at 20Mhz. Even so I don't think it's going to do what you want since there's no easy way to control the number of cycles or "pulses" that is going out and as far I understand you need exactly 4096 "pulses".

    Perhaps it's possible to connect the CCP output back into a counter input and set that up to generate an interrupt which stops the CCP module at the right spot but it seems like a long shot.

    I'm definetly no "ASM-guy" but to set and reset a pin (or rather the port latch) in ASM you can do:
    Code:
    @ BSF PORTA, 3
    @ BCF PORTA, 3
    But even if you put 4096 of those in a row you won't get more that 2.5Mhz because each instruction takes one cycle and one cycle is Fosc/4 meaning 200ns per cycle @20MHz so the frequency would be 1/400ns=2.5Mhz. Not to mention you'd fill up the flash memory of the 16F88....

    How about this, I THINK it should run faster than your For i = 0 to 4095 loop.
    Code:
    i VAR BYTE
    j VAR BYTE
    For i = 1 to 16      '16*256=4096
      For j = 0 to 255
    @  BSF PORTA, 3
    @  BCF PORTA, 3
      NEXT
    NEXT
    If I'm right that inner loop seems to execute in 7 cycles and the outer loop seems to take another 12 for a total of (7*256*16)+(12*16)=28864 cycles @ 200ns each. A total of 5772.8us for 4096 pulses or an average frequency of ~709kHz. There will be some jitter in the pulsestream when it goes from the inner to outer loop but I guess it doesn't matter(?)

    /Henrik.

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Here's a neat trick to get 1MHz with a 20MHz oscillator. This takes 5 instruction cycles to toggle the pin, and Timer1 keeps track of the toggle count for you.

    You don't need to use any incrementing or decremeting loops or variables.

    1. Set T1CKI pin, and make the pin an output.
    2. Load Timer1 low & high registers with 65,536 - the number of clocks to output & count.
    3. Setup Timer1 for external clock, 1:1 prescaler, and turn it on.

    T1CKI outputs your clock while Timer1 count increments on every low-to-high transition on T1CKI.

    Code:
    PORTC.0 = 1       ' set pin so 1st low-to-high increments count
    TRISC.0 = 0       ' make pin an output
     
    Main:
      TMR1H = $F0    ' 65,536 - 4096 = 61,440 = $F000
      TMR1L = $00    ' so 4096 toggles will set TMR1IF
      T1CON = %00000011 ' 1:1 prescaler, external clock, Timer1 on
     
    ASM
    Pulse
        BCF PORTC,0       ; clear T1CKI pin
        BSF PORTC,0       ; set T1CKI pin
        BTFSS PIR1,TMR1IF ; when TMR1 overflows, count is complete
        GOTO Pulse        ; loop until Timer1 overflow
        BCF  PIR1,TMR1IF  ; clear TMR1 overflow flag bit
    ENDASM
    When PIR1,TMR1IF = 1 you have 4096 clocks. I tested this on a 16F877A, so just change the T1CKI pin to whatever it is on your PIC type.
    Last edited by Bruce; - 18th May 2010 at 16:52. Reason: A better way
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default

    Son of a gun, that works (please note that T0CKI is RB6 on a 16F88).

    The three cycle loop 'overhead' (BTFSS & GOTO) is a bottleneck. You could get better performance if you spread it out over more pulses. If you were to produce 16 pulses within the loop then you could use a single byte variable counter instead of Timer 1 and bump the output up to 2+ MHz.

    Regards, Mike
    Last edited by Mike, K8LH; - 18th May 2010 at 08:13.

  7. #7
    Join Date
    Mar 2010
    Location
    Minnesota, USA
    Posts
    41


    Did you find this post helpful? Yes | No

    Default

    Thank You Bruce for the code!!! I will try to get it loaded and tested tonight.

    Quote Originally Posted by Mike, K8LH View Post
    Son of a gun, that works (please note that T0CKI is RB6 on a 16F88).

    The three cycle loop 'overhead' (BTFSS & GOTO) is a bottleneck. You could get better performance if you spread it out over more pulses. If you were to produce 16 pulses within the loop then you could use a single byte variable counter instead of Timer 1 and bump the output up to 2+ MHz.

    Regards, Mike
    Mike are you saying Use a FOR Loop ouside of the ASM like this or is there an ASM FOR Loop I should be using for it (Keep in mind I dont know ASM):
    Code:
    PORTB.6 = 1       ' set pin so 1st low-to-high increments count
    TRISB.6 = 0       ' make pin an output
    T1CON = %00000011 ' 1:1 prescaler, external clock, Timer1 on
    C1 VAR BYTE
     
    FOR C1 = 0 to 255
    ASM
    Pulse
        BCF PORTB,6       ; clear T1CKI pin / 1
        BSF PORTB,6       ; set T1CKI pin
        BCF PORTB,6       ; clear T1CKI pin / 2
        BSF PORTB,6       ; set T1CKI pin
        BCF PORTB,6       ; clear T1CKI pin / 3
        BSF PORTB,6       ; set T1CKI pin
        BCF PORTB,6       ; clear T1CKI pin / 4
        BSF PORTB,6       ; set T1CKI pin
        BCF PORTB,6       ; clear T1CKI pin / 5
        BSF PORTB,6       ; set T1CKI pin
        BCF PORTB,6       ; clear T1CKI pin / 6
        BSF PORTB,6       ; set T1CKI pin
        BCF PORTB,6       ; clear T1CKI pin / 7
        BSF PORTB,6       ; set T1CKI pin
        BCF PORTB,6       ; clear T1CKI pin / 8
        BSF PORTB,6       ; set T1CKI pin
        BCF PORTB,6       ; clear T1CKI pin / 9
        BSF PORTB,6       ; set T1CKI pin
        BCF PORTB,6       ; clear T1CKI pin / 10
        BSF PORTB,6       ; set T1CKI pin
        BCF PORTB,6       ; clear T1CKI pin / 11
        BSF PORTB,6       ; set T1CKI pin
        BCF PORTB,6       ; clear T1CKI pin / 12
        BSF PORTB,6       ; set T1CKI pin
        BCF PORTB,6       ; clear T1CKI pin / 13
        BSF PORTB,6       ; set T1CKI pin
        BCF PORTB,6       ; clear T1CKI pin / 14
        BSF PORTB,6       ; set T1CKI pin
        BCF PORTB,6       ; clear T1CKI pin / 15
        BSF PORTB,6       ; set T1CKI pin
        BCF PORTB,6       ; clear T1CKI pin / 16
        BSF PORTB,6       ; set T1CKI pin
    ENDASM
    NEXT

Members who have read this thread : 0

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