This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMS320F28379D: Counter randomly increments when run withing PWM ISR()

Part Number: TMS320F28379D

Hello experts,

I am facing a wierd issue , If I execute the below function inside a PWM ISR(50 us time), testCounterSpeedMA is incremented multiple times and it increments randomly!
Sometimes it increments by 2, 11, 7 etc..
The same function if i run it in a slower task, example inside a 10 ms timer , it correctly increments by 1 when ever there is a speed change.

Why does this happen?
I would like to execute this function from the PWM isr and have the counter increment only once, when ever there is a speed change.

void checkForSpeedChange(MOTOR_Vars_t *pMotor)
{
volatile static int16_t prevSpeedMA = 0;
volatile static uint16_t testCounterSpeedLoopResetMA = 0;
if(prevSpeedMA != pMotor->speedRefRPM)
{
testCounterSpeedMA++;

//Log speed change
pMotor->speedChange = true;

//Store the previous value
prevSpeedMA = pMotor->speedRefRPM;
}
}

  • Hi AK,

    Some initial questions for you:

    1. How are you measuring/watching the counter incrementing? If you place a breakpoint in that ISR and step through the code to that breakpoint and keep checking the counter variable, do you still see it jump from 2 to 11 etc.?
    2. What is the expected outcome that you are not seeing (e.g. is testCounterSpeedMA supposed to increment by 1 each time ths checkForSpeedChange() is called?)
    3. How is the ISR being trigger by the PWM? Is it at a known/consistent time?
    4. It would also help if you could explain the nature of your program - what is it that you are trying to achieve? Are you varying the speed of the PWM and if so, how are you currently doing this?

    Best Regards,

    Allison

  • Hi Allison,

    1. I am not using any breakpoint, i am just seeing the variables in my expressions window and checking how many times it is incrementing.

    2. The expected outcome is when ever there is a change in speed setpoint(speedRefRPM), the counter (testCounterSpeedMA) should increment only once.

    speedRefRPM is set by the user(from the expressions window). Ex: lets say speedRefRPM is initially 2000, and i change it to 5000. The function(checkForSpeedChange) is supposed to detect a change in the value of speedRefRPM and increment testCounterSpeedMA once. Again if i change speedRefRPM from 5000 to 1000, testCounterSpeedMA should increment once more, and so on..

    3. The pwm interrupt is generated every 50 microseconds.(constant time)

    4. I am basically trying to detect , if the user has instructed the control system to change the motor speed and do certain functionalities(data logging, led blinking etc) once the change in speed is detected. The issue here is that , it has to be done only once, and to verify that the change in speed is really detected only once , i have kept the counter to verify really if it is executing only once or multiple times...

    If i execute the function from within the pwm isr, the counter increments multiple times, the same function if i execute it in a slower task such as a 10ms timer, the counter correctly increments only once for every speed change.

    Let me know if you need any more information.

  • Hi AK,

    Thanks for the helpful information. 

    Could you please set a breakpoint within the checkForSpeedChange() function? You can do so on the "testCounterSpeedMA++;" line so that every time the variable is incremented, the whole program will stop. This way, you know exactly when the incrementing is happening. 

    Right now, you are expecting to increment the testCounterSpeedMA variable only when you manually change the speedRefRPM value. So if you run your program and hit the breakpoint before you change the speedRefRPM, you can check the speedRefRPM and prevSpeedMA values to see if one of them somehow changed already and caused the counter to increment. Let me know if you are able to do so/if you see any results.

    Best Regards,

    Allison

  • Hi Allison,

    I did set a break point at testCounterSpeedMA++ line and yes , it does increment more than once when ever there is a speed change.

    I noticed this behavior:

    If i change the speedRefRPM from 2000 to 3000 , then it increments once correctly.

    But lets say i change speedRefRPM from 2000 to 10000,  then the first time the breakpoint is hit, speedRefRPM value is 2004, and the next time the break point is hit speedRefRPM is 10000, Why does this happen?

  • Hi AK,

    This is indeed strange behavior. I would think that once you change the value, it will jump to the new value and the ISR will catch the change. This doesn't seem like a PWM module-related issue, but more of an issue with your algorithm.

    1. Does the prevSpeedMA value look correct in the memory browser when you are debugging?
    2. Can I ask why you are using volatile static type?
    3. How are you obtaining the pMotor variable?

    Best Regards

    Allison

  • I found the issue, Strange! but i was comparing a floating point variable with a signed 16 bit integer, once i changed both of then to the same type , it worked correctly.