# C embedded Delay in a continous while loop

Status
Not open for further replies.

#### winses

##### Newbie level 3
Hi all

I need a help .I am on the way to design of a new product based on LCD.The code that i am using is of C.

Now i have a continuous while(1) loop for an RTC operation in the main code .

But i need a delay of 2 to 3 sec in that while(1) loop for some buzzer related operation.I need to do this without affecting the RTC operation.

thanks
winses

#### ChrisC

##### Member level 2
My first thought would be: why isn't the RTC code running off a timer interrupt?

However, assuming you've got a good reason for doing your RTC updates outside of interrupts, then the way I'd approach this is to include a software timer within the RTC loop so that whenever you need to generate the 2-3 second delay, instead of stopping the loop for all of that time, you run the software timer in parallel with the RTC and, only when this timer has reached the required value, then would you perform whatever action you need to perform after the delay.

Pseudo-code example:
Code:
unsigned char|int|long delay_timeout;   // select variable type based on maximum timeout value required...

delay_timeout = 0;
while
{
do_rtc_stuff();

// when you need to generate the buzzer delay, set delay_timeout to the appropriate non-zero value...
if(delay is required)
{
delay_timeout = [number of ticks in the required delay];
}

if(delay_timeout)
{
if(--delay_timeout == 0)
{
do_buzzer_stuff();   // this function/code block needs to execute as quickly as possible...
}
}
}

Or something like that... my head is feeling rather fuzzy today thanks to the usual seasonal cold/flu that's going around, so the above might not be totally accurate, but it should get you started at least!

Status
Not open for further replies.