AnsweredAssumed Answered

Delay in ms function

Question asked by Juan Carlos Orozco on Sep 3, 2011
Latest reply on Sep 3, 2011 by Juan Carlos Orozco

Hello,

 

I´m developing a delay function that works in milliseconds.

 

this is my code so far:

 

void delayms(unsigned int time)
{
  result1 = max_value / 1000;
 
  final_result = result1 * time;
 
  d_enabled = FALSE;
 
  if(!d_enabled)
  {   
    dmsec     = 0;
    TPM2MOD   = final_result;
    TPM2SC    = 0x0F;
    TPM2C0SC  = 0x54;                
    TPM2C1SC  = 0x10;
    d_enabled = TRUE;
  }
 
  while(!delay_done);
}

 

Here´s the TPM_ISR

 

interrupt VectorNumber_Vtpm2ch0 void timer2_CH0(void)
{

  // Clear CH0F flag
  TPM2C0SC &=0x7F;
 
    dmsec++;
   
    if(dmsec == final_result)
    {
      TPM2MOD  = 0;
      TPM2SC   = 0x00;
      TPM2C0SC = 0x00;                
      TPM2C1SC = 0x00;
      delay_done  = TRUE;
    }
 
              
  //Clear the 16-bit timer1 counter
  //TPM1CNTH = 0x00;
 
}

 

My timer counts at 1 second with a MOD equal to 0xFF ot 65535 so basically assigns the MOD value to a fixed one depending on the function call.

 

My problem is, with that while it seems the timer ISR is not working, so I´m looking for a way to loop inside the function until the timer returns a flag?.

Outcomes