PDA

View Full Version : 16Bit 125KHz PWM with PIC12F1572



Zebryk
- 4th July 2022, 22:03
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

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

richard
- 5th July 2022, 02:08
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 .

Zebryk
- 5th July 2022, 21:44
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,

richard
- 6th July 2022, 02:14
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

tumbleweed
- 9th July 2022, 15:28
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.

mpgmike
- 10th July 2022, 22:04
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.

HenrikOlsson
- 11th July 2022, 07:57
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.

Ioannis
- 11th July 2022, 09:24
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

mpgmike
- 11th July 2022, 20:27
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.