Page 265 - Programming Microcontrollers in C
P. 265

250    Chapter 5  Programming Large 8-Bit Systems

                          task, but it also assumes that the computer does not have much else to
                          do. Other tasks could be built into the above program, but it is absolutely
                          necessary that the program have a cycle time that is shorter than the
                          period of the PWM signal. If the loop time of the FOREVER loop is less
                          than the PWM period, then you can use the above synchronous approach.
                          It makes no difference when in the cycle that the two if statements in
                          the FOREVER loop are executed. It is necessary that they be executed
                          prior to the occurrence of the OC1 that designates the end of the period.
                          If it becomes necessary to include so much code in the FOREVER loop
                          that it is impossible to guarantee that the loop time will always be less
                          than the period, then an asynchronous approach should be used. This
                          program was compiled and executed on an evaluation module. The
                          value of time_on was adjusted to determine the range of outputs that
                          could be created with this program. The minimum time on must be
                          greater than the time required to execute the code

                   if(TFLG1&OC3F)
                   {
                       TFLG1=OC3F; /* reset OC3 interrupt flag */
                       TOC3=TOC1+time_on;
                   }
                          when OC3F is found on. The reason for this timing is that TOC3
                          must be updated after TOC1 is given a new value and the update
                          must be complete prior to the passing of time_on . Otherwise, the
                          period of TOC1 will pass before the OC3 will occur. This time was
                          measured, and it was found to be 6 clock cycles. Therefore, reliable
                          performance is obtained with a minimum time on of 6 clock cycles.
                              The maximum time on was found to be 0xffe with the above
                          code. The output signal at 0xfff was on all of the time with no
                          single cycle off period as the program would indicate.
                              An interrupt can be requested whenever an output compare occurs.
                          We are servicing two output compares in this case. One’s initial
                          thought might be to have two interrupts, in this case one for OC1 and
                          one for OC3. Is this approach really necessary? If it is guaranteed
                          that the time_on parameter is always less than period, then an
                          approach that can be used is to delay the reset of the OC1 flag until
                          the OC3has occurred. OC3 will always happen after OC1. The problem
                          with this approach is that OC3 occurs very near the end of the period,
                          and there might not be enough time to reset the OC1 interrupt flag
   260   261   262   263   264   265   266   267   268   269   270