Abstract:
A system and method for controlling time-critical functions by dynamically adjusting the interrupt frequency of a microprocessor. Interrupts are generated at a predetermined frequency. Time-critical functions are then performed in response to the interrupts. The interval is adjusted based on timing data from a substantially accurate source. Where the interval is measured in terms of clock pulses, a non-integer number of pulses per interval is attained by varying the number of pulses per interval so that the average over a number of intervals is equal to the non-integer number of pulses per interval. The interval is further adjusted using a selected damping coefficient to eliminate an underdamped condition.

Description:
This is a continuation of application Ser. No. 07/807,989, filed Dec. 16, 1991, now abandoned. 
    
    
     FIELD OF THE INVENTION 
     The present invention relates to a system and a method for dynamically adjusting the interrupt frequency of a microprocessor timer thereby increasing the accuracy of time-critical functions. In particular, the present invention provides a system and a method for adjusting the timer interrupt frequency according to timing information from a substantially accurate source. 
     BACKGROUND OF THE INVENTION 
     Although it is often believed that quartz crystal oscillators are very accurate, temperature changes and crystal aging cause substantial shifts in a crystal&#39;s natural frequency. For many systems, this frequency shift is unacceptable and some means of correction is needed. 
     For frequency correction, trimmer capacitors are inserted to fine tune the crystal frequency. If these are temperature compensating capacitors, the oscillator&#39;s frequency can be made more consistent over wide temperature swings. Disadvantages, however, include the need to tune these capacitors for each crystal, and of course the added cost of the components. Additionally, the capacitors provide no automatic correction for frequency shifts from aging. 
     Some devices, like ABB ERNIs which are known in the art, incorporate a timer interrupt mechanism that executes an interrupt after a counter attains a specific threshold value. The frequency this counter is incremented is related to the crystal, so an inaccurate crystal will cause inaccurate timer interrupt intervals. Since these intervals may be used in time critical functions such as a time stamping scheme as disclosed in U.S. patent application Ser. No. 414,999 entitled &#34;A System for Time Stamping Events Using a Remote Master Clock&#34;, filed Sep. 29, 1989, it is essential that they be as accurate as possible. 
     SUMMARY OF THE INVENTION 
     The present invention provides a system and method for controlling time critical functions. Interrupts are generated at a predetermined interval. The time-critical functions are then performed in response to the interrupts and the interval is adjusted using timing data from a substantially accurate source. 
     In a preferred embodiment, a shift in the interrupt frequency is determined with respect to the timing data and a new interval is defined to compensate for the frequency shift. Based on the new interval, the interrupt frequency is altered by increasing the time between a first number of interrupts and by decreasing the time between a second number of interrupts. The average of the time between interrupts of the first and second numbers of interrupts is indicative of the new interval. In a further preferred embodiment the interval is adjusted by a damping coefficient to prevent an underdamped system condition. 
     The interval is preferably a number of clock pulses provided by a timing means at a fixed frequency. A cycle can be defined as a number of interrupts. Thus, the new interval can be based on the average number of pulses per interval over a given cycle. By averaging over a cycle, non-integer intervals in terms of pulses per interval can be realized. 
    
    
     BRIEF DESCRIPTION OF THE DRAWINGS 
     The present invention will be better understood and its numerous objects and advantages will become apparent by reference to the following detailed description of the invention when taken in conjunction with the following drawings, in which: 
     FIG. 1 is a system block diagram in accordance with the present invention. 
     FIG. 2 is a flow diagram representing one implementation of the present invention. 
    
    
     DETAILED DESCRIPTION OF THE INVENTION 
     Referring to FIG. 1, a network having a master device 10, slave device 12 and network communication bus 30 are depicted. The master device 10 has a real-time clock 20 which has a substantially accurate time base. The slave device 12 comprises a processor 22, an interrupt counter 24, and an oscillator 26, which is typically a quartz crystal oscillator. 
     When the slave device 12 is initially powered, the interrupt counter 24 is reset to zero, and the timer threshold value is set to a default value. This default value is based on the assumption that the device will be used at a specific temperature and the crystal&#39;s &#34;make&#34; frequency, that is, the actual frequency of a newly manufactured crystal. When counter 24 reaches this default threshold, an interrupt is initiated. At this point, any time-specific functions, such as time stamping information, are performed. An interrupt is provided each time the counter attains the default threshold. 
     The master device 10, supplies the slave device 12 with three pieces of data: 
     i) A start mark which causes the device to note the current time and store it in a &#34;start&#34; register, 
     ii) A stop mark which causes the device to note the current time and store it in a &#34;stop&#34; register, 
     iii) The elapsed time between the start and stop marks which is assumed to be accurate. 
     Essentially the processor 22 determines the elapsed time by subtracting the start time from the stop time. It then compares this internal elapsed time with the received elapsed time. Differences are used to change the default counter value to a count that will improve the accuracy of the interrupt interval. 
     If the internal elapsed time is less than the received elapsed time, counter threshold value is decreased; causing interrupts to occur more frequently. Likewise, if the internal elapsed time is greater than the received elapsed time the threshold value is increased. 
     The new threshold N can be determined from the equation: ##EQU1## where I is the internal elapsed time calculated by the processor 22, R is the received elapsed time provided by the clock 20, and C is the current threshold value. 
     This equation is derived from the following: ##EQU2## 
     Where E is the internal time base or oscillator error. Then to create a new threshold, start with the current threshold, C, and use E as a correction factor, so that: 
     
         N=C+E*C                                                    (3) 
    
     Simplifying equation (3) gives: ##EQU3## 
     This equation cannot be applied directly because typically the new interval will include a fractional quantity. Since the counter 24 increments in integers, interrupts cannot be initiated after, for instance, 200.85 pulses of the oscillator 26, therefore a further refinement is needed. 
     In a preferred embodiment the new interval is dynamically altered in a cyclic pattern. Using 200.85 as an example, let one cycle be 100 interrupts. Now for 85 of these 100 interrupts, the interrupt is executed after 201 pulses, and for the remaining 15 interrupts, after 200 pulses. Thus the average over 100 interrupts is precisely 200.85 pulses per interrupt (e.g., 85 * 201+15 * 200). 
     To determine how many interrupts per cycle need to be stretched, use: 
     
         D=N-INT(N)                                                 (4) 
    
     where D is the distance between the new interval and the nearest integer less than the new interval. ##STR1## Then, the number of interrupts per cycle which need to be stretched to the higher integer threshold is D multiplied by the cycle size or, in this example where one cycle is 100 interrupts, 85 elements need to be stretched (i.e., 0.85*100). 
     If the cycle size is carefully selected, another option for calculating the new threshold and stretch count is available. Using the above example, with a cycle size of 100, the digits of the stretch count, 85, are always identical to the fractional part of the new ideal threshold, 0.85. Thus, all fractional quantities may be eliminated by modifying equation 1 as follows: ##EQU4## where C&#39;=100*C+stretch count then N=INT(N&#39;/100) and stretch=lower two digits of INT(N&#39;). For example, let the internal elapsed time be 76 seconds, the current threshold is 200, the current stretch is 85, and the received time is 77 seconds. ##EQU5## 
     Then N equals INT(19824.16/100) which is 198, and stretch equals the lower 2 digits of INT(19824.16) which is 24. 
     In order to avoid unnecessary division and multiplication, multiplying and dividing by 100 are the same as shifting the decimal point two places right and two places left, respectively. This technique saves microprocessor time by speeding up the routine. 
     When working with hexadecimal numbers, the principle is identical, but the cycle size is preferably selected to be 256 or 100 h. Then instead of multiplying by 100 (base 10), multiply by 100 h, or shift the &#34;heximal&#34; point two places right. 
     This system is essentially an underdamped feedback control mechanism, thus using it &#34;as is&#34; can result in the interval toggling back and forth between values that are too high, and too low. To prevent this situation, a damping coefficient is preferably incorporated in determining the new interval: 
     
         new.sub.13 damped=P*DAMP+N*DAMP                            (6) 
    
     where P is the previous interval, DAMP is the selected damping coefficient and DAMPis 1-DAMP, N is the new threshold calculated from equation 1. 
     As an example, let the previous interval be 200.00, and the new interval be 201.00. Using the ERNI damping coefficient of 0.707, which is a typical value used to obtain &#34;critical damping&#34; in many control systems: 
     
         new.sub.-- damp=200.00(1-0.707)+201.00*0.707 
    
     so 
     
         new.sub.-- damp=200.707, 
    
     and this interval is used rather than 201.00. 
     Selection of the damping coefficient involves the usual tradeoffs for feedback control systems. Choosing a very large damping coefficient (close to 1.00) will result in a very slowly changing system, but one that will never overshoot the ideal interval. On the other hand, a small damping coefficient will allow the system to reach the ideal quickly, but pass it right on by. The threshold will continue to oscillate until the system eventually settles. 
     The flowchart in FIG. 2 shows one implementation of the present invention by processor 22. This particular diagram depicts the ERNI code as an example. 
     The current threshold and stretch count are moved into adjacent bytes, to be treated as one three byte quantity at 41. 
     The numerator for equation 5 is calculated at 42. Since, the ERNI processor instruction set does not include multiplication or division, several subroutines perform multiplication of multiple byte numbers. The new interval N&#39; is calculated at 43, where an interval halving scheme is used for division of multiple byte numbers. The new damped interval is calculated at 44. 
     The new threshold bytes are mapped into the corresponding registers for the current threshold at 45. Finally these bytes are output to counter 24 for executing interrupts according to the current interval. 
     This technique has several advantages: 
     it is inexpensive: it does not require additional components, requires no manual adjustments of capacitors, and can be implemented entirely in software, 
     it is adaptable: it compensates for the current frequency of the crystal; if that crystal&#39;s frequency drifts with age, temperature, or was just inaccurately made, the scheme will just alter the interval to correct, 
     it effectively improves the resolution of the microprocessor&#39;s clock allowing much finer adjustments of the interrupt interval, 
     It is flexible: the time between start and 
     stop marks doesn&#39;t have to be consistent, thus the master doesn&#39;t have to promptly service the slave&#39;s interval adjustment needs. Further, the damping coefficient can be altered, even &#34;on the fly&#34; in order to achieve better results. 
     It should be appreciated that the present invention is not limited to its use in ABB ERNIs nor is it limited to controlling time stamping functions. The implementation described herein is purely an example. Thus, those skilled in the art will recognize that modification and variations may be made without departing from the principles of the invention described herein above and set forth in the following claims.