Simple asm delay ?


Closed Thread
Results 1 to 23 of 23

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    Hi Jerson,

    I understand your questions, let me explain some more :

    I drive 3 powerled's directly from the mains using a led driver board. These led's are switched by FET's, each on it's own channel. The driver board itself is about 10x10mm, a pic with 3 pwm channels in a SO8 package is just not available. Everything larger won't fit the enclosure.

    The driver cannot drive 2 or 3 led's at once, that's why i'm switching between the three. Doing so at a total delay (led1 delay + led2 delay + led3 delay)=1000Hz + some overhead.

    Pauseus does it's work, and all is fading smoothly. Except for the first start of a color. This sticks to 12us. When a fade is started, the color that is being faded in does not start at 1 or 2 us but starts with an initial delay of 12uS.

    Hope this makes my problem somewhat clearer ...

    I did count the instruction cycles, i even did a cross comparison of the asm files, the one using a number @ wait 0 and the one using @ wait _ledRED both files are exactly the same and have the exact same length ! I played around with the variables, changed them to byte's, putting them in bank0 ....

    Why would changing from a numeric value to a numeric value stored in a variable result in an overhead of 30uS, i just don't understand.

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


    Did you find this post helpful? Yes | No

    Default

    Small update :

    Declaring the variables as:

    ledRED VAR WORD BANK0

    Does reduce the total delay somewhat, the shortest delay (0) now results in 20uS ..

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


    Did you find this post helpful? Yes | No

    Default

    Try declaring your variable as BYTE instead of word. It will shave more time from the 20uS.

  4. #4
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    971


    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 19:00.

  5. #5
    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

  6. #6
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    971


    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 07:13. Reason: PS

  7. #7
    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 ?

Similar Threads

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