Adjusting PWM frequency


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891

    Default Adjusting PWM frequency

    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?
    Code:
    ' 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
    Roger

  2. #2
    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...

  3. #3
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    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.

  4. #4
    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

  5. #5
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    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

  6. #6
    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

  7. #7
    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

  8. #8
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    891


    Did you find this post helpful? Yes | No

    Thumbs up Thanks a lot

    It's very clear now.

    Thanks for your explanations.
    Roger

  9. #9
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

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

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 : 1

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