SparkFun Forums 

Where electronics enthusiasts find answers.

Discussions on how to get your MSP JTAG programmer up and running.
By 5thWall
#70978
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.
By OldCow
#70994
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.
By 5thWall
#71005
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.