How to setup 38kHz PWM with PIC18F1330


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: How to setup 38kHz PWM with PIC18F1330

    It's not really a parameter in that sense, it's more like a side effect of the relationship between the oscillator frequency and PWM frequency. The closer they are in relation to each other the less resolution you get - it's just the way it works.

    Basically, all it is a counter driven by the oscillator. The PWM period is set by telling a comparator at what count to "start over". If the counter is clocked at 1MHz and you tell it start over at the value 100 then the PWM frequency would be 1000000/100=10000Hz. If you tell it start over at 10000 the PWM frequency becomes 1000Hz and so on. This is what the PTPER value does.

    The PWM output is set at the start of the PWM cycle and reset when the counter equals the dutycycle value. Now I Think you can see that when the PWM frequency in the example above is 1000Hz ther resolution will be 10000 "steps" while when the PWM frequency is 10000Hz the resolution will be 100 "steps". It's just the way a digital PWM generator works.

    As for your ripple, make sure you have decoupling capacitor(s) as close to the supply pins of the PIC as possible. If the PIC has more than one pair of supply pins make sure they are all connected and properly bypassed.

    /Henrik.

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


    Did you find this post helpful? Yes | No

    Default Re: How to setup 38kHz PWM with PIC18F1330

    Thanks Henrik.

    I had to use a 4,7µF capacitor to finally get a nice clean signal

    Thank you for the explanation too. I'll have a go for some tests with the formulas; this is going to be the hardest for me...maths are not really my best friends

    Roger
    Roger

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


    Did you find this post helpful? Yes | No

    Default Re: How to setup 38kHz PWM with PIC18F1330

    So there's where I am now:

    Desired freq = 38kHz (PWMperiod = 26,3157µs), crystal (Fosc) 20MHz, prescaler = 1
    SEARCHING FOR PTPER (max 4095)

    Transform formula:
    PWMperiod[µs] = (PTPER+1 * prescaler) / (Fosc / 4)
    => (PTER+1 * prescaler) = PWMperiod * (Fosc / 4)
    => PTPER+1 = (PWMperiod * (Fosc / 4)) / prescaler
    (26,3157µs * 5'000'000) / 1 = 131

    But how do I calculate the duty-cyle register (PDC0) admitting I'd like to have a 50% value to set?
    Roger

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: How to setup 38kHz PWM with PIC18F1330

    Hi,
    There will be aproximately 4 times as many "steps" (+/-3) to the dutycycle value as you set PTPER to. If PTPER is 131 then you'll have 131*4 = 524 steps so to get a a 50% dutycycle you set the dutycycle register to 264 (PDC0H=1, PDC0L=8).

    /Henrik.

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


    Did you find this post helpful? Yes | No

    Default Re: How to setup 38kHz PWM with PIC18F1330

    Well, I just found this in the DS:
    DS note, page 131:
    To get the correct PWM duty cycle, always
    multiply the calculated PWM duty cycle
    value by four before writing it to the PWM
    Duty Cycle registers....
    I think this means a "100%" (in my mind, this is a signal always high - not useful but for the theory) duty-cycle value would be:
    - PTER * 4 => 130 * 4 => 520

    So, for a 50% duty-cycle, the PDC0 value is going to to be:
    - 520 * 50% => 260

    But, playing with the values, I can see on my scope that the numbers would be more accurate taking in account the PTER+1 (131 instead of 130) value.

    Where is the truth?
    Roger

  6. #6
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    952


    Did you find this post helpful? Yes | No

    Default Re: How to setup 38kHz PWM with PIC18F1330

    ...you were quicker
    Roger

Similar Threads

  1. 38Khz Modulator
    By Gord11 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 28th September 2011, 09:14
  2. manual hardware PWM setup on 16F1824
    By comwarrior in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 31st January 2011, 19:34
  3. Use Button For setup
    By tump in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 21st November 2007, 20:43
  4. Generated square wave 19Khz and 38Khz
    By blinkstar88 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th August 2007, 17:40
  5. pic12f683 setup
    By erice1984 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 4th July 2007, 12:48

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