+ Reply to Thread
Results 1 to 28 of 28
  1. #1
    Join Date
    May 2007
    Posts
    2

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

    hi, im looking the way to use the pwm's (no ccp) pins of this micro family.it has 5 or 9 outputs of this kind but i don't care about the frequency or assembler code. i just whant to know if picbasic pro support this pwm's pins.
    i dont what to use the darrel taylor rutines because there are in assembler and i just want to use picbasic!!!!!!!! (that's why im in this forum!!)
    thnks. a lot for this view

  2. #2

    Default HPWM use

    The manual with version 2.50 is vague on how many HPWM channels the new PBP or PBPL will support but it is at least 5 with the appropriate PIC.
    The manual is also vague on whether you can have different frequencies on different HPWM channels. This will relate to how the various timers are associated with the HPWM modules - read the data sheet for your particular PIC. The maximum frequency with PBP is 32767 Hz but with PBPL looks like it will be higher - vague manual again.

    I routinely use two channels HPWM in the 16F877A and both using the same frequency is not a problem in my case.

    HTH
    Brian
    Last edited by BrianT; - 14th September 2007 at 05:49. Reason: grammar

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    NO, PBP DOES NOT support the PWM/CAPTURE modules of those chips directly.

    Neither does any of the "darrel taylor rutines".

    But that doesn't mean you can't use them with PicBasic Pro.
    You just need to handle the registers manually.

    I think Bruce made an example or two.
    <br>
    DT

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

    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

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

    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

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

    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

  7. #7
    Join Date
    May 2007
    Posts
    2

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

  8. #8
    Join Date
    Jun 2007
    Posts
    56

    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

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

    Default

    Hi Johan,

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

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

  10. #10
    Join Date
    Jun 2007
    Posts
    56

    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

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

    Default

    You'll need to disable PWM for PORTD pins you're using to control your LCD. See if changing
    PWMCON0 = %01010100 to PWMCON0 = %01000100 helps. %01010100 turns them ALL on
    for the 18F4431.

    Also check your config settings. CONFIG3H has an option where you can force PWM4 output
    to be on RB5 or RD5. You'll obviously want this on RB5 since you're using RD5 for your LCD.
    Regards,

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

  12. #12
    Join Date
    Jun 2007
    Posts
    56

    Default

    Hi Bruce,

    I have tested it, the problem is the PWM7. How to disable it?

    I want PWM 1,3,5 only , but the PWMCON0 does not have option , the best option is %01111111 ( all odd pins enabled , independent )

    Is there any other way to disable PWM 7?

    Johan
    Last edited by Johan; - 26th October 2007 at 19:22.

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

    Default

    I don't have an 18F4431 to test, but I don't see any options for only 1,3,5 as PWM outputs.
    It appears from looking at PWMCON0 that a value of %01000100 would disable PWM outputs
    6 & 7, but I can't comfirm since I don't have one to test.
    Regards,

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

  14. #14
    Join Date
    Jun 2007
    Posts
    56

    Default

    Yes, this is the problem, if I switchoff PWM6,7 with %01000100 , I will loose the I/O ports that occupied by PWM0,2,4

    I think I have to change the LCD wiring ,
    or alternatively, can I combine 2 CCP PWM ( HPWM ) + 1 PCPWM ?

    Thanks,

    Johan

  15. #15
    Join Date
    Aug 2007
    Posts
    15

    Default help with 18f4431 hpwm

    '************************************************* ***************
    '* Name : UNTITLED.BAS *
    '* Author : [select VIEW...EDITOR OPTIONS] *
    '* Notice : Copyright (c) 2008 [select VIEW...EDITOR OPTIONS] *
    '* : All Rights Reserved *
    '* Date : 8/2/2008 *
    '* Version : 1.0 *
    '* Notes : *
    '* : *
    '************************************************* ***************
    ' pwm out
    ansel0=%00000000
    ansel1=%00000000


    portb=%00000000 'set port as output
    trisb=%00000000 'set all portb pins as output

    pr2=155 ' set to get 4khz out
    ccp1con=%0001101 'set duty cycle ccp1con <4:5> bits
    trisc=%00000000 ' make ccp pin output by clearning trisc<2> bit
    t2con =%00000110 'turn on timer2, prescale =16
    ' set trmr2 prescale value and enable timer2 t2con
    pause 1000

    portb.6 = 1 'high output
    portb.7=1 'high output


    pause 2000
    loop:
    hpwm 0,127,10
    hpwm 1,127,100
    hpwm 2,127,10
    hpwm 3,127,10
    hpwm 4,127,100
    hpwm 5,127,10
    pause 1000
    goto loop
    end
    I'm trying to get the pwm to work but the only pin that light a led is RB2( 6 and 7 work)
    Who do I get the rest of them to work?

  16. #16
    Join Date
    May 2007
    Location
    Area 71
    Posts
    52

    Default

    Read Darrel comment above.
    You have to setup manually, or you can use 2 CCP HPWM only with PBP

  17. #17
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    On these chips, I defer to Bruce's experience. (See post #4)

    Bruce,

    Is there really 2 normal CCP's and 4 other PWM's on these chips? Total of 6?
    Or am I looking at the datasheet wrong?
    The GIF image in post #4 shows 6 traces, but the program doesn't ues CCPCON?.

    Haven't used one yet.
    <br>
    DT

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

    Default

    DT,

    The 28-pin version has 6 power control pwm outputs, and 2 standard CCP pwm outputs
    for a total of 8. The 40-pin has 8 power control pwm outputs and the 2 standard CCP.

    The only thing I don't like is they use the CCP1 & CCP2 pwm outputs as power control
    pwm fault inputs.

    You should grab a couple samples. The motion feedback module on these is awesome.
    Regards,

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

  19. #19
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    WOW, that's a tough read.

    But after a few hrs in the datasheet. Yeah, these are cool.

    Got the (IC) Input Capture block: Very cool.
    The (QEI) Quadrature Encoder Interface: OK sorta cool.
    And up to 4 14-bit PWM's. (4 additional complimentary outputs) Not usable with CCP's at the same time.
    Not as cool as I expected.

    Still worth further investigation.
    Samples on order. Thanks Bruce.
    <br>
    DT

  20. #20
    Join Date
    May 2007
    Posts
    604

    Default

    How about a 28-pin PIC24FJxxGA002. 5 independent input capture AND 5 independent PWM's/OC (up to 16-bit resolution and 500kHZ). About $2 each (depending on flash from 16-64kB, 4kB or 8kB RAM). Plus lots of other stuff like 2 UART's, built-in RTCC, 2 SPI, 2 I2C, etc. Plus you can select which pins you want to use for most of the peripherals.

  21. #21
    Join Date
    Aug 2007
    Posts
    15

    Default mystified

    Thanks did not know that ccp and pwm0-5 are not the same pins that helped now how about setuo for timer 2.

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

    Default

    If you're using HPWM you don't need to mess with Timer2. It's automatically handled
    for you.

    If you plan to setup hardware PWM manually, these calcs should help.
    Code:
    PR2=(Fosc/(4*PS*Freq))-1
    TMR2 Prescaler = 1 PR2 = (8MHz/(4*1*12,000)) = (8MHz/48,000)-1 PR2 = 165.666
    TMR2 Prescaler = 4 PR2 = (8MHz/(4*4*12,000)) = (8MHz/192,000)-1 PR2 = 40.666
    TMR2 Prescaler = 16 PR2 = (8MHz/(4*16*12,000))-1 = (8MHz/768,000))-1 PR2 = 9.416
    
    CCPR1L:CCP1CON<5:4>=(PR2+1)*4*Duty%
    (165+1)*4*0.5 = 332
    (40+1)*4*0.5 = 82
    (9+1)*4*0.5 = 20
    
    To load the duty cycle registers just place the value calculated in a word, and
    do something like this;
    
    Duty = 332              ' Roughly 50% duty cycle
    CCP1CON.4 = Duty.0	' Setup 10-bit duty cycle as
    CCP1CON.5 = Duty.1	' a 10-bit word
    CCPR1L = Duty >> 2
    Search here for HPWM and you'll find a boat-load more examples.
    Regards,

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

  23. #23
    Join Date
    Jan 2010
    Posts
    10

    Default

    Hi Bruce,

    you mentioned above that the equation used to calculate the PWM resolution is

    log(20MHz/19.455kHz) 3.01
    Resolution = ------------------ = ----------- = 10 bits
    .301 .301

    In the 18F2431 datasheet it says that Fosc must be divided by four, i.e.

    log(5MHz/19.455kHz) 2.41
    Resolution = ------------------ = ----------- = 8 bits
    .301 .301

    and in other microchip document (link below) it provides the same equation you used.
    http://ww1.microchip.com/downloads/e...doc/31014a.pdf

    I am confused here! Can you please tell me what you think of that?

    Kind regards,

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

    Default

    Look at equation 17-4 PWM RESOLUTION. Then look at Table 17-2 just below it.

    Try plugging in some numbers on your calculator using both methods, and see what you get according to the resolutions shown in Table 17-2 for different values of Fosc, at various frequencies.
    Regards,

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

  25. #25
    Join Date
    Jan 2010
    Posts
    10

    Default

    You are right. So for 10 bits, thats 2 LSBs of PDCxH and 8 bits of PDCxL. Thanks.

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

    Default

    So for 10 bits, thats 2 LSBs of PDCxH and 8 bits of PDCxL
    PDCxH gets the 2 MSBs. PDCxL the lower 8-bits.
    Regards,

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

  27. #27
    Join Date
    Mar 2014
    Posts
    7

    Default Re: using the pwm's in pic18f2431-2331-4331-4431

    hello bruce
    i have a doubt abt carrier generation in 18f4431.sine wave ne how il generate through luk up table bt m nt bin able to generate carrier frequency of 20khz to compare it with ma sine wave.i'm attaching image for ur reference.can u plz help me in dis .Name:  carrier vs reference plot.JPG
Views: 1860
Size:  111.6 KB
    Last edited by park21; - 15th April 2014 at 10:59.

  28. #28
    Join Date
    Jan 2013
    Posts
    60

    Default Re: using the pwm's in pic18f2431-2331-4331-4431

    Hi,
    Finding this old thread may have answered some questions, I wanted to know, so thanks for it, especially Bruce.
    Cheers, Camerart.

Members who have read this thread : 20

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