delay_us variable

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
speedy08fr
Posts: 21
Joined: 17 Nov 2010 15:02
Location: france (ardenne)

delay_us variable

#1 Post by speedy08fr » 22 Oct 2019 18:44

Hello
I can not assign a variable instead of time

dim temp as longword
delay_us (temp)
thank you
micobasic 7.5
easypic 6
pic18f45k22

thank you

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: delay_us variable

#2 Post by janni » 22 Oct 2019 23:00

delay_us is an inline function, i.e. it's a fixed code delay prepared during compilation. Therefore it accepts only a constant parameter (as known at compile time).

Forming variable delay function of so short duration and microsecond resolution is not easy and possible only for fast enough processor clock. Even then it has to be written in assembly. Here's such a function from my replacement of Delays library (for PIC18 processor)

Code: Select all

sub procedure VDelay_us(dim Time_us as word)
' produces delay in microseconds (up to 1024us)
' Limitations:
'      - minimum clock frequency: 1MHz
'      - maximum clock frequency: 48MHz
'      - minimum delay & resolution depend on clock frequency, for example:
'        clock   min. delay  resolution  error (Time_us - delay)
'         4MHz     60us         4us      0,1,2 or 3us
'         8MHz     30us         2us      0 or 1us
'        12MHz     20us         1us      0..1us
'        16MHz     15us         1us      0
'        32MHz      8us         1us      0
'        48MHz      5us         1us      0
'      - due to compiler optimization, delays for Time_us=0 are shorter
'        than minimal by half of possible resolution (or two asm instructions).
' The delays are accurate for clock frequencies that allow it (when multiples
' of single instruction time give whole us periods) - most of the common ones.
' Note that active interrupts will lengthen the delay. With microsecond ranges
' interrupt servicing may render the delay unpredictable and thus unuseful.
 dim loops as word absolute 0 volatile

  Time_us=Time_us
  loops=Clock_MHz
  asm
         movff    FARG_VDelay_us_Time_us,R1
         movff    FARG_VDelay_us_Time_us+1,R2
         movlw    0xFC
         andwf    R2,W
         bz       $+10
         movlw    0x3
         movwf    R2
         setf     R1
         bra      $+2
         movf     R0,W
         mulwf    R1
         movff    PRODL,R0
         movff    PRODH,R1
         mulwf    R2
         movf     R1,W
         addwf    PRODL,W
         movwf    R1
         movlw    4
         bcf      STATUS,C
         rrcf     R1,F
         rrcf     R0,F
         decfsz   WREG,F
         bra      $-8
         movlw    16
         subwf    R0,F
         movlw    0
         subwfb   R1,F
         bnc      $+20
         bnz      $+2
         bnz      $+6
         movf     R0,F
         bz       $+12
         incf     R1,F
         bcf      STATUS,Z
         dcfsnz   R0,F
         decf     R1,F
         bnz      $-4
  end asm
end sub

speedy08fr
Posts: 21
Joined: 17 Nov 2010 15:02
Location: france (ardenne)

Re: delay_us variable

#3 Post by speedy08fr » 25 Oct 2019 19:44

bonjour
mercie de votre réponse
avec toute mes amitié

Post Reply

Return to “mikroBasic PRO for PIC General”