PDA

View Full Version : Redefining HPWM as output



peterdeco1
- 19th September 2005, 11:41
Hello Everyone. I have a 16F818 and am using it's PWM on Rb2 to generate tones, coupling it through a FET (N channel). The drain on the FET goes to a speaker terminal. The other speaker terminal goes to B+. It works perfectly except, a couple of seconds after playing the tone, Rb2 goes HIGH causing the FET to turn on and the speaker consumes a lot of power. If you look at the START of my program you will see that I attempted to convert Rb2 as output low. It does NOT. I only included NOTE1 to shorten this post. Any suggestions? Thank you.

OSCCON = $60 '4mhz 'set int osc to 4mhz
adcon1 = 7 ' set inputs to digital
DEFINE CCP1_BIT 2 'PWM OUTPUT ON Rb2
TRISA = %11111111 'INPUT SWITCHES
TRISB = 0 'PORTB OUTPUT
PORTB = 0 'ALL LOW
@ DEVICE MCLR_OFF, INTRC_OSC, WDT_ON, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_ON
Pause 500

START:
PORTB = 0 'ALL LEDS OFF
Output PORTB.2 'ATTEMPT TO REDEFINE RB2 AS OUTPUT LOW DOESN'T WORK
Low PORTB.2
NAP 2
IF PORTA.1 = 1 Then NOTE1
IF PORTA.0 = 1 Then NOTE2
IF PORTA.7 = 1 Then NOTE3
IF PORTA.6 = 1 Then NOTE4
IF PORTA.2 = 1 Then NOTE5
IF PORTA.3 = 1 Then NOTE6
IF PORTA.4 = 1 Then NOTE7
GoTo START

NOTE1:
HPwm 1,127,522 "PLAY SOUND
High PORTB.7 "ON LED
IF PORTA.1 = 1 Then NOTE1 'IF SWITCH ON KEEP PLAYING
HPwm 1,127,0 'TURN OFF SOUND
GoTo START

mister_e
- 19th September 2005, 15:17
what about if you place a LOW PORTB.2 after the last HPWM line???

EDIT: i think i need to sleep.. See Alain solution...

Acetronics2
- 19th September 2005, 15:30
Hi, Peterdeco

Melabs says : to stop HPWM : HPWM 1, 0, xxxx ( duty cycle to zero ! )

That's all !!!

Now, if you can decently explain what PBP does when you ask for 0 Hz freq ...

you could also disable the CCP1 module ... but why ???

Alain

picnaut
- 19th September 2005, 20:55
Hi,

Do you have to change the frequency to zero?
I wouldn't.
Anything below the minimum allowed frequency can cause strange results with the HPWM command.
I think if you simply issue the command at the frequency you've been using, but at a 0% duty cycle, it will be solid OFF. If you issue the command with a 100% duty cycle (255 if 8-bit) it will be solid ON.

Does anybody see any problem with this approach?

Regards,

mister_e
- 19th September 2005, 22:13
Does anybody see any problem with this approach?

Can't say... i never use HPWM. Still prefer to write directly to the internal register. BUT, as Alain said and AS per the book


Dutycycle 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.

Bruce
- 20th September 2005, 01:02
If you want to turn OFF hardware PWM & have a solid logic output on the CCP pin, don't rely on using the HPWM command with a zero duty-cycle. Just use CCP1CON = 0 to disable the PWM module, then set/clear the pin as required.

After clearing CCP1CON, you can simply issue the HPWM command again, and PBP will automatically configure CCP1CON for PWM mode.

Some I/O-pins are multiplexed with peripherals. When these peripherals are enabled, they may "override" the TRIS bit for a pin. Even though the HPWM command does leave the TRIS bit set to output, and it leaves a zero in the output latch, if the PWM module is still enabled, and not outputting a PWM signal, you'll see a high impedance on the CCP output pin.

Acetronics2
- 20th September 2005, 06:26
Hi,

I think if you simply issue the command at the frequency you've been using, but at a 0% duty cycle, it will be solid OFF. If you issue the command with a 100% duty cycle (255 if 8-bit) it will be solid ON.

Does anybody see any problem with this approach?

Regards,

Hi, Picnaut

this approach runs perfectly well, and it is recommended by Melabs itself ...as I use HPWM for R/C E-motors , I just clear CCP1CON when I'm not really really sure a glitch could give a false value to duty cycle. ( never saw it ...but i'm sure !!! )

Alain

peterdeco1
- 20th September 2005, 09:54
Thank you Everybody. Bruce, I did the CCP1CON = 0 thing in place of my "zero frequency" command and it works like a charm.

picnaut
- 20th September 2005, 15:57
Bruce's approach sounds better to me.
It probably uses less code than issuing the HPWM command again also.

Cheers.

Bruce
- 20th September 2005, 19:04
Here's a simple test to show the effects of disabling the hardware PWM module, or using a zero duty-cycle or freq with HPWM.

Hook an LED up to your CCP out pin.

CCP output pin ---|>|----/\/\/\/\----GND


SYMBOL PIN = PORTC.2 ' Define whatever pin on your PIC is CCP out here

HPWM 1,127,2000
PAUSE 8000
CCP1CON = 0 ' Test once with this, then comment it out
'HPWM 1,0,0 ' Now test with this un-commented

WAITUP:
HIGH PIN
PAUSE 500
LOW PIN
PAUSE 500
GOTO WAITUP
On a 16F876A, the LED never toggles with HPWM 1,0,0. It does once the hardware PWM module is disabled with CCP1CON = 0. I'm curious if it works either way on other PIC's.

picnaut
- 20th September 2005, 20:48
Hi Bruce,

Try it again, but this time leave the frequency at 2000 (only the duty cycle is 0). See if it toggles then.
I'm curious (and no where near a PIC right now).

Cheers!

picnaut
- 20th September 2005, 20:51
Hi Bruce,

Also, maybe put a 500ms delay right after the "WAITUP" label. Otherwise, if PIN goes low, it will immediately go high again and you won't catch it.


Cheers!

Bruce
- 20th September 2005, 22:16
Works the same until CCP1CON = 0.

Another delay isn't really necessary since it never toggles the LED to begin with.

Stump
- 21st September 2005, 08:18
I find that setting duty to 0 allows the pin to float
and that can cause a fet to turn on.
I add a weak pull down resistor to the pin used
or set the output to low when duty=0