Equation 17-1 assumes you know the value in PTPER.Code:(PTPER+1) x PTMRPS TPWM = ------------------ Fosc/4
TPWM is the period of the PWM frequency.
PTPER is the value loaded into the PWM period register.
PTMRPS is the PWM timer prescaler. 1, 4, 16 or 64.
1/19,500 = 0.000051282. This is the period for a PWM frequency (TPWM) of 19.5kHz.
At 20MHz the instruction cycle time = 1/5MHz = 200nS.
To find the value to load into PTPER, divide the PWM period by the instruction cycle time.
0.000051282/0.0000002 = 256.410. Subtract 1 for 255.
If you load 255 into the period register, it will generate a PWM frequency of around 19.5kHz.
PTPERL=$FF ' low byte = $FF
PTPERH=$00 ' high byte = $00
Assuming a 20MHz osc, 1:1 prescaler, Fosc/4 = 5Mhz, so plug this into equation 17-1.
TPWM = 256/5MHz = 0.0000512.Code:TPWM = (255+1) * prescaler ------------------- Fosc/4
Frequency = 1/TPWM so 1/0.0000512 = 19.531kHz.
The period register is 12-bits wide, so the minimum period for a given oscillator speed = the
instruction cycle time * ($0FFF+1) * the prescaler.
I.E. at 40MHz the instruction cycle time = 1/10Mhz = 100nS.
100ns * $1000 = 0.0004096. 1/0.0004096 = 2.441kHz.
At 10MHz 1/2.5Mhz = 400nS. 400nS * $1000 = 0.0016384.
1/0.0016384 = 610Hz, etc, etc..
You can see from Table 17-1 how the PWM timer prescaler affects the PWM frequency.
I.E. 2.441kHz/64 = 38Hz, etc,..
Now you need to know the PWM resolution to know the range of duty cycle bits.
Note: Resolution is the number of bits you have to control the duty cycle. Not the PWM
frequency.
For PWM resolution use the equation below. Equation 17-3 'as shown in the data sheet'
doesn't produce the same figures for PWM resolution shown in Table 17-2.
So we 10-bit resolution. The value loaded into duty cycle registers would range from 0 toCode:log(Fosc/Fpwm) Resolution = ------------------ log(2) At 20MHz, with a PWM frequency of 19.5kHz; log(20MHz/19.5kHz) 3.010995384 Resolution = ------------------ = ----------- = 10.003 .301 .301
1023 or $00 to $03FF.
512 loaded into the duty registers should give ~50% duty at 19.5kHz.
There's a lot more to the PCPWM module than this, but it should help you get
started. I haven't played much with this particular feature.




Bookmarks