Positron Compiler Documentation

HPWM

Positron16 · Revision 1.1.3.5 · PDF page 174

HPWM Channel, DutyCycle, Frequency

Output a pulse width modulated pulse train using on of the OCP modules. The PWM pulses produced can run continuously in the background while the program is executing other instructions.

Channel is a constant value that specifies which hardware PWM channel to use (1 to 5). must be the same on all channels. It must be noted, that this is a limitation of the devices not the compiler. The data sheet for the particular device used shows the fixed hardware pin for each Channel. DutyCycle is a variable, constant (0-255), or expression that specifies the on/off (high/low) ratio of the signal. It ranges from 0 to 255, where 0 is off (low all the time) and 255 is on (high) all the time. A value of 127 gives a 50% duty cycle (square wave). Frequency is a variable, constant (0-65535), or expression that specifies the desired frequency of the PWM signal. Not all frequencies are available at all oscillator settings. The highest frequency at any oscillator speed is 65535Hz. The lowest usable Hpwm Frequency at each oscillator setting is dependant on the oscillator frequency that the device is operating with.

  Device = 24FJ64GA002           ' Select the device to compile for
  Declare Xtal = 32

  CLKDIV = 0                   ' CPU peripheral clock ratio set to 1:1
  Write_OSCCONH(%00010000)     ' Enable PLL

  HPWM 1,127,1000              ' Send a 50% duty cycle PWM signal at 1KHz
  DelayMs 500
  HPWM 1,64,2000              ' Send a 25% duty cycle PWM signal at 2KHz
'
' For internal oscillator with PLL
'
  Config Config1 = JTAGEN_OFF, GCP_OFF, GWRP_OFF, BKBUG_OFF, COE_OFF,_
                   ICS_PGx1, FWDTEN_OFF, WINDIS_OFF, FWPSA_PR128, WDTPOST_PS256
  Config Config2 = IOL1WAY_OFF, COE_OFF, IESO_OFF, FNOSC_FRCPLL,_
                   FCKSM_CSDCMD, OSCIOFNC_ON, POSCMOD_NONE

Some devices have alternate pins that may be used for Hpwm. The following Declares allow the use of different pins: -

Declare CCP1_Pin Port.Pin    ' Select Hpwm port and bit for OCP1 module.
Declare CCP2_Pin Port.Pin    ' Select Hpwm port and bit for OCP2 module.
Declare CCP3_Pin Port.Pin    ' Select Hpwm port and bit for OCP3 module.
Declare CCP4_Pin Port.Pin    ' Select Hpwm port and bit for OCP4 module.
Declare CCP5_Pin Port.Pin    ' Select Hpwm port and bit for OCP5 module.

See also : PWM, PulseOut, Servo.

View original PDF page 174Open page in PDFOriginal PDF page 174