Page 288 - Programming Microcontrollers in C
P. 288

Timer Operations    273

                       /* The initialization portion of the program */
                       TCTL2.EDG1B=ON; /* capture falling edge only */
                       OC1M.OC1M7=ON; /* sent OC1 out to PA7 */
                       OC1M.OC1M5=ON; /* couple OC1 to OC3 */
                       TMSK1.OC3I=ON; /* enable the OC3 interrupt */
                       TMSK1.IC1I=ON; /* enable the IC1 interrupt */
                       OC1D.OC1D5=ON; /* turn on OC3 with OC1 */
                       TCTL1.OL3=ON; /* toggle OC3 when OC3 occurs */
                       PACTL.DDRA7=ON; /* make OC1 an output to PA7 */
                       TOC1=TCNT+PWM_period; /* set OC1 */
                       TOC3=TOC1+PWM_count; /* set OC3 time on */
                       cli(); /* enable the system interrupts */
                       /* the applications portion of the program */
                       FOREVER
                       {  /* All of the numbers used below are derived
                                  in the text */
                          if(old_motor_speed!=motor_speed)
                          {
                              motor_period=30000000Lu/motor_speed;
                              old_motor_speed=motor_speed;
                              PWM_count= ((motor_speed+12528)/63)*8;
                              PWM_count=limit(PWM_count);
                          }
                          if(tick)
                          {
                              tick=FALSE;
                              delpc=(3809500Lu/motor_period);
                              delpc=delpc*
                                 (motor_period-measured_period)/
                                 motor_period;
                              PWM_count-=delpc;
                              PWM_count=limit(PWM_count);
                              rpm=30000000L/measured_period;
                          }
                       /* input the new motor speed */
                       }
                   }

                   /* functions of the main program */
   283   284   285   286   287   288   289   290   291   292   293