Simple asm delay ?


Closed Thread
Results 1 to 23 of 23

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    Did you find this post helpful? Yes | No

    Default

    Assuming you have TMR0 (8 bit timer) free, you can try this code

    Code:
    AsmDelay macro  delayus
          movlw  0FFh-delayus    ; delayus to overflow                          1T
          movwf  TMR0                                                                            1T
          bcf        INTCON, T0IF    ; clear the timer0 Overflow flag          1T
          btfss     INTCON, T0IF    ; wait till it overflows                          1T/2T
          goto      $-1                   ; back to the btfss                              2T
      
          endm
    The minimum delay you will achieve will be 4T cycles or roughly 2uS at 8MHz.


    The way to use it would be
    Code:
          GPIO = %01
          @ AsmDelay 12
          GPIO = %10
          @ AsmDelay 24
    I hope this takes you closer to your goal. The code is just a concept, you may have to correct it. One important point you should note - the maximum delay will be 255uS
    Last edited by Jerson; - 12th November 2009 at 18:00.

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


    Did you find this post helpful? Yes | No

    Default

    Try this.
    Code:
    asm
    DelayCycles macro ncy ; ncy can be from 1 up to 256
      local n
    n set   (ncy)   ; assign ncy value to label n
    
      while n > 2   ; inserts goto $+1 while n > 2
        goto $+1    ; burn 2 cycles
    n   set   n - 2 ; n = n - 2
      endw
                    ; handle left-overs
      while n > 0   ; 1 nop for odd numbers
        nop         ; burn 1 cycle
    n   set   n - 1 ; n = n - 1
      endw
      endm
    endasm
    
    Main:
      HIGH 0
      @ DelayCycles 1  ; adds 1 nop
      LOW 0
      @ DelayCycles 5  ; adds 2 x goto $+1 and 1 x nop
      GOTO Main
    Regards,

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

  3. #3
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    967


    Did you find this post helpful? Yes | No

    Default

    Brilliant work.

    PS. It has a catch though. Te macro expansion occurs at compile time. So, you will not be able to "feed" the delay value at runtime
    Last edited by Jerson; - 13th November 2009 at 06:13. Reason: PS

  4. #4
    Join Date
    Apr 2006
    Location
    GearSweaterMountain, The Netherlands
    Posts
    52


    Did you find this post helpful? Yes | No

    Default

    Dear Jerson & Bruce,

    I could not get here earlier, sorry. I appreciate your efforts !

    Jerson, if i declare the delay variable as a byte, it takes even longer.
    I just don't understand .. The delay macro i found works perfect if given a number straight from the call, if it is fed by a variable the delay jumps to 20 or 30us. It also seems that the delay is fixed after that, if i compile @ wait 1000, it delays a perfect 1000 cycles, if i compile @ wait ledred (i.e. ledred = 100) it delays 20uS no matter what ledred is ?

    Bruce, your asm code also does the trick, perfect 1us or 50us delays, pretty neat. But it also suffers from the same problem. If i compile @ Delaycycles 200 if works perfect, if i compile ledred var byte, ledred = 200, @ delaycycles _ledred it gives a delay of 33uS no matter what the value of the leded var is ??

    Apart from that, i need to do a delay between 1 and 1000uS, using tmr0 the max delay will be 256, and i can use the Bruce asm up to 512, the ASM code i posted earlier can go up to about 1024.

    I would use the 16-bit TMR1 if it was available but i need that to do a 100Hz sync (count) interrupt...

    Any ideas ?

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


    Did you find this post helpful? Yes | No

    Default

    @ delaycycles _ledred it gives a delay of 33uS no matter what the value of the leded var is ??
    That's because when you pass _ledred to the macro as the argument, it uses the 'address'
    of ledred, and not the actual value in RAM. Look at the address PBP places ledred at to see
    why that happens.

    You can't put a macro inside a loop with a variable as the argument. What you're looking to do just
    isn't possible (as far as I know) with a macro inside the loop, and calls to any .asm routine, timer, etc,
    just aren't going to give you from 1uS to 1000uS delay periods you need immediately after
    placing a value on GPIO.

    I would re-think the whole process if I'm stuck at 8MHz, or switch osc speed & PIC to support using
    the pauseus commands.
    Last edited by Bruce; - 13th November 2009 at 23:42. Reason: Macros
    Regards,

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

  6. #6
    Join Date
    Apr 2006
    Location
    GearSweaterMountain, The Netherlands
    Posts
    52


    Did you find this post helpful? Yes | No

    Default

    Hi Bruce,

    Thank you for your detailed answer ! All is clear now.

    I have adapted the code in a way that the led's never go below 12uS, that way the 'color' switch on and off effect (color switches on for 12uS instead of 1uS) does not occur. This makes the colors somewhat more pastel, but it is still a smooth fade and a cool effect.

    And, yes, if i'm going to make a new design i will certainly use a larger external osc ..

    Thanks for all your answers and information !

    Best regards,
    UB

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


    Did you find this post helpful? Yes | No

    Default

    Grab a new 8-pin PIC12F1822 with 32MHz internal osc when they come out. Then you can
    get PAUSEUS down to 2uS. These even have an onboard EUSART.
    Regards,

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

Similar Threads

  1. 16F628A - Stops if release power switch.
    By dene12 in forum General
    Replies: 16
    Last Post: - 14th February 2009, 07:57
  2. Old and beyond help ?
    By DavidFMarks in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 11th December 2008, 15:23
  3. RF Transmitter
    By et_Fong in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 27th October 2005, 16:34
  4. Memory Space of the PIC16F84...
    By Tear in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 1st July 2005, 19:55
  5. Problem with saving to EEPROM...
    By Tear in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st July 2005, 00:10

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