Page 218 - Programming Microcontrollers in C
P. 218

Pulse Width Modulator System     203

                          period will be 2 microseconds times the total count value. Prior to
                          starting use of the PWM, the bit flag.ON should be reset and the
                          value for count, which is the main time base period count, should
                          be set.
                   /* bits in flag */


                       #define ON 0

                       union both time_cnt;
                       unsigned long off_count,pwm_number,count;
                       bits flag;


                   void __TIMER_OC(void)
                   {
                       if( TSR.OCF2==1) /* accesses the TSR */
                       {
                          if(flag.ON==0)
                          {
                              flag.ON=1;
                              TCR.OLVL2=0; /* Timer Compare 2 off*/
                              time_cnt.b.hi=OCHI2;/* get start time */
                              time_cnt.b.lo=OCLO2; /* reset the OCF */
                              time_cnt.l +=pwm_number;
                              OCHI2=time_cnt.b.hi;
                              OCLO2=time_cnt.b.lo;
                              return;
                          }
                          else
                          {
                              flag.ON=0;
                              TCR.OLVL2=1;/* Timer Compare 2 to on */
                              time_cnt.b.hi=OCHI2;/* get start time */
                              time_cnt.b.lo=OCLO2; /* reset the OCF */
                              time_cnt.l +=off_count;
                              OCHI2=time_cnt.b.hi;
                              OCLO2=time_cnt.b.lo;
                              return;
   213   214   215   216   217   218   219   220   221   222   223