how to read ky40 mechanical encoder ?


Closed Thread
Results 1 to 21 of 21

Hybrid View

  1. #1
    Join Date
    May 2013
    Location
    australia
    Posts
    2,631


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    Code:
    ToggleLED1:
         if b=0 then cnt=cnt-1
         IF B=1 THEN CNT=CNT+1
     @ INT_RETURN
    I found that strategy gave quite disappointing results , was jerky missed counts and had sudden and unexpected direction reversals. there are better ways

  2. #2
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    I am sorry Richar for having posted the file that is not the final one:
    please change the following:

    A VAR PORTD.0 ' encoder A PHASE
    shAll be:
    A VAR PORTB.0

    This is the interrupt input !

    I would be happy to know ...
    Thanks and regards,
    Ambrogio

  3. #3
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    I did capture the output of the terminal printout for a better check.
    There are about 200 changes of the cnt obtained by "manually" turning the encoder knob on my control panel. Probably sometims cw and ccw.
    I noted very few spikes : I am not shure if they are due to the time required for the serial port output or to the encoder software.
    Any improvement to the program is very much appreciated.
    I am really searching for a good solution.
    Thanks
    regards,
    Ambrogio

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,631


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    I do it this way

    enc pins must be sequential and on same port
    both enc pins must be able to generate an interrupt (ioc on neg and pos edge is best , ioc, rac,rbc on neg or pos edge will work as will int0 int1)
    enc pins must be pulled up (10k) and have 0.1uF filter caps

    I have an asm version that's 100 bytes smaller and has about 1 tenth of the irq latency and is much faster too


    Code:
    '****************************************************************
    '*  Name    : rotary encoder.BAS                                *
    '*  Author  : richard                                           *
    '*  Notice  : Copyright (c) 2013                                *
    '*          : All Rights Reserved                               *
    '*  Date    : 9/4/2013                                          *
    '*  Version : 1.0    16f1825                                    *
    '*  Notes   :  enca porta.2 encb porta.3                        *
    '*          :   note mclr disabled ,full quad decode            *
    '****************************************************************
      
    #CONFIG
                 __config        _CONFIG1,    _FOSC_INTOSC & _CP_OFF & _WDTE_ON  &  _PWRTE_ON  &  _MCLRE_OFF  & _CLKOUTEN_OFF
                  __config      _CONFIG2, _PLLEN_ON & _LVP_OFF
    #ENDCONFIG
      
     
    include "dt_ints-14.bas"
    include "REENTERPBP.bas"
     
    asm
    INT_LIST macro
          INT_HANDLER IOC_INT, _enc_isr,pbp,NO
          endm
     INT_CREATE
    ENDASM    
     
    DEFINE OSC 32
     
     
     
     
       OSCCON=$70
       ANSELA=0
       
       OPTION_REG.6=0
     
       ev    VAR BYTE bank0
       cnt   VAR WORD bank0
       TMP   VAR BYTE bank0
       enca  VAR porta.2 
       encb  VAR porta.3
       TRISA = 111110
      
       lata.0=1
       IOCAP=12    ;enca porta.2 encb porta.3  pos edge trigger
       IOCAN=12    ;enca porta.2 encb porta.3  neg edge trigger
        
        
       pause 4000 
       
       serout2 PORTA.0,84,[ "ready",13,10]
       IOCAF=0
       
       INTCON=$C8
       
       
       
    mainloop:  
     
     serout2 PORTA.0,84,[13,10, "C",#CNT]
     pause 1000
     
     goto mainloop
        
     
        
     
    enc_isr:
        
    
    ev=ev<<2              ;shift last reading into position
    ev=ev|((porta&12)>>2)  ;read enc pins 2,3  mask off others then  combine this reading with last reading
    LOOKUP (ev&$f),[0,255, 1, 0, 1, 0, 0, 255, 255, 0, 0, 1, 0, 1, 255, 0],tmp ;lookup the decision matrix
    if tmp then         ; decide to inc , dec or ignore 
     if tmp.7 then
       cnt=cnt-1
     else
        cnt=cnt+1
     endif
    endif
    intcon.0=0                   ; clean up and exit
    iocaf=0
    @ INT_RETURN

  5. #5
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    Thanks Richard for the code.
    I am just a little bit busy on these days and so I plan to try your code on next days.
    As you have seen I am using pic18f452: Is your code applicable to this micro ?
    If no, what should I modify or adapt ?
    Thanks
    Regards,
    Ambrogio

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,631


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    for a pic18f452 you need to use RBC_INT for best results and pins portb.4,5 or portb.6,7 or portb.5,6

    this line needs to match chosen p1ns

    ev=ev|((porta&12)>>2) ;read enc pins 2,3 mask off others then combine this reading with last reading
    for pins portb.4,5 it would be

    ev=ev|((portb&48)>>4)
    Code:
    INT_LIST macro
          INT_HANDLER IOC_INT, _enc_isr,pbp,NO
          endm
     INT_CREATE
    would be
    Code:
    INT_LIST macro
          INT_HANDLER RBC_INT, _enc_isr,pbp,NO
          endm
     INT_CREATE
    IOCAP=12 ;enca porta.2 encb porta.3 pos edge trigger
    IOCAN=12 ;enca porta.2 encb porta.3 neg edge trigger
    IOCAF=0
    INTCON=$C8
    would be
    dummy_var = portb
    INTCON=$C8

    any references to IOCAF need to be deleted , trisb must be appropiate

  7. #7
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    Hi Richard,
    I am going to modify my previous program with your indications.
    Probably I did something wrong and it is not working.
    I am attaching the full code : could you please have a look of it ?
    Thanks again for the assistance
    regards,
    Ambro
    Attached Files Attached Files

Similar Threads

  1. Question about mechanical relay
    By Megahertz in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 1st October 2012, 10:37
  2. Mechanical question
    By muddy0409 in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 11th October 2011, 05:41
  3. Probe to read quadrature encoder with DT_INT need help
    By phoenix_1 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 31st August 2009, 20:43
  4. Mechanical question
    By selbstdual in forum Off Topic
    Replies: 10
    Last Post: - 8th February 2008, 14:34
  5. Replies: 6
    Last Post: - 14th October 2005, 19:41

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