New approach to Rotary Encoder


Closed Thread
Results 1 to 40 of 91

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Great, finally... Now try removing the PAUSE 15 and see if that improves it further.

  2. #2
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Thanks to Mr.Henrik Olsson this code work PERFECT !!! I'm sure I would not have done it without his help !
    Code:
    Main:
        PortA.1 = 0
        newState = PortA & 011100        
        PortA.1 = 1
    
        If newState <> 28 THEN
            If newState <> oldState THEN
                  
                Select Case oldState
                    Case 12
                    If NewState = 20 THEN dir=up       
                    If NewState = 24 THEn dir=dn
                    
                    Case 20
                    If NewState = 24 THEN dir=up               
                    If NewState = 12 THEN dir=dn
                   
                    Case 24
                    If NewState = 12 THEN dir=up                       
                    If NewState = 20 THEN dir=dn
                END SELECT
    
                GOSUB UpOrDn 
    
        PortA.1 = 0
        OldState = PortA & 011100         
        PortA.1 = 1
                   
           endif          
        ENDIF
    Goto Main

  3. #3
    Join Date
    Sep 2011
    Posts
    5


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Hi to all. I want to use a rotary encoder with a 18f252, and i have a lot of troubles. I want to use interupts, so my only solution PORTB or I can use Portc also?

    I made a pcb and I made the connections to PORTC, and I also use a debounce circuit, which I think causes problems. Anyone has a snipset code cause I do not have any expieriance with encoders?
    5v
    __|__
    | |
    / /
    \ \
    /10k / 10K
    \ \
    | A | B ENC
    - -
    | |-------------------------------
    \ |
    /10K \
    \ /10K
    / \
    |--------------------- |--------------------
    PORTC.2 | PORTC.3 |
    ---- ----- 100nF
    ---- -----
    | |
    --------- -----------
    ----- -----
    - -
    Last edited by kzeoprf; - 21st August 2012 at 10:49.

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Hello,
    On the 18F252 there's no interrupt capabillity on PortC.

    Your schematic really doesn't show up that good here but it LOOKS like you have a 10k from 5V to the "top" of each of the switches in the encoders, then the other side of each switch thru another 10k to the input with a 100nF cap to GND, is that correct?

    If the above is not correct please post a proper schematic for us to see.
    If the above IS correct then it's not a very good hardware design IMHO. When the switch in the ecoder is closed you have 2*10k in series to the input, decoupled by a 100nF cap, this may be fine. But when the switch is open there's no path for the capacitor to discharge except thru the fairly high input impedance of the pin. In effect the pin floats which is not a good thing for inputs.

    /Henrik.

  5. #5
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    I finally got around to trying my rotary encoders out on an 18F24K22 using DT interrupts.

    I kept things simple; checking for only 1 movement per click (detent). This encoder also has a momentary ON switch.


    Code:
    '***************************************************************************
    '*  Name    : 18F24K22 encoder.pbp                                         *
    '*  Author  : Demon                                                 *
    '*  Date    : Sep 5 2012                                                   *
    '*  Version : 1.0                                                          *
    '*  Hardware : PIC 18F24K22, internal oscillator, 8 MHz                    *
    '*           : ICSP                                                        *
    '*           : MeLabs U2 Programmer v4.32                                  *
    '*  Software : PIC Basic Pro v2.60C                                        *
    '*           : MicroCode Studio Plus v2.2.1.1                              *
    '*           : MPASM v5.46                                                 *
    '*  CONFIG   : - if you use these, you must comment the ones in the        *
    '                .INC file in PBP folder                                   *
    '              - available options at the bottom of P*.INC file in         *
    '                MPLAB TOOLS/MPASM SUITE folder                            *
    '***************************************************************************
    asm
     __CONFIG    _CONFIG1H, _FOSC_INTIO67_1H & _PLLCFG_OFF_1H & _PRICLKEN_ON_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
     __CONFIG    _CONFIG2L, _PWRTEN_ON_2L & _BOREN_SBORDIS_2L & _BORV_285_2L
     __CONFIG    _CONFIG2H, _WDTEN_OFF_2H
     __CONFIG    _CONFIG3H, _PBADEN_ON_3H & _HFOFST_OFF_3H & _MCLRE_EXTMCLR_3H
     __CONFIG    _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
    endasm
    
    ;--- Oscillator speed ------------------------------------------------------
    
    DEFINE OSC 8
    CLEAR
    
    ;--- Setup Interrupts ------------------------------------------------------
    INCLUDE "DT_INTS-18.bas"        ; Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas"     ; PBP Re-entry for external interrupt
    
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler    INT_INT,  _ExternalInterrupt0,   PBP,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    
    @   INT_ENABLE   INT_INT     ; enable external (INT) interrupts
    
    ;--- Setup Registers -------------------------------------------------------
    
    OSCCON = %01100110              ' OSCILLATOR CONTROL REGISTER
    OSCTUNE = %00000000             ' OSCILLATOR TUNING REGISTER
    
    PMD0 = %11111111                ' PERIPHERAL MODULE DISABLE REGISTER 0
    PMD1 = %11111111                ' PERIPHERAL MODULE DISABLE REGISTER 1
    PMD2 = %00001111                ' PERIPHERAL MODULE DISABLE REGISTER 2
    
    INTCON2=%10000100               ' INTERRUPT CONTROL 2 REGISTER
    
    ANSELA = 0                      ' Digital I/O
    ANSELB = 0                      ' Digital I/O
    ANSELC = 0                      ' Digital I/O
    
    ;--- Setup Port directions -------------------------------------------------
    
    TRISA = %00000000               ' Set port A pins to output
    TRISB = %00000001               ' Set port B pin 0 to input, others output
    TRISC = %00000110               ' Set port C pins 1,2 to input, others output
    
    ;--- Pins ------------------------------------------------------------------
    
    WiperA      VAR PortB.0         ' Interrupt 0
    WiperB      VAR PortC.2
    
    Switch      VAR PortC.1
    
    LedPower    VAR PortB.5
    
    ;--- Variables -------------------------------------------------------------
    
    LEDS        VAR BYTE            ' 8 Leds connected on Port A
    
    ;--- Program Start ---------------------------------------------------------
    
    start:
    
    Pause 200                       ' Let PIC stabilize
    
    Ledpower = 0
    
    Leds = %00000001
    PortA = leds
    
    ;--- The Main Loop ---------------------------------------------------------
    
    mainloop:
    
      if Switch = 1 then
        Ledpower = 1                    ' Turn ON Led
        pause 200                       ' Debounce
        Ledpower = 0                    ' Turn it back OFF
      endif
    
      Goto mainloop
    
    end
    
    '---[INT - interrupt handler]---------------------------------------------------
    ExternalInterrupt0:
      if wiperb = 1 then
        if leds.7 = 0 then
          LEDS = LEDs << 1              ' Leds blink counter-clockwise
        endif
      else
        if leds.0 = 0 then
          leds = leds >> 1              ' Leds blink clockwise
        endif
      endif
    
      PortA = leds
      pause 100
    @ INT_RETURN

    I used both 0.01uF and 0.1uF caps to improve debounce and wired the C pin straight to Vss (only check wiper A and B). This is the recommended circuit by the manufacturer:

    Name:  Encoder debounce.JPG
Views: 13160
Size:  39.3 KB

    Robert
    Last edited by Demon; - 4th October 2016 at 17:35.

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Nice try Robert.

    I only object to the use of Pause 100 inside the ISR.

    Ioannis

  7. #7
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,172


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Quote Originally Posted by Ioannis View Post
    Nice try Robert.

    I only object to the use of Pause 100 inside the ISR.

    Ioannis

    I had first added PAUSE to reduce jitter, the only debounce I could think of on short notice. But adding 0.1uF caps as suggested in this thread was the key. I left the 0.01uF caps in, they can't hurt.

    I now removed the PAUSE and it works even better (faster response time).

    Thanks!

    Robert
    Last edited by Demon; - 7th September 2012 at 22:55. Reason: type faster than my brain

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