Page 275 - Programming Microcontrollers in C
P. 275

260    Chapter 5  Programming Large 8-Bit Systems

                       TFLG1=OC3F; /* reset OC3 interrupt flag */
                       TOC3=TOC1+time_on;
                   }
                              Listing 5-6: Motor Control Program Framework
                              The interrupt service routine for IC1 resets the IC1F bit in the
                          TFLG1 register. Then the elapsed time from the last input capture time
                          is calculated. The result of this calculation is saved in period1 which
                          is the current elapsed time required for the motor to rotate one revolution.
                          At the end of this calculation, the contents of the input capture register is
                          saved in time1 to be used as the old time in the next calculation.
                              The PWM output from OC3 will be used to drive motor 1 whose
                          shaft sensor is fed into IC1. The value in speed will be the desired
                          speed of the motor in revolutions per minute. The range of this number
                          will be 1000<=RPM<=10000, and it will have to be put in place through
                          a debugger for this test. This range of motor speed was chosen to sat­
                          isfy the speed of a small DC motor used in the final system. Let us plan
                          a servo type operation where the motor is driven by a feedback loop
                          that is controlled by the speed error. Therefore, within the applications
                          loop, there must be code to check and control the PWM signal to force
                          the speed1 parameter to match the input speed parameter.
                              The input speed is measured in RPM, and the values measured by
                          the microcontroller are all times. A question that should be considered is
                          whether the number placed in speed1 will actually be times or should
                          this number be RPM. To calculate the time, note that there is one interrupt
                          per revolution of the shaft and one minute is 60 seconds, so RPM/60 will
                          be revolutions per second. What we need is the number of bus cycles in
                          the time corresponding to this period. With a prescaler count of four, the
                          clocking speed of the TCNT register is one count every two microseconds,
                          or 500,000 counts per second. Therefore, the conversion from RPM to
                          time for a revolution of the shaft in bus cycle counts is

                                       500000 * 60  30000000
                              Counts =             =
                                          RPM          RPM
                              At 3000 RPM, the count value will be 10000 while at 16000
                          RPM the count value will be 1875. These numeric values can be
                          handled by the timer system in the MC68HC11.
                              The above expression can be used to calculate RPM from the
                          count value as
   270   271   272   273   274   275   276   277   278   279   280