Trying to get started w/ HPWM


Closed Thread
Results 1 to 11 of 11

Hybrid View

  1. #1
    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

  2. #2
    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.

  3. #3
    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

  4. #4
    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