Searching \ for 'Delays' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page:
Search entire site for: 'Delays'.

Truncated match.
PICList Thread
1997\04\13@165259 by John Doe

Could someone send me a formula of sorts...and some code in the Parallax
instruction set.....on how to figure out delays depending on the
frequency your running at.....the delays should at least be in milli
seconds.......since I had my Basic Stamp 2...the pause command spoiled

thanks a lot...

1997\12\26@114221 by Stein Sem-Jacobsen
picon face
part 0 475 bytes
Is there an easy way to calculate delays?
Stein Sem-Jacobsen

1997\12\26@173257 by Ivan Cenov

part 0 1819 bytes


Delays can be implemented by software or by software & timer
depending on the PIC you are using (timer & interrupts available).

If there is interrupt capability (PIC16CXX and not PIC16C5XX),
you can enable TIMER0 interrupt and the interval which the timer interrupts at
will become time quantum for delays. In ISR you can implement a counter (RTClock)
which will increment. And delays will be measured in this counter units.
If you want smaller units, ISR can write to TIMER0 a value that is closer to 0xff.

Instead of TIMER0 you may use TIMER1 and CCP1 in 1010 compare mode that
generates interrupt. CCPR1H:CCPR1L will contain interval measured in 1/(CLOCKIN/4).
ISR should set TMR1L and TMR1H to an appropriate value closer to 0.

A routine performing delay can be defined as
void Delay (char ticks);
and it will count _ticks_ changes of above RTClock
before return to the caller.

If there is no interrupts it is still possible to call (very) often a procedure that will check if
timer0 overflow is occured and if it is to increment RTClock. Here is good idea to have
a guard value, smaller than 0xff and check if timer0 has got above it. It is important
to call this procedure at intervals smaller than the time that TIMER0 needs to pass from
this guard value to 0xff and 0x00.

The delay procedure will have loop with
call to timer0 check routine and
check for RTClock change
and this will repeat until _ticks_changes happen.

these variant are not for very short delays.

Hope this helps.

Ivan Cenov

2000\02\08@053436 by James Cameron
These PICs are just too fast, we often need a way to have the
processor wait around for a while.  A series of NOP (no operation)
instructions is straightforward, but wasteful of instruction memory.
A counted loop is the next common trick, because that lets us tune it.

But what are some of the more exotic ways to delay?

1) The Common NOP, a delay for one instruction cycle

       DELAY   NOP                     ; delay one cycle

  Advantages: extremely simple, obvious, maintainable, scaleable.

  Disadvantages: is it really there for a reason?

2) The Common Loop, three times the initial value plus three

       DELAY   MOVLW   D'95'           ; 288 cycle delay
               MOVWF   COUNTER
               DECFSZ  COUNTER,F
               GOTO    $-1

  Advantages: fairly simple, maintainable, scaleable up to 771
  cycles; after that go for a nested loop.

  Disadvantages: costs one file register; though there is a variant
  using just the W register.

3) The Novel GOTO, two instruction cycles in one instruction

               GOTO   $+1              ; two cycle delay

  Advantages: half the space of two NOPs.

  Disadvantages: obscure unless commented.

4) The CALL to Nowhere, four instruction cycles

               ORG     0
               GOTO    MAIN
       FOUR    RETURN                  ; four cycle delay function
       DELAY   CALL    FOUR

  Advantages: quarter the space of four NOPs, the RETURN can be
  reused by other code, good use for those three bytes between the
  reset vector and the interrupt vector on a PIC 16F84.

  Disadvantages: implementation separate from use, can look odd, uses
  one stack level.

5) The Double Call to Nowhere, eight or four cycles

               ORG     0
               GOTO    MAIN
       EIGHT   CALL    FOUR
       FOUR    RETURN
       DELAY   CALL    EIGHT

  Advantages: looks simple, allows various size delays to be rapidly
  constructed during prototyping.

  Disadvantages: uses two stack levels.

6) The Do Something Useful Extension to the Common Loop, five times
  the initial value plus three

       DELAY   MOVLW   D'95'
               MOVWF   COUNTER

               MOVF    TRISM           ; fetch TRIS mirror
               TRIS    TRISB           ; reapply it
               DECFSZ  COUNTER,F
               GOTO    $-3

  Advantages: good for precise delays that are no multiples of three,
  allows useful functionality to be placed within the delay.

  Disadvantages: increased convolution of code, lower maintainability.

7) The Long Delay Using Timer, more of a technique than a code
  fragment, set the timer, wait for it to roll over.

  Advantages: immune to distortion by interrupts, easily scaled using
  a prescaler, even possible to tune the delay by modifying the
  preload value.

  Disadvantages: allocates a timer.

8) The Watchdog Delay, go to sleep and get woken by the watchdog.

  Advantages: extremely simple technique, can be varied by changing
  the WDT prescaler ratio.

  Disadvantages: difficult to calibrate.

9) The Data EEPROM Delay, a typically 10ms delay that can be triggered
  by writing to data EEPROM and waiting for the interrupt.

  Advantages: tests the endurance of the EEPROM.

  Disadvantages: tests the endurance of the EEPROM.

James Cameron

2000\02\08@071220 by paulb

James Cameron wrote:

> But what are some of the more exotic ways to delay?

 I feel that many applications (real-time control, clocks) resolve to a
fundamental "tick" or hierarchy thereof, often around a millisecond,
which method {7} provides.  Counting a thousand of these gives a second,
at which point a train of countdowns (semaphores) can lead to various
housekeeping actions i.e., if T1 then { T1--; if T1 == 0 then action1 };

 On the 1 ms "ticks" also, a debounce uses a counter which counts down
from 20 (20 ms) to verify a keypress/ release.  Other countdowns in ms
are used for playing tunes or "tick", "Click" or "blip" noises.

 It is highly undesirable to pre-load or fiddle with the TMR0 when you
are using it this way, firstly because it interferes with the prescaler
in a very inconvenient fashion, and secondly as the 1 ms countdowns can
be used for a 500 Hz tone while the TMR0 MSB can be copied to a port for
a 1 kHz tone, bit 6 for a 2 kHz tone, etc., controlled as above in even
numbers of milliseconds.

 You can similarly, wait on those individual bits by polling, for sub-
delays.  A sub-delay on bit 3 toggling could be used to time a phase
accumulator (32 kHz clock) to generate quite a complete range of square
wave tones.

 So, you may say that method "uses" a timer-counter, but I submit that
in a well-designed application, you get an awful lot of "use" out of it!
       Paul B.

More... (looser matching)
- Last day of these posts
- In 2000 , 2001 only
- Today
- New search...