PDA

View Full Version : Adjusting PWM frequency



flotulopex
- 13th March 2008, 19:34
Hello,

I've tried to use PWM in 10bits mode for the first time and (almost) everything works fine.

Making some measurements, I notice that the PWM frequency doesn't reach 1kHz as it should (in my opinion). I get around 0,975kHz instead.

Is this due to my 4MHz XTal's or what?

' PIC 16F690 Fuses
@ DEVICE FCMEN_OFF
@ DEVICE IESO_OFF
@ DEVICE BOD_ON
@ DEVICE CPD_OFF
@ DEVICE PROTECT_OFF
@ DEVICE MCLR_OFF
@ DEVICE PWRT_OFF
@ DEVICE WDT_OFF
@ DEVICE HS_OSC

' Registers 76543210
OPTION_REG = %10000101 'PORT A&B Pull-Ups disabled (look WPUA & WPUB)
ANSEL = %00010100 'Select analog inputs Channels 0 to 7
ANSELH = %00000010 'Select analog inputs Channels 8 to 11
ADCON0 = %00000001 'A/D Module is ON (Bit5:2 select channels 0:11)
ADCON1 = %00000000 'A/D control register
CM1CON0 = %00000000 'Comparator1 Module is OFF
CM2CON0 = %00000000 'Comparator2 Module is OFF
INTCON = %10100000 'INTerrupts CONtrol (TMR0 ON)
TRISA = %00000000 'Set Input/Output (0 to 5)
PORTA = %00000000 'Ports High/Low (0 to 5)
TRISB = %00000000 'Set Input/Output (4 to 7)
PORTB = %00000000 'Ports High/Low (4 to 7)
TRISC = %00000000 'Set Input/Output (0 to 7)
PORTC = %00000000 'Ports High/Low (0 to 7)

' HPWM registers
T2CON = %00000101 'Timer2 = ON (bit2), prescaler = 4
CCP1CON = %00001100 'Select PWM Mode
CCPR1L = 0
CCP1CON.5 = 0
CCP1CON.4 = 0
PR2 = 255

DutyCl var word
dutycl = 856
TEST:
CCPR1L = dutycl >> 2
CCP1CON.5 = Dutycl.1
CCP1CON.4 = Dutycl.0
pause 1000
goto test

skimask
- 13th March 2008, 20:31
Is this due to my 4MHz XTal's or what?
Check out that PIC-Multi-Calc file. That should tell you if 4Mhz will get you the 1khz you desire.
Also keep in mind that there's a 'granularity' with everything in the digital world. I haven't run the numbers, but, just for instance, with a 4Mhz crystal, you might get .975khz and 1.025khz with just a single number change, simply because you can't divide by a fraction. Same reason you can't get EXACTLY 9600 baud from a 20Mhz crystal in a PIC. You'll get close, really close, but not EXACTLY.
Bump up Fosc and you might be able to get the numbers you want...

flotulopex
- 14th March 2008, 07:56
So this is what I did get from PIC MultiCalc.
<img src=http://www.picbasic.co.uk/forum/attachment.php?attachmentid=2413&stc=1&d=1205480970>
Under "Possible match", the settings I use for 10 bts resolution are the same.

Now this question: why is the maximum duty value "1000"? A 10 bits resolution should allow 1024 steps, no?

Bruce
- 14th March 2008, 09:33
PR2 should be loaded with 249 for 1kHz @4MHz. Plug this into the calculation shown in the
data sheet for PWM resolution, and you'll see it's just short of 10-bits.

flotulopex
- 14th March 2008, 11:27
Bruce,

Why should it be loaded with 249?

If I calculate the possible resolution with this given formula, it makes exactly 10 bits with PR2 set to 255 "+1".
<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=2417&stc=1&d=1205492502">
I have to admit that the PWM calculation is still a mystery to me; I only roughtly understand the different parameters involved.

But this is exactly why I would like to understand where this value "249" comes from.

Tosc is a fourth of one microsecond (using the 4MHz XTal), isnt 'it?

Adrian
- 14th March 2008, 13:08
Hi Roger
I'm pretty new to this as well but I'd like a shot at answering your question. If I'm wrong I get shot down as well!

Freq Calc

PWM freq = Fosc/(PR2 + 1) x 4 x Prescale
With PR2 at 249 this is 4,000,000/250 x 4 x 4 = 1000 Hz.

Your 255 gives 4,000,000/256 x 4 x 4 = 976.56Hz which is what you measured.

Resolution formula as you stated. With PR2 at 249 then Resolution is Log 4 x 250/Log2 = 9.9657 = 1000 bit resolution (999.94 actually)

With PR2 ar 255 then resolution will be Log 4 x 256/Log2 = 10 bit but your frequency then can't be 1kHz ...only 976 Hz

Hope I got this all OK guys

Adrian

skimask
- 14th March 2008, 13:17
Hope I got this all OK guys
Looks good to me...and it's also the reason why you'll never get exactly 1khz at 4mhz...

PR2 = 249 = 976.56hz
PR2 = 248 = 1004.0160642570281124497991967871 Hz

flotulopex
- 14th March 2008, 15:08
It's very clear now.

Thanks for your explanations.

skimask
- 14th March 2008, 18:12
It's very clear now.
Thanks for your explanations.

It's a bit confusing...
The FREQ of the PWM can only be (X) and you might not be able to set it to (X+1) because the 'setter' of the FREQ is a divisor of Fosc. The duty cycle of that particular frequency of the PWM can be practically anything.
There are other ways to get an exact PWM frequency with an exact duty cycle at that exact frequency...but they go quite a bit further than a standard PIC.