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


Results 1 to 30 of 30

Threaded View

  1. #26
    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 15:52. Reason: A better way
    Regards,

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

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