125KHz HW PWM Output



THE BOOK of DT's INTERRUPTS is an organization of interrupt service routines and other works written by Darrel Taylor, RIP.

Interrupt routines are arranged per Darrel's original list. Darrel Taylor's Instant Interrupts are an extension of the work of Tim Box whom in October of 2002 wrote INT_CTRL.pbp

Many of the forum members felt this is a long overdue project.

THE BOOK of DT's INTERRUPTS
will be a valuable resource and a tribute to Darrel Taylor the "Fanatical Contributor".

THE BOOK of DT's INTERRUPTS is located at http://dt.picbasic.co.uk/

+ Reply to Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2013
    Posts
    41

    Default 125KHz HW PWM Output

    Greetings!

    Having problems generating a clean 50% duty cycle hardware PWM @125KHz using a PIC16F628 with a 20MHz crystal.
    The below is from a 2002 Microchip AppNote: (Which specified a 16F628 in the original schematic.)

    The drive signal can be generated directly by the PWM
    unit on most PIC® microcontrollers such as a
    PIC16F627 microcontroller. For a device operating at
    20 MHz, one can obtain a 125 kHz signal by setting the
    Timer2 prescaler to 1. A period of 8 μs is then obtained
    by setting the PR2 register to 39. To get a 50% duty
    cycle output, set CCPR1L to 14 and CCP1CON<5:4>
    to <0:0>. These settings will ensure a constant carrier.
    To modulate the data, one can turn the drive signal on
    and off by setting and clearing the CCP1RIL bit.

    My interpertation of the above code is:
    PR2 = 39 ' A period of 8 µs is then obtained by setting the PR2 register to 39.
    CCPR1L = 14 ' To get a 50% duty cycle output, set CCPR1L to 14 and CCP1CON<5:4> to <0:0>.
    CCP1CON = %00001100 ' PWM Mode, CCP1CON<5:4> = <0:0>
    T2CON = %00000100 ' For a device operating at 20 MHz, one can obtain a 125 kHz signal by setting the Timer2 prescaler to 1.

    However, this results in the below:

    Name:  F0000TEK.BMP
Views: 62
Size:  76.1 KB



    Note that although it is exactly 125.0KHz, it is not symetrical and has an approximate duty cycle of 2.8us/5.2us * 100 = 35%

    Although running on an old dev board from Bruce Reynolds, I would prefer to update to a PIC10(LF32x) dev board.
    (Or something from melabs.)

    Suggestions?

    Jay Zebryk, W1JRZ​

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,118


    Did you find this post helpful? Yes | No

    Default Re: 125KHz HW PWM Output

    Note that although it is exactly 125.0KHz, it is not symetrical and has an approximate duty cycle of 2.8us/5.2us * 100 = 35%
    maybe more like duty cycle of 2.8us/8us * 100 = 35%


    CCPR1L = 14 ' To get a 50% duty cycle output, set CCPR1L to 14 and CCP1CON<5:4> to <0:0>.
    or correctly
    ' To get a 50% duty cycle output,
    CCPR1L to $14 and CCP1CON<5:4> to <0:0>.
    Warning I'm not a teacher

  3. #3
    Join Date
    Jul 2013
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Re: 125KHz HW PWM Output

    I found this:

    https://www.micro-examples.com/publi...alculator.html

    Which produces this:

    PR2 = 0b00001001 ;
    T2CON = 0b00000101 ;
    CCPR1L = 0b00000100 ;
    CCP1CON = 0b00111100 ;

    Running that results in 3.8 / (3.8 +5.2) = 47.5% which is much closer.

    FYI: This is for a RFID application wherein the center frequency is important to the resonate gain of the attached antenna.

    Wondering if this is an 8, 10 or 16Bit computation?
    Wondering if this makes any difference consideration the even value of 125.0KHz?

    Thanks,

    Jay Zebryk, W1JRZ

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,118


    Did you find this post helpful? Yes | No

    Default Re: 125KHz HW PWM Output

    totally missing the point

    CCPR1L = $14 not 14 ; its hex 14 or dec 20

    Warning I'm not a teacher

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,275


    Did you find this post helpful? Yes | No

    Default Re: 125KHz HW PWM Output

    It is a 10bit PWM system but the resolution of the PWM signal depends on the PWM period or frequency. Higher the frequency, the lower the resolution.

    So, at 20MHz and a frequency signal of 125KHz, the resolution will be 7 bits only, even though the control register is 10bit wide.

    Ioannis

  6. #6
    Join Date
    Jul 2013
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Re: 125KHz HW PWM Output

    Ioannis,

    Thanks for the feedback!

    I have been using the attached calculator from Mister E.

    Name:  Mr_E.png
