This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Timer-Need your Help.....

hai all, I want a to implement timer concept for 10sec. I want to start timer in one state and want to stop it another state.Is it possible?

//states are INIT,POWERON,GATEOPEN,GATECLOSE.
//set active state as INIT
while(1)
{
 process();

}
void process()
{
 switch(activestate)
 {
  case INIT:
           starttimer(2minutes);//here i want to start the timer and proceed to next state ie parallel timer for 2minutes.
           change_active_state=poweron;
  case poweron:
             if(timernotover)
            //remain in same state
            else
           //proceed.
  case open:
             if(gateopentogateclose)
            {
              start timer(10sec);
              //watch the input status
              if(gate close detected)
                stoptimer;
              change state to closed;
            }
  case closed:
            if( close detected)
            {
             stoptimer;
            }
      .
      .
      .
      .
      .
      .


 }
}


my problem is if i start timer then it remains in same state till the timer completes. I want to start timer and proceed to next state and kill the timer there....
in the above scenario the timer waits in INIT state for 2minutes but it should wait in poweron state.

timer routine
starttimer(int noofsec)
{
TMOD=TMOD&0xF0;
TMOD=TMOD|00x01;
for(i=0;i<noofsec*20;i++)
{
//50 milli sec timer
TH0=0x3C;
TL0=0xB0;
TF0=0;
TR0=1;
while(TF0==1);
TR0=0;
}
}


without for loop is there any way to maintain the timer parallely........
with thanks,
Ram

Parents
  • Don't busy-loop your timer.

    Use a timer interrupt that updates an "uptime" variable with suitable resolution.

    Then in your code remember current uptime.

    Do what you need to do until uptime is at least 2 min larger.

    If you have a busy-loop function - as you currently do - then it will not return until after two min.

    And no pseudo-code when you ask for specific problems.

    Pseudo-code should only be used when discussing an algorithm, to give a rough description of the algorithm.

Reply
  • Don't busy-loop your timer.

    Use a timer interrupt that updates an "uptime" variable with suitable resolution.

    Then in your code remember current uptime.

    Do what you need to do until uptime is at least 2 min larger.

    If you have a busy-loop function - as you currently do - then it will not return until after two min.

    And no pseudo-code when you ask for specific problems.

    Pseudo-code should only be used when discussing an algorithm, to give a rough description of the algorithm.

Children
No data