Trying to get started w/ HPWM


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2008
    Location
    Los Angeles, CA
    Posts
    156

    Default Trying to get started w/ HPWM

    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.

  2. #2
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by circuitpro View Post
    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
    Last edited by ScaleRobotics; - 19th February 2010 at 07:12.

  3. #3
    Join Date
    Dec 2008
    Location
    Los Angeles, CA
    Posts
    156


    Did you find this post helpful? Yes | No

    Default

    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.

  4. #4
    Join Date
    Dec 2008
    Location
    Los Angeles, CA
    Posts
    156


    Did you find this post helpful? Yes | No

    Angry

    Oops. Missed the footnote on PortB.3!

  5. #5
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by ScaleRobotics; - 19th February 2010 at 19:30.

  6. #6
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by scalerobotics View Post

    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.

  7. #7
    Join Date
    Dec 2008
    Location
    Los Angeles, CA
    Posts
    156


    Did you find this post helpful? Yes | No

    Smile

    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!

  8. #8
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    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.




    Code:
    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
    Charles Linquist

  9. #9
    Join Date
    Dec 2008
    Location
    Los Angeles, CA
    Posts
    156


    Did you find this post helpful? Yes | No

    Default

    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.

  10. #10
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    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.
    Charles Linquist

  11. #11
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by circuitpro View Post
    ....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.

Similar Threads

  1. need help on hpwm
    By helmut in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 28th August 2007, 15:49
  2. HPWM of the port of two HPWM
    By ja2rqk in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 25th April 2007, 15:05
  3. sound command & music
    By trying in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 26th May 2006, 14:14
  4. HPWM and A to D interaction question 18F252
    By tcbcats in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 10th May 2006, 03:50
  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 : 1

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