Timers: Sw Timer Calculations

With a periodic timer, the counter is being decremented as time passes. Thus, we have a slightly different scenario from the stopwatch example. Nonetheless, suppose we had a Timer32 configured as periodic timer, with a counter frequency of 1000 Hz and a load value of 999. We start the timer, let it run for a bit, and then check its counter value. The counter is currently at 827 and no rollovers have occurred yet. We then check the counter value after another period of time. The counter is at 693 and no rollover has occurred. How much time has passed? In this situation, no rollover occurred in between the two time points, so we can simply find the difference between the two values. 827 - 693 = 134 cycles have passed. Converting this into seconds by dividing by the counter frequency, we get 134 / 1000 = 0.134 seconds. Thus, we can summarize this behavior with the formula T2 - T1 = C1 - C2, where C1 is the initial counter value and C2 is the final counter value. This formula holds true when no rollover has occurred between T1 and T2

Let’s take another example. With the same counter frequency and load value as before, suppose we establish T1 as the point when the counter reads 406 and two rollovers have occurred since the timer was started. Suppose T2 is taken when the counter reads 851 and three rollovers have occurred since the beginning. In this case, our previous formula won’t hold because a rollover has occurred between T1 and T2. Instead, let’s find the time from T1 to the rollover point. We know the rollover occurs when the counter value reaches 0, so we need to know how many cycles it takes to get from T1 to 0, which is simply C1 - 0 = C1. So it takes 406 cycles to get to the first rollover. The counter takes on cycle to be reloaded, so we add 1 to our total. Finally, the counter value is now at the load value (999), and we need to know how many cycles it takes to get from there to T2. This is 999 - C2 = 999 - 851 = 148. So the total amount of time (in cycles) that has elapsed is 406 + 1 + 148 = 555, which in seconds is 555 / 1000 = 0.555 seconds. To generalize this process, we can express the equation as T2 - T1 = C1 + 1 + N - C2, where N is the load value. Rearranging into a more familiar form, we get T2 - T1 = (N + 1) + (C1 - C2). This formula holds true when only one rollover has occurred between T1 and T2

There is one more case we have to consider, though. Using the previous example, what if our T1 was instead the point at which the counter value was 522 and no rollover has occurred and T2 was where the counter was at 351 and two rollovers have occurred? Now, the counter has rolled over more than once between T1 and T2. To resolve this, we can take a similar approach as the previous example. Finding the cycles elapsed from T1 to the rollover point is the same as before: C1 - 0 = C1, so it takes 522 cycles to reach that point. Next, one cycle is taken to reload the counter. Now, one rollover has occurred since the timer was originally started. However, we need another rollover to occur to get to T2, so we need to figure out how many cycles it takes to get to the next rollover point. Our counter is currently at 999 since it was just reloaded. Conveniently, by the same logic as C1 - 0 = C1, 999 is how many cycles it will take to get to 0 from that point. The counter then takes another cycle to reload the counter. Now, we are at two rollovers since the timer was originally started, which means the remaining time is the time to T2. With the same logic as the previous example, 999 - C2 = 999 - 351 = 648. Thus, our total is 522 + 1 + 999 + 1 + 648 = 2,171, which is 2171 / 1000 = 2.171 seconds. If we consolidated all these steps into a single formula, we would see T2 - T1 = C1 + 1 + N + 1 + N - C2. Simplifying, we get T2 - T1 = 2 * (N + 1) + (C1 - C2). Notice that 2 corresponds to the amount of rollovers that have occurred between T1 and T2. Thus, we can generalize this formula as T2 - T1 = (R2 - R1) * (N + 1) + (C1 - C2), where R1 and R2 are the amount of rollovers that have occured since the timer was started at T1 and T2, respectively. This a general formula for the periodic timer. The previous two formulas are special cases of this general formula, where R2 - R1 = 0 when no rollover occurs and R2 - R1 = 1 when only one rollover has occurred.