Page 284 - Programming Microcontrollers in C
P. 284

Timer Operations    269

                          close the loop and regulate the motor speed. The relation that can be
                          derived from the above data is

                                             RPM + 12528
                              PWM_count =
                                                 7.875
                              This expression is accurate to about 1% across the range of
                          interest. The measured data is the input capture count, which we will
                          call  p. What we must do is to calculate a new value for the
                          PWM_count from a measured p. An error in p is called p and this
                          expression means a change in p. The PWM_count will be designated
                          as pc and also means a change in pc. We shall now attempt to arrive
                          at an equation that expresses the necessary change in pc to correct
                          for an error in p. The error in p is the difference between the desired
                          cycle time and the measured cycle time. RPM in the above expression
                          is converted to cycle time p by noting that the RPM is 60000 / shaft
                          cycle time in milliseconds, and that the clock is ticking 500 times per
                          millisecond. Therefore, we have
                                     1   12528 + 60000* 500 
                              p=                             
                                c
                                   7.875          p          
                              If there is a error in the cycle time say, ∆p, then the change in
                          pc, ∆pc, is seen to be

                                               3809500
                              p  + ∆p  =1591+
                                c    c
                                                p+  ∆p
                              This expression, after some simplification and approximation
                          reduces to

                                                ∆p
                                    –
                              ∆p= 3809500 +
                                 c               2                   (5-1)
                                                p
                              This value calculated by the above expression will be used to change
                          the PWM_count that drives the motor. The data contained in
                          measured_period are placed there by the input capture interrupt
                          service routine. It is assumed that the data there are always current.
                              The calculation to determine the PWM_count correction is
                          substantial. We shall use this approach here to determine the
                          correction, but recognize that there is another approach that probably
                          requires less total calculation. This approach is to use a lookup table.
   279   280   281   282   283   284   285   286   287   288   289