18F4431 Quaduature Encoder


Results 1 to 29 of 29

Threaded View

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


    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.

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