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.

Getting the right frequency for PWM signal, using TX1

Other Parts Discussed in Thread: MSP430F5359


Hi All,

we have a question about using a PWM signal to create a buzzer sound at the right frequency on the MSP(MSP430F5359), we use a timer that is sourced by Aclk and Aclk is sourced by a  32768 Hz crystal. Our target frequency is 2880 Hz for the buzzer. But we can't get the right frequency because the timer has a to low resolution. We use the following code:

P4DIR |= BIT2;  // PWM settings                           

P4SEL |= BIT2;                          

TA2CCR0 = 11;          //   // 32768 Hz ACLK / 2880 buzzer = 11.377 = 11               

TA2CCTL1 = OUTMOD_3;                     

TA2CCR1 = 5;                             

TA2CTL = TASSEL__ACLK + MC_1;           // ACLK, up-down mode 

This gives a frequency of around 2730 Hz when we meassure it, we need a duty cycle of 50 procent so TACCR1 gets the half of TA2CCR0 but this is rounded to 5. The timer registers can only be whole values, using a higher and lower value for TA2CRR1 and TA2CRR0 gives a to high or to low frequency.

Is there a way to get a more preciese PWM signal? Could using the DCO be an option? To our understanding the DCO is not accurate enough. Does the DCO Always have the same preciese speed when booted up (with the same settings)?

Any feedback is welcome!

  • Dividing a fixed number by a variable number doesn't lead to equidistant steps. The smaller the divider, the larger the steps go.
    However, generating a variable frequency with 50% duty cycle isn't PWM. PWM stands for Pulse Width Modulation. That means a fixed frequency with variable duty cycle. Something completely different and something the MSP can do easily. What you want is frequency modulation and something the MSP isn't built for. It can only be done with limitations.
    When trying to do anyway, you can make your life a bit easier: run the timer in up mode and set the CCR0 to toggle mode. This gives perfect 50% duty cycle without a second setting. It also halves the output frequency (obviously). Still the frequency adjustment remains as rough as it was.
    You can try to optimize it by using dividers and tables to keep the timer resolution vs frequency in a range where your steps are fine enough, but that's all you can do.
  • Hi Jens-Michael,

    Thank you for your reply, if we understand you correctly then there is no way that the MSP can generate the exact frequency of 2880 Hz? And the best hope is to just get als close to it as possible?

    Is using the the TX1 crystal better than using the DCO, since this gives atleast a stable frequency Always and the DCO does not.

**Attention** This is a public forum