using the pwm's in pic18f2431-2331-4331-4431


Closed Thread
Results 1 to 28 of 28

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    HPWM is for control of CCP PWM. Not Power Control PWM. To use PCPWM you'll need to do
    things manually.

    The example below is for the 18F2431. It's all in PBP.
    Code:
     
        DEFINE LOADER_USED 1
        DEFINE OSC 20
        
        ' At 20MHz, to figure a PWM frequency of 19.455kHz
        '
        ' TPWM = time period of PWM frequency
        ' PTPER = 12-bit period register PTPERL and PTPERH
        ' PTMRPS = PWM time base prescaler
        ' 
        '        (PTPER+1)*PTMRPS         257
        ' TPWM = ----------------  =  ------------ = 0.0000514 
        '             Fosc/4            5000000
        '
        ' Frequency = 1/TPWM = 1/0.0000514 = 19.455kHz
        '
        ' PWM resolution (bits resolution for duty cycle)
        '
        '              log(20MHz/19.455kHz)    3.01
        ' Resolution = ------------------ = ----------- = 10 bits
        '                    .301              .301
          
        ' so we'll need a word sized var for Duty
        Duty Var Word
       
        PORTB = 0               ' clear port latch
        TRISB = %11000000       ' PWM0,1,2,3,4,5 outputs
        
        TRISC = 2               ' RC1 = FLTA input (ground RC1 to halt PWM)
                                ' RC1 should be pulled high for normal PWM operation
                                ' when fault A is enabled.
        ' PCPWM init
        DTCON = %00000101       ' ~500nS dead-time (for complementary outputs only)
        PTCON0 = %00000000      ' 1:1 postscale, Fosc/4 1:1 prescale, free running mode
                                ' PTCON0 = %00000100 would give 19.45kHz/4
        PTPERL = 0              ' 
        PTPERH = 1              ' PTPER = $0100 or 256d for ~19.45kHz
    	
        ' PWM4,5 independent, PWM0,1,2,3 complementary
        PWMCON0 = %01010100     ' PWM[5:0] outputs enabled
        PWMCON1 = 1             ' updates enabled, overrides sync w/timebase
        PTCON1 = %10000000      ' PWM time base is ON, counts up
        FLTCONFIG = %00000011   ' enable fault A, cycle-by-cycle mode
    
        Duty = 800              ' ~50%
        PDC2L = Duty.LowByte    ' maintain a fixed 50% duty cycle on PWM4,5
        PDC2H = Duty.HighByte   ' independent PWM outputs.
            
    Main: ' ramps up & down duty-cycle on complementary PWM0,1,2,3 outputs
      For Duty = 800 To 200 STEP-1 ' ~20% to 80%
        PDC0L = Duty.LowByte
        PDC0H = Duty.HighByte
        PDC1L = Duty.LowByte
        PDC1H = Duty.HighByte
        Pause 5
      Next Duty
      
      Pause 500 ' 1/2 second delay between ramp up/down
      
      For Duty = 200 To 800   ' ~80% to 20%
        PDC0L = Duty.LowByte
        PDC0H = Duty.HighByte
        PDC1L = Duty.LowByte
        PDC1H = Duty.HighByte
        Pause 5
      Next Duty
    
      Pause 500 ' 1/2 second delay between ramp up/down
      
      GoTo Main
    
      End
    The .gif attached shows all 6 PWM channels captured with a logic analyzer. PWM0 to PWM3
    operate in complementary mode. PWM4 and PWM5 operate in independent mode.

    PCPWM is a tad more complex than CCP PWM, but with a little time spent experimenting and
    going over the data sheet, it's fairly easy to figure out.
    Attached Images Attached Images  
    Regards,

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

  2. #2
    Join Date
    Aug 2005
    Location
    NC
    Posts
    8


    Did you find this post helpful? Yes | No

    Default What Logic Analyzer?

    Bruce,

    What Logic Analyzer did/do you use, that you posted the screen capture (I'm making at least one or two assumptions here...) in the above post? A BitScope? or something else? I'm in the market for a USB MSO (I have a Tek 1220 LA and two Tek Scopes, but they're all analog with no comms for a PC for data analysis, etc.).

    Thanks,

    Brad

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


    Did you find this post helpful? Yes | No

    Default

    Hi Brad,

    The DigiView shown here http://www.tech-tools.com/dv_main.htm

    I have the DV1-100 shown in the hand.
    Regards,

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

  4. #4
    Join Date
    May 2007
    Posts
    2


    Did you find this post helpful? Yes | No

    Talking thank's for this reply

    thanks all you guys for ypur time to answer this threat!!.
    but i think i will gonna use the code you post for checkin!!!!, thanks a lot for this forum!
    thanks all your guys!!!

  5. #5
    Join Date
    Jun 2007
    Posts
    56


    Did you find this post helpful? Yes | No

    Default

    Hi Bruce,

    I also got the PWM's working, but there is a problem now : LCD displays doesn't work properly, say I want to display " Welcome " , LCD displays " We$&ome" or something like that
    If I delete the PWM config & code, LCD working fine.

    Johan

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


    Did you find this post helpful? Yes | No

    Default

    Hi Johan,

    Which port/ports are you using for the LCD?
    Regards,

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

  7. #7
    Join Date
    Jun 2007
    Posts
    56


    Did you find this post helpful? Yes | No

    Default

    I am using RD2,RD3,RD4,RD5,RD6,RD7 ( cannot change these , as these are hardwired on my easypic4 )

    The problem starts even before PWM channels are active

Members who have read this thread : 2

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