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.

PWM using timer interrupts

Other Parts Discussed in Thread: MSP430F2274

I have been using PWM for DC motors to control motors using MSP430F2274 . Just written the code based on this http://gushh.net/blog/?cat=361 

My question is .. Here in the code enters while loop motor runs . Is it possible to use interrupts . Say motors starts running when interrupt occurs(using switch interrupt) . Is it possible to control speed based number of switch presses continously?? Timer interrupt concept are new to me . Can you guys direct me to what i should read to get familiar . I have read MSP430x2xx Family . Timer guide for MSP430 . 

Thanks in advance

  • Vinoth,

    Yes you can use interrupts to generate PWM. Have a look at the example code provided by TI in slac123d.zip, could be found in the software section: http://www.ti.com/product/msp430f2274#toolssoftware . Then look at the file name msp430x22x4_adc10_16.c in the C folder.

    Hope this helps. 

    Satbir

  • It is possible to generate a PWM signal to control motor speed usign a tiemr itnerrupt. However, doing a PWM in software using interrupts introduces some jitter (depending on other processor work) and limites the PWM cycle freuency and PWM duty cycle resolution and precision.

    But the MSP timers can generate PWM signals in hardware. So you'll only need a timer interrupt (or a continuous main loop) to check the motor speed and change the PWM signal duty cycle. Note that you can update the PWM duty cycle only once per PWM cycle, even if you check the motor speed more often.

    Teh tiemr guide is indeed the main source for information about the timer. YOu can also search the forum. There have been lots of threads about PWM.

  • Basically, you are using two CCRx in a Timer to generate PWM wave form. You use CCR0 to control the period, and the other CCRx to control the duty cycle. CCRx = 0 for 0% and CCRx > CCR0 for 100%.

    Once set up, you do not need to do anything else and the same waveform will repeat itself automatically. But if you want to change the duty cycle (to change the motor speed), you need to change the setting of CCRx accordingly.

    For example:

    CCR0 is 199. This means this Timer will count from 0 up to 199, back to 0, up to 199, and repeat.

    CCRx is currently 50. This means from count 0 to count 49 the output is on, and from count 50 to count 199 the output is off. Thus 50 counts on, and 150 counts off. That is 25% on and 75% off.

    Now, you want to change to 26% on and 74% off, you need to change CCRx to 62.

    If you want to change to 20% on and 80% off, you need to change CCRx to 40 instead.

    You can make this change any time. Thus you normally sit in a endless loop so that the Timer keeps producing the 25% PWM. Only when you press a button to generate an interrupt, and the interrupt service routine (ISR) should change the CCRx setting according to your wish. Once ISR returns, you sit in the same endless loop again the the Timer keeps producing the new PWM over and over again.

    Please note that the above applies to changing duty cycle only. If you try to change the period, it is a little more complected.

  • OCY said:
    You can make this change any time.

    Not quite. If you change the duty cycle from 40% to 20% while the timer is currently at 30%, you won't get an output change on 20% (the timer is already past this) nor will you get it at 40% (the old 40% setting isn't there anymore when the timer reaches this point). So the next output change will happen at 120%. You can only apply the change if the timer is before the intended new trigger point, of if the timer is past the old trigger point.

    Similar things happen if you use a 'toggle' mode and change from 20% to 40%. You might get two toggle events then.

    TimerB allows to synchronize the change of the CCRx content with the timer overflow, which will meet both conditions. For timerA, it is best to do the change in the timer interrupt, so it happens right after the last output change. This will do for most cases. However, timer latency and code execution time and maybe delayed ISR execution due to other ISRs may still shift this into a 'critical' region. So use TimerB if your application cannot bear 'overlong' duty cycles at the moment of a DC change.

**Attention** This is a public forum