Page 219 - Programming Microcontrollers in C
P. 219

204    Chapter 4  Small 8-Bit Systems

                          }
                       }
                       else /* must be that OCF1==1 rather than OCF2
                   */
                       {
                          time_cnt.b.hi=OCHI1; /* output compare */
                          time_cnt.b.lo=OCLO1; /* get lo byte */
                          time_cnt.l +=count; /* time for interrupt */
                          OCHI1=time_cnt.b.hi;
                          OCLO1=time_cnt.b.lo;/* reset the OCF */
                          ms_10++;
                          return;
                       }
                   }

                              Listing 4-6: ISR For PWM System
                              Since the interrupt bits are set to allow output compare interrupts,
                          an interrupt will occur. The interrupt service routine will be entered,
                          and the instruction sequence

                   if( TSR.OCF2==1) /* accesses the TSR */
                   {
                       .
                       .
                       .
                          accomplishes two things. First, it determines if the output compare 2
                          caused the interrupt, and, secondly, it accesses the TSR and arms the
                          reset of the OCFX when the proper lower byte of the output compare
                          register is read later.
                              For the moment, assume that OCF2 bit is set: the interrupt was
                          caused by a compare in output compare register 2. In this case a test
                          is made to determine if flag.ON is reset. Since the initialization rou­
                          tine reset this bit, it will be 0. Therefore, the code sequence
                   flag.ON=1;
                   TCR.OLVL2=0; /* Timer Compare 2 to turn off*/
                   time_cnt.b.hi=OCHI2; /* get the start time */
                   time_cnt.b.lo=OCLO2; /* reset the OCF */
                   time_cnt.l +=pwm_number;
   214   215   216   217   218   219   220   221   222   223   224