What kinda Delay Routine is this.

Discussion in 'Programmer's Corner' started by R!f@@, Sep 15, 2016.

  1. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,747
    759
    Code (CSS):
    1. /* Delay Function */
    2. #define FOSC 16000000UL // Using Internal Clock of 16 MHz
    3. #define delay_us(x) { unsigned char _dcnt; \
    4. _dcnt = (x)/(24000000UL/FOSC)|1; \
    5. while(--_dcnt != 0) continue; \
    6. }
    7. void delay_ms(unsigned int cnt)
    8. {
    9. unsigned char i;
    10. do {
    11. i = 5;
    12. do {
    13. delay_us(164);
    14. } while(--i);
    Is this a blocking delay ?
     
  2. JWHassler

    Member

    Sep 25, 2013
    201
    33
    Whether it is blocking or not depends on the nature of "delay_us()".
    That function might allow interrupts to continue.

    On the face of it, though, it seems to be a blocking delay.
     
  3. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,747
    759
    If it is blocking, why all the useless jibber jabber words..?
    why not just
    Code (C):
    1. delay_us(164);
     
  4. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,891
    375
    The code includes the definition of delay_us in lines 3 to 6.
    Yes, it is a blocking delay.
    It will allow interrupts, but they will change the timing. For accurate timing, interrupts should turned off.
     
  5. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,747
    759
    Blocking delay allowing interrupts. !! That's new to me.
    What do you mean by "change the timing".
     
  6. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,747
    759
    Wait..
    That is for the delay to have the defined timing, interrupts should be turned off and then back on ?
     
  7. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,891
    375
    Yes. If an interrupt occurs while the delay code is running, the delay code will be er... interrupted. When the interrupt ends, the delay code will continue running. Thus the total time from the start of the delay routine to its end will be the expected delay time plus however long the interrupt code took (and of course there may be multiple interrupts).
     
    R!f@@ likes this.
  8. R!f@@

    Thread Starter AAC Fanatic!

    Apr 2, 2009
    8,747
    759
    I got that from a code, my next project on the list.
    So it could be that specific delay time is added with interrupt duration.
    I think I am getting hang of the code.

    The delay is sorta counter routine type ? Yes ?
     
  9. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,891
    375
    Yep.
     
    R!f@@ likes this.
  10. RichardO

    Well-Known Member

    May 4, 2013
    1,230
    382
    One way of keeping an interrupt from changing the time delay is to make the time delay an interrupt. :D
     
    R!f@@ likes this.
  11. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,796
    831
    Or implement the timer in hardware, so that nothing in your code will affect it. Interrupts may affect when the delay end is tested. But an algorithm can be developed with the median execution time of all enabled interrupts and their probability of occurring.
     
Loading...