title: using timer for delay
body: <p>I need to implement a delay function with a hardware timer. The timer value is incremented each millisecond. </p>
  
  <p>The usual approach is to use the width of the timer register and use the modulo behavior corresponding to the</p>
  
  <pre><code>volatile int TimerReg;
  
  void Delay(int amount)
  {
      int start = TimerReg;
      int elapsed;
  
      do
      {
          eleapsed = TimerReg - start;
      } while (elapsed &lt; amount);
  }
  </code></pre>
  
  <p>This works when the TimerReg has the width of int. The difference <code>now - start</code> is a steadily increasing value in that case.</p>
  
  <p>But when the width of TimerReg is less than the width of int, or (as in my case) the timer counts only from 0..1000, you get a problem when the timer wraps from 999 over 1000 to 0.</p>
  
  <p>What is a good approach to use such a timer?
  I would like to avoid the modulo operation because this is expensive on the microcontroller.</p>
  
  <p><em>Edit</em>: The division module is not included in the microcontroller code yet.</p>