PDA

View Full Version : Trying to get started w/ HPWM



circuitpro
- 18th February 2010, 22:50
I have a 18F67J60, and I'm trying to understand how to use the HPWM output on PORTB.3. Because I've not used HPWM before, getting started is very confusing. The datasheet refers to this pin as "ECCP2 Enhanced PWM output, channel A", but the PBP manual is talking about Channel NUMBERS.

Another concern is that I'm using DT-INTs and using TMR0 and TMR1 already. Is this a problem? Any pointers to get me started in the right direction would sure be appreciated.

ScaleRobotics
- 19th February 2010, 06:54
I have a 18F67J60, and I'm trying to understand how to use the HPWM output on PORTB.3. Because I've not used HPWM before, getting started is very confusing. The datasheet refers to this pin as "ECCP2 Enhanced PWM output, channel A", but the PBP manual is talking about Channel NUMBERS.
Your channel numbers are 1, 2, 3, 4, and 5 for ECCP1, ECCP2, ECCP3, CCP4 and CCP5. But none of them are on port B.3. They are a little all over the place on the chip. But ECCP1 is on PortC.2, and ECCP2 is on PortC.1



Another concern is that I'm using DT-INTs and using TMR0 and TMR1 already. Is this a problem? Any pointers to get me started in the right direction would sure be appreciated.
Not a problem, you will be ok here because PWM uses Timer 2 or Timer 4

circuitpro
- 19th February 2010, 17:03
I guess it's time to just get a stand-alone HPWM working on the right port/pins, and then try to integrate it with what I've got already got. Thanks.

circuitpro
- 19th February 2010, 18:32
Oops. Missed the footnote on PortB.3!

ScaleRobotics
- 19th February 2010, 18:45
Hey Circuitpro,

Yes, the PBP "HPWM" command uses the hardware PWM pins on the chip. These are defined in the datasheet. To use "HPWM", you need to use one of the available HPWM pins.

You could use the PBP "PWM" command, but this does not work for continuous PWM, only a defined number of cycles.

Your only option (if you need to get continuous PWM on portb.3) is to use DT Interrupts and make your own pwm on any pin ... but this is a little more complicated. (maybe you could still use the same board if you do this ... not quite sure what your options are with your particular project.)

Edit: forgot about this option, Darrel has made it easier for everyone. http://darreltaylor.com/DT_INTS-14/SPWM.html

ScaleRobotics
- 19th February 2010, 20:00
Edit: forgot about this option, Darrel has made it easier for everyone. http://darreltaylor.com/DT_INTS-14/SPWM.html

But this uses Timer1, so I guess you are on the DIY route with interrupts, or DT Interrupts.

circuitpro
- 19th February 2010, 22:00
I was thrown this PWM 'addition' after the finished pcb was delivered, so will have to have a gleep or two on the board anyway, so I'll just use one of the dedicated hardware ports this time (for continuous LED brightness). Thanks for the info on the DT-PWM's too!

Charles Linquis
- 20th February 2010, 02:10
This is part of some code that I posted a long time ago in the "code examples" section of this forum.

Writing directly to the hardware registers offers much more flexibility than using the PBP HPWM command.

It was written for use with an 18F872x chip running either at 20Mhz or 40Mzh

It was written for use with HYPERTERMINAL, and recognizes the ESC codes for the VT-xxx type terminals. Once you select the PWM frequency, you can use the UP and DOWN arrows on the keyboard to change the duty cycle.

You might have to change a few things, since this is only a segment of a larger program, but it should show you what is going on.







PWMTester:

HSEROUT [13,10,10,"Test PWM ",13,10]

HSERin [Char]
HSEROUT [13,REP 10\4]

IF Char !=13 THEN
GOTO PWMTester
ENDIF

HSEROUT ["Pick PWM Frequency 1 = 312Khz,2 = 156Khz, 3 = 78Khz, 4 = 39Khz "]
HSERin [Char]
IF Char < "1" or Char > "4" THEN
HSEROUT [13,10,10]
GOTO PWMTester
ENDIF

IF Mhz40 = 1 THEN ; Set MHz = 1 if running 40 Mhz, to "0" if running 20Mhz
Select CASE Char
CASE "1"
PWMFREQ = $1F
NumBits = 7
MaxPWM = 127
CASE "2"
PWMFREQ = $3F
NumBits = 8
MaxPWM = 255
CASE "3"
PWMFREQ = $7F
NumBits = 9
MaxPWM = 511
CASE "4"
PWMFREQ = $FF
NumBits = 10
MaxPWM = 1023
END SELECT
ELSE
Select CASE Char
CASE "1"
PWMFREQ = $F
NumBits = 6
MaxPWM = 63
CASE "2"
PWMFREQ = $1F
NumBits = 7
MaxPWM = 127
CASE "3"
PWMFREQ = $3F
NumBits = 8
MaxPWM = 255
CASE "4"
PWMFREQ = $7F
NumBits = 9
maxPWM = 511
END SELECT
ENDIF


CCP1CON = %00001100
CCPR1L = %00010000
CCPR1H = %00000000 ' Initialize to 0, PWM register
PR2 = PWMFREQ
T2CON = %00000100 ' Prescale 1, Timer2 ON - Needed for PWM
PORTC.2 = 0
TRISC.2 = 0
PWMVal = 0
Gosub WritePWMreg
HSEROUT [13,10,10]

HSEROUT ["PWM Value (",#MaxPWM," Max)",13,10]
HSEROUT ["0",8]
NewChar:
HSERIN [Char]
If Char = 27 THEN
HSERIN 50,NoArrow,[Char2,Char3]
IF Char3 = 65 AND PWMVal < MaxPWM tHEN
PwmVal = PWMVal + 1
ENDIF
IF Char3 = 66 AND PWMVal > 0 tHEN
PwmVal = PWMVal - 1
ENDIF
HSEROUT [#PWMVal," ",REP 8\7]
GOSUB WritePWMReg
ENDIF

goto NewChar
NoArrow:
Break = 1
PWMVal = 0
Gosub WritePWMReg
CCP1CON = 0 ; Shut off the PWM controller
GOTO PWMTester



WritePWMReg:
CCPR1L = PWMVal >> 2
CCP1CON.5=PWMVal.1
CCP1CON.4=PWMVal.0
RETURN

circuitpro
- 20th February 2010, 18:04
WOW. Thanks for the serious 'pointer'! Ha. I just assumed that the HPWM command was the way to go. I haven't studied this long enough to know better, but I saw a code example on melab's own website that used this method, and never once used HPWM - I kept looking for the HPWM command.

Trying to direct the a PWM output to an available/unused pin now is what's concerning me, but am about to do some reading on the CCPMX & CONFIG1 commands, and hopefully will find the answers.

Your code is a great help, and I'll stop looking at 'HPWM'. THANKS.

Charles Linquis
- 20th February 2010, 18:36
The PBP HPWM command only gives something like 32Khz max PWM frequency. Although that is fine for a few things, writing directly to the registers allows frequencies to 416K and above.

Your chip's datasheet is your friend here. It explains the PR2 value vs. max resolution. For example: In the PIC18F8722 datasheet, look up table 17-3.

rmteo
- 20th February 2010, 18:42
....Trying to direct the a PWM output to an available/unused pin now is what's concerning me.....
Unless you are using a device that has PPS (Peripheral Pin Select for mapping digital peripherals to various I/O for design flexibility) such as the 28/44-pin J11 and J50 series, the PWM outputs are fixed.