18F4431 Quaduature Encoder


Closed Thread
Results 1 to 29 of 29

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    I don't have the 18F4431 or Microchip motor control board, but this works
    on the 18F2431. It outputs PWM on RB0,RB1,RB2,RB3,RB4 and RB5 with a
    varying duty cycle. Ramps up, then down.

    It should be at least enough to get you started.
    Code:
        DEFINE OSC 20
        
        Duty VAR WORD
        PORTB = $FF
        TRISB = 0
        
        PTCON0=$00 'page 8,step 1
        PTPERL=$A0      ' $01A0 = 12kHz
        PTPERH=$01
        PWMCON0=%01011110 'would be %01011111 for the 18F4431
        PWMCON1=%00000001 'step 4
        DTCON=$00
        ;OVDCOND=$00 'step 6  ; May want to un-comment these based on your PIC & board.
        ;OVDCONS=$00
        ;FLTCONFIG=%10110011
        ;POVD=00000000
        ;SEVTCMP=$0000
        ;step 8
        SEVTCMPL=$00
        SEVTCMPH=$00
        PDC0L=$00
        PDC0H=$00
        PDC1L=$00
        PDC1H=$00
        PDC2L=$00
        PDC2H=$00
        ;PDC3L=$FF ; un-comment for 18F4431
        ;PDC3H=$00
        PTCON1=%10000000
        
    Main:
        FOR Duty = 50 TO $500 STEP 4
            ASM
            bsf   PWMCON1,UDIS	    ;Disable the PWM buffer update
            MOVFF _Duty,PDC0L
            MOVFF _Duty+1,PDC0H
            MOVFF _Duty,PDC1L
            MOVFF _Duty+1,PDC1H
            MOVFF _Duty,PDC2L
            MOVFF _Duty+1,PDC2H
            ;MOVFF _Duty,PDC3L      ;un-comment for 18F4431
            ;MOVFF _Duty+1,PDC3H
            bcf	  PWMCON1,UDIS	    ;Enable the PWM buffer update
            ENDASM
            PAUSE 50
        NEXT Duty
        FOR Duty = $500 TO 50 STEP-4
            ASM
            bsf	  PWMCON1,UDIS	    ;Disable the PWM buffer update
            MOVFF _Duty,PDC0L
            MOVFF _Duty+1,PDC0H
            MOVFF _Duty,PDC1L
            MOVFF _Duty+1,PDC1H
            MOVFF _Duty,PDC2L
            MOVFF _Duty+1,PDC2H
            ;MOVFF _Duty,PDC3L      ;un-comment for 18F4431
            ;MOVFF _Duty+1,PDC3H
            bcf	  PWMCON1,UDIS	    ;Enable the PWM buffer update
            ENDASM
            PAUSE 50
        NEXT Duty
        GOTO Main
        
        END
    This is just a slight modification to what you posted before. To work with the
    18F2431. I haven't done much beyond this with the PCPWM module. Not yet
    at least..;o}

    HTH
    Regards,

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

  2. #2
    Join Date
    Oct 2004
    Location
    Hangover, Germany
    Posts
    289


    Did you find this post helpful? Yes | No

    Smile

    I'm using the 18F2431 too.
    And I begun with the PICDEm MC LV board...

    It's a great uC ! (UART, I2C, fast AD, 3-times PWM...)
    I use it as a PI(D)-Controller for a BLDC-Motor.
    And I use it for his fast AD-Converters in a CR-Tuning-Box.

    The Quadrature-Encoder isn't very strange ... the whole business with this chip is to use the right timers for the right tasks and use the right prescalers !!

    ;-)
    PBP 2.50C, MCS+ 3.0.0.5, MPLAB 8, MPASM 5.14, ASIX Presto, PoScope, mE mikroBasic V7.2, PICKIT2

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


    Did you find this post helpful? Yes | No

    Default

    Yeah this one is a nifty series PIC for sure. Sampling 4 x A/D inputs at once is
    a very handy feature.

    See this post http://www.picbasic.co.uk/forum/showthread.php?t=6768

    I just received a few samples of the new USB 18F2553. 12-bit A/D on this
    one is nice, but the 18F2431 is still my favorite. Now, if I can just find more
    time to play with it..;o}
    Regards,

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

  4. #4
    Join Date
    Jun 2006
    Posts
    37


    Did you find this post helpful? Yes | No

    Default

    Thanks a lot Bruce.
    I just want to get started with PPWM and then build my own code for brushless DC motor control.
    Thanks for your code although its in assembly.i will figure it out.

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


    Did you find this post helpful? Yes | No

    Default

    Here's the BASIC equivalent commands to the right of the assembly version.
    Code:
    FOR Duty = 50 TO $500 STEP 4
            ASM
            bsf   PWMCON1,UDIS    ; PWMCON1.1 = 1
            MOVFF _Duty,PDC0L     ; PDCOL = Duty.LowByte
            MOVFF _Duty+1,PDC0H   ; PDC0H = Duty.HighByte
            MOVFF _Duty,PDC1L     ; PDC1L = Duty.LowByte
            MOVFF _Duty+1,PDC1H   ; PDC1H = Duty.HighByte
            MOVFF _Duty,PDC2L     ; PDC2L = Duty.LowByte
            MOVFF _Duty+1,PDC2H   ; PDC2H = Duty.HighByte
            ;MOVFF _Duty,PDC3L    ; PDC3L = Duty.LowByte
            ;MOVFF _Duty+1,PDC3H  ; PDC3H = Duty.HighByte
            bcf   PWMCON1,UDIS    ; PWMCON1.1 = 0
            ENDASM
            PAUSE 50
        NEXT Duty
    bsf PWMCON1,UDIS just sets bit 1 in PWMCON1. bcf PWMCON1,UDIS then
    clears the UDIS bit.

    MOVFF is move file to file.

    MOVFF _Duty,PDC0L moves the low byte of Duty into file register PDC0L.

    MOVFF _Duty+1, PDC0H moves the high byte of Duty into file register PDC0H.

    Placing the underscore before the _Duty variable allows you to access the
    variable declared in BASIC with assembler.

    So the BASIC version would look like this;
    Code:
      FOR Duty = 50 TO $500 STEP 4
        PWMCON1.1 = 1
        PDC0L = Duty.LowByte
        PDC0H = Duty.HighByte
        PDC1L = Duty.LowByte
        PDC1H = Duty.HighByte
        PDC2L = Duty.LowByte
        PDC2L = Duty.HighByte
        PDC3L = Duty.LowByte
        PDC3H = Duty.HighByte
        PWMCON1.1 = 0
        PAUSE 50
      NEXT Duty
    
      FOR Duty = $500 TO 50 STEP-4
        PWMCON1.1 = 1
        PDC0L = Duty.LowByte
        PDC0H = Duty.HighByte
        PDC1L = Duty.LowByte
        PDC1H = Duty.HighByte
        PDC2L = Duty.LowByte
        PDC2L = Duty.HighByte
        PDC3L = Duty.LowByte
        PDC3H = Duty.HighByte
        PWMCON1.1 = 0
        PAUSE 50
      NEXT Duty
    Regards,

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

  6. #6
    Join Date
    Jun 2005
    Location
    Penang
    Posts
    40


    Did you find this post helpful? Yes | No

    Default Hmm..

    Bruce,

    That's a super start to a beginner like me.

    But,i gotta some problem with the PWM Period value.I mean how to find the values for .. say...a PIC18F4431 running at OSC 20 , wish to set base period 19.5KHZ.How to find this using equation 17-1 ( i use this equ since it's free running mode here).Apart from that , how to calculate the min & max value of dutycycle?.Dohh...it's really bothering me no matter how many times i try it.

    Could you guide me how to do this.I am a bit confused.The rest seems yeeha as it's taught in "863 MTR" from Microchip.


    Regards.

  7. #7
    Join Date
    Jun 2005
    Location
    Penang
    Posts
    40


    Did you find this post helpful? Yes | No

    Default Anyone...

    Anyone..know how to do it?.

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


    Did you find this post helpful? Yes | No

    Default

    Code:
           (PTPER+1) x PTMRPS
    TPWM = ------------------
                Fosc/4
    Equation 17-1 assumes you know the value in PTPER.

    TPWM is the period of the PWM frequency.
    PTPER is the value loaded into the PWM period register.
    PTMRPS is the PWM timer prescaler. 1, 4, 16 or 64.

    1/19,500 = 0.000051282. This is the period for a PWM frequency (TPWM) of 19.5kHz.

    At 20MHz the instruction cycle time = 1/5MHz = 200nS.

    To find the value to load into PTPER, divide the PWM period by the instruction cycle time.

    0.000051282/0.0000002 = 256.410. Subtract 1 for 255.

    If you load 255 into the period register, it will generate a PWM frequency of around 19.5kHz.

    PTPERL=$FF ' low byte = $FF
    PTPERH=$00 ' high byte = $00

    Assuming a 20MHz osc, 1:1 prescaler, Fosc/4 = 5Mhz, so plug this into equation 17-1.
    Code:
    TPWM = (255+1) * prescaler
           -------------------
                 Fosc/4
    TPWM = 256/5MHz = 0.0000512.
    Frequency = 1/TPWM so 1/0.0000512 = 19.531kHz.

    The period register is 12-bits wide, so the minimum period for a given oscillator speed = the
    instruction cycle time * ($0FFF+1) * the prescaler.

    I.E. at 40MHz the instruction cycle time = 1/10Mhz = 100nS.

    100ns * $1000 = 0.0004096. 1/0.0004096 = 2.441kHz.

    At 10MHz 1/2.5Mhz = 400nS. 400nS * $1000 = 0.0016384.

    1/0.0016384 = 610Hz, etc, etc..

    You can see from Table 17-1 how the PWM timer prescaler affects the PWM frequency.
    I.E. 2.441kHz/64 = 38Hz, etc,..

    Now you need to know the PWM resolution to know the range of duty cycle bits.

    Note: Resolution is the number of bits you have to control the duty cycle. Not the PWM
    frequency.

    For PWM resolution use the equation below. Equation 17-3 'as shown in the data sheet'
    doesn't produce the same figures for PWM resolution shown in Table 17-2.
    Code:
                   log(Fosc/Fpwm)
    Resolution = ------------------
                       log(2)
    
    At 20MHz, with a PWM frequency of 19.5kHz;
    
                 log(20MHz/19.5kHz)   3.010995384
    Resolution = ------------------ = ----------- = 10.003
                       .301               .301
    So we 10-bit resolution. The value loaded into duty cycle registers would range from 0 to
    1023 or $00 to $03FF.

    512 loaded into the duty registers should give ~50% duty at 19.5kHz.

    There's a lot more to the PCPWM module than this, but it should help you get
    started. I haven't played much with this particular feature.
    Regards,

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

Similar Threads

  1. Quadrature encoder and ASM Interrupts. questions..
    By godfodder in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 17th March 2013, 15:45
  2. Instant Int and encoder (Elect. Gearing)
    By boroko in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 29th November 2009, 03:29
  3. Reset timer / encoder transition
    By DJEE in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 22nd June 2007, 15:37
  4. encoder HEDL 5540
    By kutsi in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 11th June 2007, 15:00
  5. encoder wowes
    By wallaby in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 6th December 2005, 22:56

Members who have read this thread : 3

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