Views: 52
Size:  8.3 KB



    Is the Duty Step % (0.625) X 50% = 31.25% the problem?
    (I am measuring 3.8uS On / 4.2uS Off)
    How can I set it for 4mS On / 4mS exactly?

    (FYI, I am headed towards filtering the Square wave into a Sine wave and want it to be symetrical.)

    Is this even doable with the ancient 16F628?
    I am only using this because I had the Dev Boards handy.
    Do you remember Bruce Reynolds?
    What would you recommend for a 8PIN Dip?

    Peace,

    Jay Zebryk


    Attached Files Attached Files

  7. #7
    Join Date
    Apr 2014
    Location
    Northeast
    Posts
    498


    Did you find this post helpful? Yes | No

    Default Re: 125KHz HW PWM Output

    Referencing the Mister e Calculator, it looks like the best resolution you can get is 0 to 160, where 160 will be 100% Duty Cycle.

  8. #8
    Join Date
    Jul 2013
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Re: 125KHz HW PWM Output

    Hi,

    That implies one needs to set the Duty Cycle to 80 to get a 4uS On / 4uS Off square wave?
    (This does not make sense to me.)

    Thanks,

    Jay Zebryk

  9. #9
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,421


    Did you find this post helpful? Yes | No

    Default Re: 125KHz HW PWM Output

    Richard already covered it but it seems to have been overlooked.

    The duty cycle register is 10bit wide but, as Ioannis says, the usable number of bits differs depending on the PWM period. The calculator tells you that for 125kHz (with PR2=39) varying the duty cycle register from 0 to 160 will give you a duty cycle of 0-100%.

    For 50% duty cycle you obviously want to set the register to 80.

    Now, remember that the register is 10bit wide and that two low order bits are in CCP1CON so what goes in CCPR1L is the top 8 bits of the 10bit value. Or, in this case 80/4=20 which, as Richard rightly points out is $14.

  10. #10
    Join Date
    May 2013
    Location
    australia
    Posts
    2,118


    Did you find this post helpful? Yes | No

    Default Re: 125KHz HW PWM Output

    Is the Duty Step % (0.625) X 50% = 31.25% the problem?(I am measuring 3.8uS On / 4.2uS Off)
    How can I set it for 4mS On / 4mS exactly?
    your understanding is flawed and math is incorrect

    Duty is Steps x 0.625% // steps = CCPR1L x4 + CCP1CON<5:4>

    so:
    80x0.625% = 50% <=> steps = 20x4 + 0
    56x0.625% = 35% <=> steps = 14x4 + 0
    Warning I'm not a teacher

Similar Threads

  1. 18f4431 pwm output
    By prits068 in forum mel PIC BASIC
    Replies: 0
    Last Post: - 5th April 2010, 21:41
  2. 4 PWM output
    By Macgman2000 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 13th September 2008, 06:19
  3. 4-20mA output using PWM
    By gavo in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 8th December 2007, 09:45
  4. 2 PWM output
    By Md.Shah in forum mel PIC BASIC
    Replies: 3
    Last Post: - 9th October 2006, 22:38
  5. 18F6585, PWM Output, which Pin?
    By Mark Scotford in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 26th April 2005, 21:37

Tags for this Thread

Posting Permissions

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