The modules can be used alone or together to provide up to three channels of independent PWM (if your device supports it).
Among some of the new features are:
- adds support for many J, K, and Q family devices, including those with PPS
- #options to assign output pin, CCP, and TMR peripheral and other settings
- all modules support using choice of CCP1/CCP2/CCP3 and TMR2/TMR4/TMR6 (if available)
- supports devices with standard prescaler settings 1/4/16 or expanded prescalers 1/2/4/8/16/32/64/128
- user callback event to allow customizing the setup (assign PPS pins, etc)
- SetFreq allows up to Fosc/4 output freq (16MHz at clock=64)
- low-level SetPWM(period, prescaler, duty) function for custom settings
Other than new options for setup (and new functions), its basic operation is similar to the original.
Here are a few examples...
example 1 - basic functions
Code: Select all
// HPWM example using CCP1 and TMR2
device = 18F26K22
clock = 32
include "intosc.bas"
#option DIGITALIO_INIT = true
include "setdigitalio.bas"
#option HPWM_CCP = 1 // CCP1 (module default)
#option HPWM_TMR = 2 // TMR2 (module default)
#option HPWM_PIN = PORTC.2 // CCP1 output pin (26K22, module default)
#option HPWM_DEFAULT_DUTYCYCLE = 50 // new default duty cycle for SetFreq
include "hpwm.bas"
dim fact, fmin, fmax as longword // freq, in hz
dim duty, maxduty as word
dim pr as byte // period reg
dim prescaler as byte // prescaler (1/4/16 or 1/2/4/...128)
dim i as byte
dim stat as boolean
main:
// get min and max pwm freq
fmin = hpwm.MinFreq()
fmax = hpwm.MaxFreq()
// set pwm to fmax, default duty cycle
hpwm.SetFreq(fmax)
// get details
fact = hpwm.Freq() // actual freq
pr = hpwm.getPR() // period reg
prescaler = hpwm.GetTMRPrescaler() // timer prescaler setting
maxduty = hpwm.MaxDuty() // max duty setting
duty = hpwm.GetDuty() // current duty setting
// set pwm to 10KHz, 25% duty
stat = hpwm.SetFreq(10000, 25)
// get details
fact = hpwm.Freq() // actual freq
pr = hpwm.getPR() // period reg
prescaler = hpwm.GetTMRPrescaler() // timer prescaler setting
maxduty = hpwm.MaxDuty() // max duty setting
duty = hpwm.GetDuty() // current duty setting
// stop pwm
hpwm.Stop()
// set pwm using period, prescaler, and duty cycle (low-level)
hpwm.SetPWM(pr, prescaler, duty)
hpwm.Start() // SetPWM requires calling Start
// step duty cycle, in percent
for i = 0 to 100 step 25
hpwm.SetDutyPercent(i)
duty = hpwm.GetDuty() // get duty cycle word
delayms(10)
next
// step duty cycle value, 0 to max
for duty = 0 to hpwm.MaxDuty()
hpwm.SetDuty(duty)
delayms(10)
next
// stop pwm
hpwm.Stop()
// for each prescaler range, get the max and min freqs
prescaler = 1
repeat
hpwm.setPWM(0, prescaler) // set max freq
fmax = hpwm.Freq()
hpwm.setPWM(255, prescaler) // set min freq
fmin = hpwm.Freq()
prescaler = hpwm.GetNextPrescaler(prescaler)
until (prescaler = 0)
Code: Select all
// HPWM2 example using CCP2, TMR4, and PPS
device = 18F26K40
clock = 32
include "intosc.bas"
#option DIGITALIO_INIT = true
include "setdigitalio.bas"
include "pps.bas"
#option HPWM2_CCP = 2 // CCP2 (module default)
#option HPWM2_TMR = 4 // TMR4 (module default)
#option HPWM2_PIN = PORTC.1 // CCP2 output pin
include "hpwm2.bas"
// user callback event to set CCP2 PPS
// (this assignment must match '#option HPWMx_PIN' setting)
event user_ccp2()
pps.unlock()
pps.assign_output(RC1PPS, PPS_CCP2) // CCP2 -> RC1
end event
main:
// setup event
hpwm2.SetEvent(user_ccp2)
// set pwm to 10KHz, 25% duty
hpwm2.SetFreq(10000, 25)
Code: Select all
// HPWM/HPWM2/HPWM3 with PPS
device = 18F26K42
clock = 64
include "intosc.bas"
#option DIGITALIO_INIT = true
include "setdigitalio.bas"
include "pps.bas"
#option HPWM_CCP = 1
#option HPWM_TMR = 2
#option HPWM_PIN = PORTC.2
include "hpwm.bas"
#option HPWM2_CCP = 2
#option HPWM2_TMR = 4
#option HPWM2_PIN = PORTC.1
include "hpwm2.bas"
#option HPWM3_CCP = 3
#option HPWM3_TMR = 6
#option HPWM3_PIN = PORTB.5
include "hpwm3.bas"
// user callback events to set CCP PPS
// (these assignments must match '#option HPWMx_PIN' settings)
event user_ccp1()
pps.unlock()
pps.assign_output(RC2PPS, PPS_CCP1) // CCP1 -> RC2
end event
event user_ccp2()
pps.unlock()
pps.assign_output(RC1PPS, PPS_CCP2) // CCP2 -> RC1
end event
event user_ccp3()
pps.unlock()
pps.assign_output(RB5PPS, PPS_CCP3) // CCP3 -> RB5
end event
main:
// setup events
hpwm.SetEvent(user_ccp1)
hpwm2.SetEvent(user_ccp2)
hpwm3.SetEvent(user_ccp3)
// turn off autostart for all modules
hpwm.Autostart(false)
hpwm2.Autostart(false)
hpwm3.Autostart(false)
// setup all three pwm modules
hpwm.SetFreq(100000, 25)
hpwm2.SetFreq(200000, 50)
hpwm3.SetFreq(300000, 75)
// start all three pwm channels running
hpwm.start()
hpwm2.start()
hpwm3.start()
There's bound to be some devices that aren't supported, so if your favorite doesn't work let me know and we'll add it.