16Bit 125KHz PWM with PIC12F1572


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

    Default 16Bit 125KHz PWM with PIC12F1572

    Hello,

    I am upgrading from a 16F628 to a 12F1572 to take advantage of it's "Precision" 16Bit PWM.

    How do I configure PBP 3 to take advantage of it's HWPWM function?
    (Needed is a symetrical 125KHz (4mS/4mS) 50% duty cycle.)

    ----------------------------------------------------------------------------------------------------

    ' 125KHz PWM Test Generator PIC16F628 For Planar Antenna Experiment
    ' 12LF1572 (12F1572 is the 5Volt Version)
    ' Jay Zebryk 2022-07-04


    #CONFIG
    __config _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _CP_ON & _BOREN_ON & _CLKOUTEN_OFF
    __config _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_ON & _BORV_LO & _LPBOREN_OFF & _LVP_OFF
    #ENDCONFIG


    ' CP_OFF changed to CP_ON to enable hardware PWM.
    ' See HPWM_F1579_NOTES.txt
    ' MCLRE_ON changed to MCLRE_OFF for digital input with Weak Pull-Up Enabled


    ' OLD SETTINGS FROM 16F628


    'PR2 = %00001001 ' dec 9 47.5%
    'T2CON = %00000101 ' dec 5
    'CCPR1L = %00000100 ' dec 4
    'CCP1CON = %00111100 ' dec 60


    WPUA = %00000100 ' Weak Pull-Up Enabled RA2 (SW Input)


    OSCCON = %01111000 ' Bits 6-3 16 MHz OSC, Internal, I/O Pins Enabled
    Define OSC 16


    DEFINE CCP1_REG PORTA ; Tell PBP which pins the CCP outputs are on
    DEFINE CCP1_BIT 2


    Beep VAR PORTA.0 ' Assign name Beep (and/or Piezoto PORTB.0
    TX var TRISA.1 ' 125KHz PWM Output (CCP1 Pin)
    SW var TRISA.2 ' CW vs 100mS ON / 900mS OFF


    PortA = %00000000 ' PortA all OFF
    TRISA = %11111100 ' Inputs = 1, Outputs = 0


    I VAR BYTE ' Index I


    CLEAR ' Clear all variables to 0

    Boot:


    Pause 1000


    For I = 0 to 1
    Beep = 1
    Pause 100
    Beep = 0
    pause 100
    Next I


    pause 1000


    ' THE BELOW PRODUCES A COMPILATIO ERROR
    HPWM 1, Duty, 125000 ' Start HWPWM CONTINUOUS


    Main:


    Beep = 1
    pause 100
    Beep = 0
    pause 1000


    goto Main


    end

    ----------------------------------------------------------------------------------------------------

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


    Did you find this post helpful? Yes | No

    Default Re: 16Bit 125KHz PWM with PIC12F1572

    How do I configure PBP 3 to take advantage of it's HWPWM function?


    125KHz is only available with a pbpl compile

    from the manual hpwm section
    Frequency is the desired frequency of the PWM signal. On devices with 2 channels,
    the Frequency must be the same on both channels. Not all frequencies are available
    at all oscillator settings. For the non-long versions of PBP (PBP and PBPW),
    the highest frequency at any oscillator speed is 32767Hz.
    (Needed is a symetrical 125KHz (4mS/4mS) 50% duty cycle.)
    every chip you have posted about on this subject is quite capable of that output.
    your posted code has never been correctly setup to achieve that goal.
    you have never acknowledged anybody's code corrections or explanation to achieve your goal,
    you have never indicated that you have even tried them out .
    Warning I'm not a teacher

  3. #3
    Join Date
    Jul 2013
    Posts
    41


    Did you find this post helpful? Yes | No

    Default Re: 16Bit 125KHz PWM with PIC12F1572

    FYI:

    1. I just requested a current PBP manual to see if the HWPWM feature now includes >32767Hz.
    2. I have the PIC12F1572 data sheet which seems to have a completely differenct PWM operation than the 16F628.
    3. I did try and test your suggestions for the 16F628 which worked except for the symetrical issue.

    Below is the App so far with Zero progress on the PWM.
    All it will do is modulate an 125KHz magnetic antenna using OOK.

    Currently, I have configured the internal OSC for 16MHz.
    I do not know whether the 4X PLL is required for a perfectly symetrical On/Off Duty Cycle of 4uS On / 4uS Off.

    Peace,

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


    1 out of 1 members found this post helpful. Did you find this post helpful? Yes | No

    Default Re: 16Bit 125KHz PWM with PIC12F1572

    3. I did try and test your suggestions for the 16F628 which worked except for the symetrical issue.
    what did you try , post your code

    Currently, I have configured the internal OSC for 16MHz.
    all those calc's are for a 20MHz clock if you use a different OSC then they must be recalculated

    the internal OSC is not brilliantly accurate and could need trimming to approximate a symmetrical output


    on paper these settings are incorrect for 16 or 20 MHz
    ' OLD SETTINGS FROM 16F628




    'PR2 = %00001001 ' dec 9 47.5%
    'T2CON = %00000101 ' dec 5
    'CCPR1L = %00000100 ' dec 4
    'CCP1CON = %00111100 ' dec 60
    Warning I'm not a teacher

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: 16Bit 125KHz PWM with PIC12F1572

    I did try and test your suggestions for the 16F628 which worked except for the symetrical issue.
    That's probably because you're setting the CCP1CON CCP1X and CCP1Y bits, which control the duty cycle lsb's.
    Clear those two bits (ie CCP1CON = $0C for PWM mode).

    You should be able to get 125KHz with a 50/50 duty cycle at either 16MHz or 20MHz. You don't need 16-bit PWM for that.

  6. #6
    Join Date
    Apr 2014
    Location
    Northeast
    Posts
    501


    Did you find this post helpful? Yes | No

    Default Re: 16Bit 125KHz PWM with PIC12F1572

    The 16-bit PWM Module in the PIC12F1572 takes cues from the 16-bit Output Compare (OC) Module. There are several FRMs that cover the dsPIC33 OC functions if you want a more elaborate explanation. If your PWM Clock is 4 MHz (Fosc/4 @ 16 MHz), each tick takes 0.000 000 250 seconds; or 0.25 uS. For 125 kHz, you need your Period to last for 32 Clock cycles. Unfortunately, that will be your resolution -- 0 >> 31. You cannot get 16-bit resolution -- or anywhere near that. Even setting Fosc to 32 MHz, yielding an 8 MHz Fosc/4, max resolution is still only 0 >> 63. The fastest PWM frequency with 16-bit resolution will be around 122 Hz (not kHz, just Hz). I had considered using this PIC for a previous project and decided to use a dsPIC33 instead, as I couldn't get a fast enough frequency out of the PIC12F1572.

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,428


    Did you find this post helpful? Yes | No

    Default Re: 16Bit 125KHz PWM with PIC12F1572

    If the dutycycle is supposed to be fixed at 50% I don't see how it matters if you have 5, 6, 7 or even 16 bits of resolution.

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,281


    Did you find this post helpful? Yes | No

    Default Re: 16Bit 125KHz PWM with PIC12F1572

    A major limitation is that the chip does not support use of XTAL as a clock source. User needs to select either internal oscillator or external clock source, something that Jay is not fond of.

    I think crystal based clock is a must since the internal clock is specified at 2% and at 125Khz this means +/- 2.5KHz deviation.

    Ioannis

  9. #9
    Join Date
    Apr 2014
    Location
    Northeast
    Posts
    501


    Did you find this post helpful? Yes | No

    Default Re: 16Bit 125KHz PWM with PIC12F1572

    Just another thought, the PSMC Peripheral found on some PIC16F17XX parts (and probably others) should at least clock from XTAL. It's a 16-bit PWM type control strategy.

Similar Threads

  1. 125KHz HW PWM Output
    By Zebryk in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 28th June 2022, 01:56
  2. Replies: 4
    Last Post: - 13th April 2022, 08:33
  3. PBP for dsPIC 16bit??
    By photoelectric in forum PBP Wish List
    Replies: 33
    Last Post: - 9th July 2014, 08:13
  4. Replies: 1
    Last Post: - 4th March 2013, 21:57
  5. Replies: 3
    Last Post: - 12th March 2008, 05:33

Posting Permissions

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