Adjusting PWM frequency


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by flotulopex View Post
    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...

  2. #2
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default

    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=120548097 0>
    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?
    Attached Images Attached Images  
    Last edited by flotulopex; - 14th March 2008 at 08:53.

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default 249?

    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=120549250 2">
    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?
    Attached Images Attached Images  
    Roger

  5. #5
    Join Date
    May 2007
    Location
    Suffolk, UK
    Posts
    59


    Did you find this post helpful? Yes | No

    Default

    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

  6. #6
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Adrian View Post
    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

  7. #7
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Thumbs up Thanks a lot

    It's very clear now.

    Thanks for your explanations.
    Roger

Similar Threads

  1. SLA battery charging with PWM but which frequency
    By showtime in forum mel PIC BASIC Pro
    Replies: 31
    Last Post: - 13th June 2017, 11:41
  2. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  3. PWM setting PIC16F887
    By Gevo in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 12th June 2008, 07:24
  4. PWM - Dutycycle affecting frequency?
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 18th May 2007, 09:32
  5. inaccurate frequency using TMR1 PI18F452
    By nkarpovich in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 14th October 2006, 16:22

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts