Paul Laiming

Timer problem with 9S08AW

Discussion created by Paul Laiming on Feb 11, 2009
Latest reply on Feb 13, 2009 by Paul Laiming

I've run into a problem using timer triggers and Im not sure what Im doing wrong.

I am using MC9S08AW16 for a motor control application. MCU is running off an external clock and setup to use TPM1 to PWM a motor control chip. (Prescaler=2, Modulo=1025, 256.25uS period) 
TPM2  is setup at 10ms with overflow interupt enabled. I am using the overflow interrupt to
trigger execution of my main control loop so I can track time-based statistics.
// Loop continously, do not leave this for loop
  for(;:smileywink: {
    // Wait for 10ms timer flag from ISR
    while(!timer10ms) {}
     main routine....

The issue I am having is that I need to calculate motor speed by  measuring time between 2 tachometer pulses. I do not have any more timers so I am trying to use TPM2 channel to capture the timer on a falling edge of the tachometer input signal,  save to a variable, capture the time on the next pulse, and calculate the time between the two. I am also trying to count overflows so I can add them back to the total.
My code is as follows:

 if(calculate_speed) {
        if(hall_switch < 2) {
        //if we didnt get the halls to switch then the motor isnt spinning
           motor_period = 0;        
        else {
        // calculate the motor rpm                                                          
          motor_period = (hall_tpm2_overflows * tpm2_modulo) + hall_time0 - hall_time1;
        hall_switch = 0;
        hall_time0 = 0;
        hall_time1 = 0;
        //enable timer
        TPM2C0SC = 0x44;       

__interrupt void isrVtpm2ch0(void)
  /* Write your interrupt code here ... */
  if(hall_switch == 0) {
    // store timer value
    hall_time0 = TPM2C0V;
    // reset channel
    TPM2C0SC = TPM2C0SC & 0x7F;
    // clear overflow counter
    tpm2_overflows = 0;
    //increment hall switch counter
  } else { 
    hall_time1 = TPM2C0V;
    hall_tpm2_overflows = tpm2_overflows;
    //disable ISR
    TPM2C0SC = 0x04;


What I am seeing is very inconsistent values for motor_period even when the motor is spinning at a constant speed.  Can you see anything blaringly wrong with my setup or approach?




Message Edited by t.dowe on 2009-09-04 02:09 PM