PDA

View Full Version : Multiple HPWM's 16F877



emmett brown
- 5th October 2006, 21:43
Am I at all close? I can get it to compile without it but adding a
"For HPWMFREQ2 leaves me in the dark as two FOR NEXT loops is not correct but both timers need to change frequency at the same time. Thanks again to the best support group I've ever known!


DEFINE LOADER_USED 1
DEFINE ONINT_USED 1
DEFINE OSC 10
DEFINE CCP1_REG PORTC 'Hpwm 1 pin port pin 17 rc2 ccp1
DEFINE CCP1_BIT 2 'Hpwm 1 pin bit
DEFINE ADC_BITS 12 ' Set number of bits in result 8-10-12
DEFINE ADC_CLOCK 3 ' Set clock source (rc = 3)
DEFINE ADC_SAMPLEUS 500 ' Set sampling time in microseconds


HPWMFREQ VAR WORD
HPWMFREQ2 VAR WORD
DUTY VAR BYTE
DUTY2 VAR BYTE

CNTRL var word 'ADC BEGINNING SAMPLE
TST var word 'ADC COMPARISON SAMPLE

HPWMFREQ=1500 'CURRENT FREQUENCY OF HPWM
HPWMFREQ2=3000 'CURRENT FREQUENCY OF HPWM
CNTRL=0 'ADC CONTROL
TST=0 'ADC TEST SAMPLE
TRISA = 255 'ADC Set PORTA to all input
ADCON1 = 2

BEGIN:
hpwm 1,DUTY, HPWMFREQ
HPWM 2,DUTY2,HPWMFREQ2 'OR HPWMFREQ/2?
ADCIN 0, CNTRL
For HPWMFREQ = 1500 To 2000 Step 100
'FOR HPWMFREQ2=3000 to 4000 Step 200
DUTY=65
DUTY2 = 65
HPWMFREQ=HPWMFREQ+1
HPWMFREQ2=HPWMFREQ2+1
ADCIN 0, CNTRL
ADCIN 0, TST
IF TST < CNTRL THEN
HPWMFREQ=HPWMFREQ +1
HPWMFREQ2=HPWMFREQ2+1
Endif
ADCIN 0, TST
if TST >= CNTRL then
HPWMFREQ=HPWMFREQ +1
HPWMFREQ2=HPWMFREQ2+1
ENDIF
NEXT HPWMFREQ
goto BEGIN:
end

HenrikOlsson
- 6th October 2006, 06:17
Hi,
When you add the FOR HPWMFREQ2=3000 to 4000 Step 200 you also need to add a NEXT HPWMFREQ2

But even when you do it won't work. The manual says:


Some devices have 1, 2 or 3 PWM channels. On devices with 2 channels, the Frequency must be the same on both channels.


/Henrik Olsson

emmett brown
- 7th October 2006, 02:08
But does it mean that the chips with three pwm's need to be the same frequency? I'll try it in the simulator then run it on the chip Thanks much for your time! Emmett

emmett brown
- 10th October 2006, 01:37
Hi group, The PBP manual says that the highest frequency at the HPWM's at any oscillator speed is 32767Hz. Is this correct? I'm looking for 48Khz. Also It says that if two are used they have to be the same frequency. The same frequency if I use three? Don't understand the limitations. Thanks again all! Emmett

mister_e
- 10th October 2006, 02:35
It could depend of the PIC too.

BTW @48MHZ, the highest frequency possible by using the calc should be 'round 5 MHZ (even if i have some doubt), 32.xxxKHZ is really low.

HPWM is certainelay limited. To avoid this limitation, you have to write to the CCP register.
Here's a short code snip for 2.5MHZ @ 50% duty cycle on a 16F877.


DEFINE OSC 20
TRISC=0
DUTY VAR WORD

T2CON=%00000100
PR2=1
DUTY=4
CCP1CON=%00001100 ' PWM MODE
CCP1CON.5=DUTY.1
CCP1CON.4=DUTY.0
CCPR1L=DUTY>>2

START:
PAUSE 1
GOTO START
<IMG SRC="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1113&stc=1&d=1160445821">