Page 296 - Programming Microcontrollers in C
P. 296

Timer Operations    281

                              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 */
                       /* Send out the measured RPM to the terminal
                          periodically */
                          if(tick1&&!in_process)
                          {
                              tick1=FALSE;
                              rpm=30000000L/measured_period;
                              dprint(rpm);
                              do_crlf();
                          }

                          if (SCSR.RDRF==ON) /* read in data if it is
                                                        there */
                          {
                              in_process=TRUE;
                              new_character=SCDR; /* get new byte and
                                                               save it */
                              while(SCSR.TDRE==OFF); /* wait until transmit
                                                                 buffer is empty */
                              SCDR=new_character; /* send out byte and
                                                               reset TDRE */
                              /* got a number, process it */
                              if(new_character>=’0' && new_character <=’9')
                              new_speed = 10*new_speed + new_character-‘0’;
                              else if(new_character==’\r’)
   291   292   293   294   295   296   297   298   299   300   301