Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C embedded Delay in a continous while loop

Status
Not open for further replies.

winses

Newbie level 3
Joined
Jun 18, 2009
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,306
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.

Can any one please help me..how can i overcome tho issue.

thanks
winses
 

ChrisC

Member level 2
Joined
Sep 30, 2009
Messages
51
Helped
14
Reputation
28
Reaction score
2
Trophy points
1,288
Location
London, UK
Activity points
1,760
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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top