18F4431 Quaduature Encoder


Closed Thread
Results 1 to 29 of 29

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,627


    Did you find this post helpful? Yes | No

    Default

    Hi,
    This works for me on 18F2431, should be the same. Don't forget to set the QEA, QEB & QEI pins to inputs.
    Code:
    QEICON = %00010100                      'Setup QEI, 4X mode, reset on indexpulse.
    
    PosHigh var BYTE
    PosLow var BYTE
    PosTemp var BYTE
    
    Position var WORD                       'Motor position in encoder counts.
    
    Main:
      Gosub GetPosition
      HSEROUT [#Position,13,10]
      Pause 100
    Goto Main
    
    GetPosition:
    'Since the position counter isn't double buffered we can get to situations
    'where the lowbyte overflows between readings of the high and low byte. This
    'is prevented by reading the high byte two times and compare the two readings.
    'If they are the same then we're fine, if not re-read the registers.
      
        PosHigh = POSCNTH                       'Get high byte of counter
        PosLow = POSCNTL                        'Get low byte of counter
        PosTemp = POSCNTH                       'Get high byte again
        
        If PosHigh - PosTemp = 0 then Goto Done  'Compare, if equal we're done.
        
        PosHigh = POSCNTH                       'If not, get values again.
        PosLow = POSCNTL
    
    Done:
        Position = PosHigh * 256 + PosLow       'Put high and lowbyte together.
    RETURN
    /Henrik Olsson.
    Last edited by HenrikOlsson; - 19th February 2007 at 21:31. Reason: Code errror fixed.

  2. #2
    Join Date
    Jan 2007
    Posts
    8


    Did you find this post helpful? Yes | No

    Default

    Henrik,
    Thank you for the code sample

  3. #3
    Join Date
    Jan 2007
    Posts
    8


    Did you find this post helpful? Yes | No

    Default

    Henrik,
    The problem was that I did not set
    the ANSEL0 register to digital i/o.

    Great tip on counter overflow.
    I will definately incorporate.

    Thank You again.
    Richard

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


    Did you find this post helpful? Yes | No

    Default

    Isn't that an awesome controller? The motion feedback module with pulse width & frequency measurement is really handy.

    Note: On the 28-pin version, forget using RE3 as an input. I tried, and submitted a support ticket to Microchip. Here's what I received back;
    RE3 pin in PIC18F2331/2431 has been removed as a digital input. Hence, pin no. 1 of PIC18F2331/2431 can be used for MCLR/ Vpp only.

    This change will be notified in a datasheet's (DS39616B.pdf) future update.
    Last edited by Bruce; - 19th February 2007 at 23:36.
    Regards,

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

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,627


    Did you find this post helpful? Yes | No

    Default

    Hi Bruce,
    Thank you for info regarding RE3/MCLR as input.

    It sure is a cool device. Using Darrel's interupt routines I managed to get motor controller for my robot working, with PID filter and all. Very fun project.

    However.....I never managed to get QEI module going in velocity mode. I made a few attempts to begin with but could never quite figure it out so I went for my own velocity calcs (newPos-oldPos). Any pointers?

    Thanks!
    /Henrik Olsson.

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


    Did you find this post helpful? Yes | No

    Default

    Hi Henrik,

    I haven't messed with QEI yet, but if I do get something going, I'll port it from C to PBP and post it here.

    My primary interest in this one was the new high-speed A/D, and motion feedback module for background pulse & freq measurements.
    Regards,

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

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,627


    Did you find this post helpful? Yes | No

    Default

    Bruce,
    I see. Thanks anyway!

    /Henrik Olsson.

Similar Threads

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

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