PWM frequency calculation.

Discussions on how to get your MSP JTAG programmer up and running.

Moderator: phalanx

Post Reply
5thWall
Posts: 2
Joined: Sun Apr 19, 2009 10:58 pm

PWM frequency calculation.

Post by 5thWall » Sun Apr 19, 2009 11:34 pm

Hi. I'm trying to make a little musical instrument with my msp430f2013. I've been able to make it produce several tones using PWM, but I still haven't figured out how to calculate specific frequencies. To be honest I think I'm still a little confused about how PWM works.

So if timer a is running at 1.11Mhz and I set the period to 254
CCR0 = 0xFE;

Then that brings it down to ~4.3Khz
Then if I set the duty cycle to 20 which is about 1/12 the period
CCR1 = 20;

Then the final output frequency will be about 338Hz (E sharp)?

Is that how it works or am I completely off base? Any help, or even a link in the right direction, will be appreciated. Thank you.

OldCow
Posts: 269
Joined: Sun Mar 26, 2006 10:04 am

Post by OldCow » Mon Apr 20, 2009 7:28 am

You misunderstood "duty cycle".

With the settings you stated, you get a square wave with a frequency of ~4.37kHz. It is "on" 20% of the time and "off" the remaining 80% of the time. That is, the period (or cycle) is about 1/4.37=0.229msec. During each cycle, it is "high" 0.229*20%=0.046msec and "low" 0.229*80%=0.206msec.

To get ~338Hz, you need to set CCR0=3284-1; (CCR0 has 16 bits).

Duty cycle determines DC bias and AC volume. The DC bias is directly proportion to the duty cycle. The AC volume is the highest at 50% duty. It is lowest at either 0% or 100% duty.

5thWall
Posts: 2
Joined: Sun Apr 19, 2009 10:58 pm

Post by 5thWall » Mon Apr 20, 2009 10:14 am

Okay. Thank you, that makes more sense, and explains why I didn't see anything with duty cycle pertaining to changing the frequency.

But it looks like my period calculations are on target, so that should be all I need to get what I want. Thank you again.

Post Reply