Fast Loop in PICBASIC Pro


Closed Thread
Results 1 to 12 of 12

Hybrid View

  1. #1
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    I agree with Steve (and everyone else) that the hardware PWM sounds best for what you described. But, if it will not work for you consider "unrolling" your loops like that shown below. You lose no time in looping but it does cost you more code space (16 repeats is not huge considering the amount of memory in the 877A).
    Code:
    DEFINE OSC 20
    TRISB.0 = 0
    PORTB = 0
    
    PORTB.0 = 1   ' I = 1 (on for 12.6 uS)
    @ nop
    Pauseus 12
    PORTB.0 = 0   ' (off for 12.4 uS)     
    PAUSEUS 12
    
    PORTB.0 = 1   ' I = 2
    @ nop
    Pauseus 12
    PORTB.0 = 0     
    PAUSEUS 12
    
    ;(more of the same here)
    
    PORTB.0 = 1   ' I = 16
    @ nop
    Pauseus 12
    PORTB.0 = 0     
    PAUSEUS 12
    
    Endhere:
    goto Endhere
    END
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    could be...
    Code:
    DEFINE OSC 20
    TRISB.0 = 0
    PORTB = 0
    pauseus 0 ' do nothing good but include PAUSEUS?C macro
    TRISB.0 = 0
    PORTB = 0
    start:
    asm
        CHK?RP PORTB
        local a=0
        while a<16        
            BSF PORTB,0
            nop
            PAUSEUS?C 12
            BCF PORTB,0
            nop
            PAUSEUS?C 12
    a+=1
        endw
    endasm
    pause 100
    goto start
    now play with the a < x value, and look what happen to your code size.. interesting eh?

    Now compare the code size of
    Code:
    PORTB.0 = 1   
    @ nop
    Pauseus 12
    PORTB.0 = 0     
    @ nop
    PAUSEUS 12
    copy and pasted 16 times, with the one generated with my solution... mmm interesting

    Sometime MPASM directive are really handy!

    EDIT: you could also use
    Code:
    @   local a=0
    @   while a<16
            PORTB.0=1
            @ nop
            PAUSEUS 12
            PORTB.0=0
            @ nop
            PAUSEUS 12
    @a+=1
    @       endw
    If you don't like the PAUSE?C, BSF, BCF option... both generate the same code.
    Last edited by mister_e; - 18th December 2006 at 06:03.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Steve, I did compare code space

    171 for 16X of my block (MPASM)

    211 for your code as posted
    191 (with the second nop removed from yours - which makes T = 25 uS)

    it is interesting.

    Now, the question for me is how the ASM while directive checks without adding time???

    EDIT (after looking up while) - I see .... it doesn't check .... now I understand your comment about code space and it is a handy directive :-) THANKS
    Last edited by paul borgmeier; - 18th December 2006 at 06:12.
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    MIIP, there's something weird... if i use a 16F877 and this....
    Code:
    DEFINE OSC 20
    DEFINE LOADER_USED 1
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
    DEFINE HSER_SPBRG 129 ' 9600 Baud @ 0.16%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    
    
    pauseus 0
    TRISB.0 = 0
    PORTB = 0
    start:
    asm
        CHK?RP PORTB
        local a=0
        while a<16        
            BSF PORTB,0
            nop
            PAUSEUS?C 12
            BCF PORTB,0
            nop
            PAUSEUS?C 12
    a+=1
        endw
    endasm
    
    pause 100
    goto start
    it compile 207 Word

    if i use...
    Code:
    DEFINE OSC 20
    DEFINE LOADER_USED 1
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
    DEFINE HSER_SPBRG 129 ' 9600 Baud @ 0.16%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    
    
    pauseus 0
    TRISB.0 = 0
    PORTB = 0
    start:
        ' #1
        PORTB.0 = 1   
        @ nop
        Pauseus 12
        PORTB.0 = 0     
        @ nop
        PAUSEUS 12
    
        ' #2
        PORTB.0 = 1   
        @ nop
        Pauseus 12
        PORTB.0 = 0     
        @ nop
        PAUSEUS 12
    
        ' #3
        PORTB.0 = 1   
        @ nop
        Pauseus 12
        PORTB.0 = 0     
        @ nop
        PAUSEUS 12
    
        ' #4
        PORTB.0 = 1   
        @ nop
        Pauseus 12
        PORTB.0 = 0     
        @ nop
        PAUSEUS 12
    
        ' #5
        PORTB.0 = 1   
        @ nop
        Pauseus 12
        PORTB.0 = 0     
        @ nop
        PAUSEUS 12
    
        ' #6
        PORTB.0 = 1   
        @ nop
        Pauseus 12
        PORTB.0 = 0     
        @ nop
        PAUSEUS 12
    
        ' #7
        PORTB.0 = 1   
        @ nop
        Pauseus 12
        PORTB.0 = 0     
        @ nop
        PAUSEUS 12
    
        ' #8
        PORTB.0 = 1   
        @ nop
        Pauseus 12
        PORTB.0 = 0     
        @ nop
        PAUSEUS 12
    
        ' #9
        PORTB.0 = 1   
        @ nop
        Pauseus 12
        PORTB.0 = 0     
        @ nop
        PAUSEUS 12
    
        ' #10
        PORTB.0 = 1   
        @ nop
        Pauseus 12
        PORTB.0 = 0     
        @ nop
        PAUSEUS 12
    
        ' #11
        PORTB.0 = 1   
        @ nop
        Pauseus 12
        PORTB.0 = 0     
        @ nop
        PAUSEUS 12
    
        ' #12
        PORTB.0 = 1   
        @ nop
        Pauseus 12
        PORTB.0 = 0     
        @ nop
        PAUSEUS 12
    
        ' #13
        PORTB.0 = 1   
        @ nop
        Pauseus 12
        PORTB.0 = 0     
        @ nop
        PAUSEUS 12
    
        ' #14
        PORTB.0 = 1   
        @ nop
        Pauseus 12
        PORTB.0 = 0     
        @ nop
        PAUSEUS 12
    
        ' #15
        PORTB.0 = 1   
        @ nop
        Pauseus 12
        PORTB.0 = 0     
        @ nop
        PAUSEUS 12
    
        ' #16
        PORTB.0 = 1   
        @ nop
        Pauseus 12
        PORTB.0 = 0     
        @ nop
        PAUSEUS 12
    
    pause 100
    goto start
    Still 207 words...

    EDIT: OK DUDE we hit the button at the same time or...so.
    Last edited by mister_e; - 18th December 2006 at 06:19.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Controlsystem for compact sporting (clay shooting)
    By Fredrick in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 30th July 2009, 17:48
  2. Serial Relays
    By tazntex in forum General
    Replies: 3
    Last Post: - 17th May 2007, 18:42
  3. How to configure SPI in PICBASIC PRO?
    By moogle in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 15th April 2007, 19:31
  4. Question for all that use MELABS PICBASIC PRO
    By oskuro in forum Off Topic
    Replies: 2
    Last Post: - 24th March 2005, 18:15
  5. PicBasic Pro & PicBasic syntax different
    By Billyc in forum General
    Replies: 5
    Last Post: - 16th April 2004, 22:19

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