Dear PICsters, A couple of times in the last few weeks some people have been asking how to go about getting a decent period reading both for short and long periods. I recently had to devise a method to do exactly that, and I thought that others on the list might be interested in what I came up with. It is often useful to discuss methods by actually applying them to an example. That makes it easier to follow, because then you can actually attach numbers to the concepts and see what is happening. Let's say that we are accumulating a period count into a 16 bit count register that consists of COUNT_LOW and COUNT_HIGH. Let's further assume that we have an interrupt routine that occurs once each millisecond. That would give us a maximum period count of 65,535 milliseconds. The granularity of the count is 1 millisecond, so an error of 1 count at the high end (65,535) is .0015%. At the LOW end, an error of 1 count could represent almost 100% error. As you can see, the real problem occurs when the count is low. Slight changes in the period can represent BIG changes in, say, speed. When the period is small it helps a lot if instead of just finding the period we find the AVERAGE period based on multiple periods. The shorter the period, the MORE samples we should include in our average. The method that I devised uses a switchover point. Any period longer than a certain value is handled regularly. Periods that are shorter than a certain minimum value are accumulated until an integer number of short periods has been accumulated, and then the average value is dtermined. To simplify things a bit, it is useful if the minimum count can be expressed by a binary number that has only a single "1" in its representation. This means we would pick a switchover value of 1024, 2048, 4096, 8192, 16384, etc. Upon initialization, and at the end of a successful period count, COUNT_LOW and COUNT_HIGH are cleared to zero. In addition, a DIVISOR register is also cleared to zero. A bit flag is initialized to the current level of the "PERIOD port bit". This is used later to determine edge changes. Inside the interrupt routine the period value is incremented once each millisecond. Then the proper PERIOD port bit is tested to determine if the period has terminated. If the period has terminated, then the DIVISOR register is incremented. Then we check the high order bits that represent the numbers greater than or equal to 1024. If they are ALL "0", then we have not yet reached our minimum count, so we "fake" the system by immediately setting things up as if a new period is beginning, but leaving the COUNT_LOW, COUNT_HIGH, and DIVISOR registers as-is. Eventually the count reaches 1024 or more. If the 16 bit counter is now divided by the DIVISOR value, then you will obtain the AVERAGE period value. This average value will have less tendency to bobble around. In this technique the system has a switchover at the minimum count point. Long periods are measured "normally", and periods below the minimum will be "averaged". This method is quite good at reducing digit bobble. ********* Another method is to keep accumulating periods until either the period count exceeds some arbitrary value (such as 1024), with a DIVISOR value of 1, -OR- until the DIVISOR value reaches a value of 10. In this case you set a flag that says the number is a multiple of ten. Process the number as if it was normal, but DISPLAY it shifted. (The decimal point position effectively performs the equivalent divide by ten function). If your main aim is simply to reduce bobble (instead of improve resolution), then you can simply ignore the digit that would be to the right of the decimal point. You could extend this concept to handle even shorter periods by extending the DIVISOR value to 100. Fr. Tom McGahee -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.