Log in

View Full Version : HPWM the hard way



markk5xh
- 6th February 2008, 20:21
I need to be able to use the 'Enhanced Capture/Compare/PWM' module in a 16F684 to drive a H bridge. I have, in the past, used the HPWM command to drive the PA1 output but now I need to be able to use the full-bridge outputs of the ECCP module. I am assuming this means that I need to write my own initialization routine. I started by simply trying to duplicate the HPWM command but I cannot get it to work. I am using the steps shown on page 84 of the 684 manual. Can someome point out the setp(s) that I'm missing?

Thanks,
Mark


@ device pic16f684, INTRC_OSC_NOCLKOUT
@ device pic16f684, WDT_ON
@ device pic16f684, PWRT_ON
@ device pic16f684, MCLR_OFF
@ device pic16f684, BOD_ON
@ device pic16F684, CPD_OFF
@ device pic16f684, PROTECT_OFF

define OSC 8

TRISA = %00000000
TRISC = %00000000
CMCON0 = %00000111

OSCCON = %01110000 '8 MHz internal osc
ANSEL = %00000000 'All Digital
ADCON0 = %00000000 'All Digital

'Test to set the hardware PWM for an output
'at 16 KHz and about 50% duty cycle
'PR2 = 124.
'Timer2 prescale = 1

TRISC.5 = 1 'SET CCP1 AS INPUT
PR2 = %01111100 'SET FREQ
T2CON = %00000000 'Stop TIMER2 and SET PRESCALE to 1
CCP1CON = %00001100 'set the CCP1CON
CCPR1L = %10000000 'set to 50% duty cycle
PIR1.2 = 0 'CLEAR THE TIMER2 INT FLAG
T2CON.2 = 1 'START TIMER2
TRISC.5 = 0 'Set CCP1 to output

'Can't get the above to work but...
' This line works as expected if uncommented.
' HPWM 1,127,1000

WHILE 1
WEND

END

Darrel Taylor
- 6th February 2008, 21:02
Set CCP module to full-bridge mode...

CCP1CON = %01001100 'set the CCP1CON
For 50% duty cycle, CCPR1L should be half of PR2

PR2 = 124 'SET FREQ
CCPR1L = 62 'set to 50% duty cycle

Added: For Full-bridge mode, you'll probably need to change the PWM1CON value to limit transition currents.

hth,