title: STM32F4 Timers - Calculation of Period and Prescale, and Generating 1 ms Delay
body: <p>I am using <code>STM32F407VGT6</code> with <code>CubeMX</code>.<br/> So i was beginning with General purpose timers and i am stuck with prescale and period values. </p>
  
  <p>Basically i want to generate a timer interrupt every <code>n</code>(where n=1,2,3..) ms and perform some tasks.<br/> There is lot of variations in the formula to calculate the value of period and prescale</p>
  
  <p>Some versions of formula are:</p>
  
  <blockquote>
    <p>TIMupdateFreq(HZ) = Clock/((PSC-1)*(Period-1))<br/>
    Update Event = TIM clk/((PSC+1)*(ARR+1)*(RCR+1))<br/>
    Prescaler = ((((ClockSpeed) / ((period) / (1 / frequency))) + 0.5) - 1)<br/></p>
  </blockquote>
  
  <p>So coming to the question, My core clock is running at <code>168 MHz</code> But i can see the Timer are connected to <code>APB1 Bus</code> which is running at <code>84 MHz</code>.</p>
  
  <p>I have tried a code which generates 1 ms delay(said by the author) and after using that values for prescale and period i generated a code which also generates 1ms Delay(by intuition - no scope).</p>
  
  <p>The code uses prescale value of 41999 and a period of 1999.</p>
  
  <p>So,<br/>PSC - 41999<br/>ARR - 1999<br/> Apllying this to the second formula</p>
  
  <p><code>Update Event = TIM clk/((PSC+1)*(ARR+1)*(RCR+1))</code></p>
  
  <p><code>Update Event = 84000000/(42000*2000) = 1</code> (Is this 1ms Delay??)</p>
  
  <p>OK so now i am trying to understand how this <code>PSC = 41999</code> and <code>Period = 1999</code> are selected?? Is it purely based on assumption as in whatever formula i use i have to assume one variable.?? How to calculate prescale and period if i want say 1.5 or 2.3 or 4.9 something like that precise timing.??</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Moreover when i used <code>PSC=41999 and Period =999</code> The update event value is 2.</p>
  
  <p><code>Update Event = 84000000/(42000*1000) = 2</code></p>
  
  <p>But my delay is twice in every sec. i.e 500ms</p>
  
  <p>and when i use <code>PSC=39999 and Period =4199</code> The update event value is 0.5.</p>
  
  <p><code>Update Event = 84000000/(40000*4200) = 0.5</code></p>
  
  <p>and my delay 2 ms.</p>
  
  <p>Thanks in advance</p>