What kinda Delay Routine is this.

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
CSS:
/* Delay Function */
#define FOSC 16000000UL // Using Internal Clock of 16 MHz
#define delay_us(x) { unsigned char _dcnt; \
_dcnt = (x)/(24000000UL/FOSC)|1; \
while(--_dcnt != 0) continue; \
}
void delay_ms(unsigned int cnt)
{
unsigned char i;
do {
i = 5;
do {
delay_us(164);
} while(--i);
Is this a blocking delay ?
 

JWHassler

Joined Sep 25, 2013
306
CSS:
/* Delay Function */
#define FOSC 16000000UL // Using Internal Clock of 16 MHz
#define delay_us(x) { unsigned char _dcnt; \
_dcnt = (x)/(24000000UL/FOSC)|1; \
while(--_dcnt != 0) continue; \
}
void delay_ms(unsigned int cnt)
{
unsigned char i;
do {
i = 5;
do {
delay_us(164);
} while(--i);
Is this a blocking delay ?
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.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
If it is blocking, why all the useless jibber jabber words..?
why not just
C:
delay_us(164);
 

AlbertHall

Joined Jun 4, 2014
12,346
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.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
Wait..
That is for the delay to have the defined timing, interrupts should be turned off and then back on ?
 

AlbertHall

Joined Jun 4, 2014
12,346
Wait..
That is for the delay to have the defined timing, interrupts should be turned off and then back on ?
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).
 

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
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 ?
 

djsfantasi

Joined Apr 11, 2010
9,163
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.
 
Top