Redefining HPWM as output


Closed Thread
Results 1 to 14 of 14
  1. #1

    Default Redefining HPWM as output

    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

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    what about if you place a LOW PORTB.2 after the last HPWM line???

    EDIT: i think i need to sleep.. See Alain solution...
    Last edited by mister_e; - 19th September 2005 at 15:48.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Wink stop HPWM

    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
    Last edited by Acetronics2; - 19th September 2005 at 15:33.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4


    Did you find this post helpful? Yes | No

    Lightbulb

    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,
    ---> picnaut

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Picnaut
    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
    Quote Originally Posted by TFM
    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.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by picnaut
    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
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  8. #8


    Did you find this post helpful? Yes | No

    Default

    Thank you Everybody. Bruce, I did the CCP1CON = 0 thing in place of my "zero frequency" command and it works like a charm.

  9. #9


    Did you find this post helpful? Yes | No

    Talking

    Bruce's approach sounds better to me.
    It probably uses less code than issuing the HPWM command again also.

    Cheers.
    ---> picnaut

  10. #10
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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
    Code:
    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  11. #11


    Did you find this post helpful? Yes | No

    Default

    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

  12. #12


    Did you find this post helpful? Yes | No

    Default

    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!
    ---> picnaut

  13. #13
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Works the same until CCP1CON = 0.

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

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  14. #14
    Stump's Avatar
    Stump Guest


    Did you find this post helpful? Yes | No

    Default hpwm

    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

Similar Threads

  1. Bit Banging input to output on PIC16F876A
    By Bronurstomp in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 26th June 2008, 19:50
  2. Multiple HPWM question
    By Johan in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th October 2007, 13:00
  3. Serious Serial Situation Setbacks...
    By Dansdog in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 7th February 2007, 03:46
  4. sound command & music
    By trying in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 26th May 2006, 14:14
  5. 3 HPWM channels
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th April 2006, 02:43

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts