how to read ky40 mechanical encoder ?


Closed Thread
Results 1 to 21 of 21

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    Is your encoder like this ?
    http://www.ebay.com/itm/400964660325...%3AMEBIDX%3AIT

    These encoders are (supposedly) destroyed by pullup resistors.

    Or is your encoder just a noisey old mechanical switch type?
    Comparators are supposed to be good buffers for mech switches.

    OK I found my answer, Mech. as stated in your post
    You are needing to "debounce" these.
    Last edited by Archangel; - 11th October 2015 at 21:21.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  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 ?

    this is my ecoder:
    http://www.ebay.com/itm/1PCS-KY-040-...item27e8f12b89

    Any help , please ?
    Thanks
    Ambrogio

  3. #3
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    CAUTION: UNTESTED CODE
    Code:
    main:
    
    A var portb.6
    B var portb.7
    state var byte
    if(( A == 1)&&(B==1)) then state = 1
    if(( A == 1)&&(B==0)) then state = 2
    if(( A == 0)&&(B==1)) then state = 3
    if(( A == 0)&&(B==0)) then state = 4
    ;pause 100
    if state = 1 then portC = %00000001
    if state = 2 then portc = %00000010
    if state = 3 then portc = %00000100
    if state = 4 then portc = %00001000
    
    debug "  state = ",#state
    
    
    goto main
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

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


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    I found this interesting.
    http://makeatronics.blogspot.it/2013...ture-with.html

    is your "re " a 20 count per turn with indents ? the 10 for a $1 ones I got from ebay have enough contact bounce to start a spring factory but the inherent software filtering of the above code does an amazing job on them 1 (4k7 pu r's and .1uFs included). I have not ported this to pbp but have for mikro c and xc8 if that helps

    http://www.circuitsathome.com/mcu/re...age-1#comments
    this link has some other insights

    basically you just need to adjust your re count using this current value / old value matrix (illegal values [bounces] are just ignored )
    Attached Images Attached Images  

  5. #5
    Join Date
    Sep 2009
    Posts
    755


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    One of possible ways to implement filter(Not tested)
    Code:
    OutputValue=0
    Select Case OldValue
    Case 0
    If Current=1 then OutputValue=-1
    If Current=2 then OutputValue=1
    Case 1
    If Current=3 then OutputValue=-1
    If Current=0 then OutputValue=1
    Case 2
    If Current=0 then OutputValue=-1
    If Current=3 then OutputValue=1
    Case 3
    If Current=2 then OutputValue=-1
    If Current=1 then OutputValue=1
    EndSelect

  6. #6
    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 ?

    still not able to make my panel encoder working .
    Could someone post the complete code applicable to mechanical encoder like my one please ?
    Thanks in advance.
    regards,
    Ambro

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,656


    Did you find this post helpful? Yes | No

    Default Re: how to read ky40 mechanical encoder ?

    post the code you are trying , and a bit of info about your re setup .

    does it have detents ?
    how many edges per turn ?
    pull ups ?
    filter caps ?
    pic type ?
    pins used ?

    still not able to make my panel encoder working
    is not much to work with

  8. #8
    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 for the assistance.
    My encoder has detents and has about 24 edges per turn.
    I used a pull up of 2x 10 k on both encoder phases and a 2x 0.1uF to ground.
    I am using pic18f452 , xtal=10 Mhz, ppl X 4 to get 40 Mhz clock speed.
    I used RB0 for the "A" phase as shown on the code.
    At the end of the world I am now able to make it working well using the following picbasic program by including Darrel Taylor interrupt routine.
    The program prints counter results on a terminal @ 115200 baud rate 8,n,1.
    Regards, Ambro


    DEFINE OSC 40 ' XTAL= 10 MHZ CLOCK= 40 MHZ
    INCLUDE "MODEDEFS.BAS"
    INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas" ; Include if using PBP interrupts
    INTCON2.BIT6=0 ' interrupt from 1 down to 0 on RB0

    CNT VAR WORD ' COUNTER TO BE DISPLAYED ON TERMINAL
    CNT= 32000
    CNTOLD VAR WORD
    CNTOLD=32000

    B VAR PORTD.1 ' encoder B PHASE
    A VAR PORTD.0 ' encoder A PHASE

    ' DEBUG INTERFACE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\

    '' Set Debug pin port ________________________________________
    DEFINE DEBUG_REG PORTD
    '' Set Debug pin bit _________________________________________ PIC_PIN_30
    DEFINE DEBUG_BIT 7
    '' Set Debug baud rate _________________________________________
    DEFINE DEBUG_BAUD 115200
    '' Set Debug mode: 0 = true, 1 = inverted ______________________
    DEFINE DEBUG_MODE 1

    DEBUG 13,10,13,10, " START OF PROGRAM ", 13, 10, 13, 10

    ADCON1=7 ' all digital !

    ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
    INT_Handler INT_INT, _ToggleLED1, PBP, yes
    endm
    INT_CREATE ; Creates the interrupt processor
    ENDASM

    @ INT_ENABLE INT_INT ; enable external (INT) interrupts

    Main:
    PAUSE 1
    IF CNT <> CNTOLD THEN
    DEBUG DEC CNT,13,10
    CNTOLD=CNT
    ENDIF

  9. #9
    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 ?

    sorry for not having paste the complete code...



    DEFINE OSC 40 ' XTAL= 10 MHZ CLOCK= 40 MHZ
    INCLUDE "MODEDEFS.BAS"
    INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas" ; Include if using PBP interrupts
    INTCON2.BIT6=0 ' interrupt from 1 down to 0

    CNT VAR WORD ' COUNTER TO BE DISPLAYED ON TERMINAL
    CNT= 32000
    CNTOLD VAR WORD
    CNTOLD=32000

    B VAR PORTD.1 ' encoder B PHASE
    A VAR PORTD.0 ' encoder A PHASE

    ' DEBUG INTERFACE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\

    '' Set Debug pin port ________________________________________
    DEFINE DEBUG_REG PORTD
    '' Set Debug pin bit _________________________________________ PIC_PIN_30
    DEFINE DEBUG_BIT 7
    '' Set Debug baud rate _________________________________________
    DEFINE DEBUG_BAUD 115200
    '' Set Debug mode: 0 = true, 1 = inverted ______________________
    DEFINE DEBUG_MODE 1

    DEBUG 13,10,13,10, " START OF PROGRAM ", 13, 10, 13, 10

    ADCON1=7 ' all digital !

    ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
    INT_Handler INT_INT, _ToggleLED1, PBP, yes
    endm
    INT_CREATE ; Creates the interrupt processor
    ENDASM

    @ INT_ENABLE INT_INT ; enable external (INT) interrupts

    Main:
    PAUSE 1
    IF CNT <> CNTOLD THEN
    DEBUG DEC CNT,13,10
    CNTOLD=CNT
    ENDIF

    GOTO Main

    '---[INT - interrupt handler]---------------------------------------------------
    ToggleLED1:
    if b=0 then cnt=cnt-1
    IF B=1 THEN CNT=CNT+1
    @ INT_RETURN

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