New approach to Rotary Encoder - Page 3


Closed Thread
Page 3 of 3 FirstFirst 123
Results 81 to 91 of 91
  1. #81
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    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

  2. #82
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588


    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

  3. #83
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,588


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Changes:

    - used a low priority interrupt for the switch to free up Main Routine for other logic.
    - added comments for the registers.
    - used constants (I remember something about improved efficiency, this is just to get in the habit).
    - PAUSE in low priority interrupt does not cause problems.

    Robert

    Code:
    '***************************************************************************
    '*  Name    : 18F24K22 encoder.pbp                                         *
    '*  Author  : Demon                                                 *
    '*  Date    : Sep 5 2012                                                   *
    '*  Version : 1.0                                                          *
    '*  Notes   : Test program                                                 *
    '*  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 ------------------------------------------------------
    DEFINE USE_LOWPRIORITY 1
    
    INCLUDE "DT_INTS-18.bas"        ; Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas"     ; PBP Re-entry for external interrupt
    INCLUDE "ReEnterPBP-18LP.bas"   ; PBP Re-entry for low priority external interrupt
    
    ASM
    ;----[High Priority Interrupts] --------------------------------------------
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler    INT0_INT,  _ExternalInterrupt0,   PBP,  yes
        endm
        INT_CREATE                  ; Creates the interrupt processor
    
    ;----[Low Priority Interrupts] ---------------------------------------------
    INT_LIST_L  macro  ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler    INT1_INT,  _ExternalInterrupt1,   PBP,  yes
        endm
        INT_CREATE_L                ; Creates the Low Priority interrupt processor
    
    ;----[Enable Interrupts] ---------------------------------------------------
        INT_ENABLE   INT0_INT       ; Enable (INT0) external interrupt
        INT_ENABLE   INT1_INT       ; Enable (INT1) external interrupt
    ENDASM
    
    INTCON2.7=1
    '   bit 7   RBPU: PORTB Pull-up Enable bit
    '               1 = All PORTB pull-ups are disabled
    '               0 = PORTB pull-ups are enabled provided that the pin is an input and the
    '                   corresponding WPUB bit is set.
    
    INTCON2.6=0
    '  bit 6   INTEDG0: External Interrupt 0 Edge Select bit
    '               1 = Interrupt on rising edge
    '               0 = Interrupt on falling edge
    
    INTCON2.5=1
    '  bit 5   INTEDG1: External Interrupt 1 Edge Select bit
    '               1 = Interrupt on rising edge
    '               0 = Interrupt on falling edge
    
    INTCON2.4=0
    '  bit 4   INTEDG2: External Interrupt 2 Edge Select bit
    '               1 = Interrupt on rising edge
    '               0 = Interrupt on falling edge
    
    ;--- Setup Registers -------------------------------------------------------
    
    OSCCON = %01100110              ' OSCILLATOR CONTROL REGISTER
    '  bit 7   IDLEN: Idle Enable bit
    '              1 = Device enters Idle mode on SLEEP instruction
    '              0 = Device enters Sleep mode on SLEEP instruction
    '  bit 6-4 IRCF<2:0>: Internal RC Oscillator Frequency Select bits(2)
    '            111 = HFINTOSC – (16 MHz)
    '            110 = HFINTOSC/2 – (8 MHz)
    '            101 = HFINTOSC/4 – (4 MHz)
    '            100 = HFINTOSC/8 – (2 MHz)
    '            011 = HFINTOSC/16 – (1 MHz)(3)
    '       If INTSRC = 0 and MFIOSEL = 0:
    '            010 = HFINTOSC/32 – (500 kHz)
    '            001 = HFINTOSC/64 – (250 kHz)
    '            000 = LFINTOSC – (31.25 kHz)
    '       If INTSRC = 1 and MFIOSEL = 0:
    '            010 = HFINTOSC/32 – (500 kHz)
    '            001 = HFINTOSC/64 – (250 kHz)
    '            000 = HFINTOSC/512 – (31.25 kHz)
    '       If INTSRC = 0 and MFIOSEL = 1:
    '            010 = MFINTOSC – (500 kHz)
    '            001 = MFINTOSC/2 – (250 kHz)
    '            000 = LFINTOSC – (31.25 kHz)
    '       If INTSRC = 1 and MFIOSEL = 1:
    '            010 = MFINTOSC – (500 kHz)
    '            001 = MFINTOSC/2 – (250 kHz)
    '            000 = MFINTOSC/16 – (31.25 kHz)
    '  bit 3   OSTS: Oscillator Start-up Time-out Status bit
    '              1 = Device is running from the clock defined by FOSC<3:0> of the CONFIG1H register
    '              0 = Device is running from the internal oscillator (HFINTOSC, MFINTOSC or LFINTOSC)
    '  bit 2   HFIOFS: HFINTOSC Frequency Stable bit
    '              1 = HFINTOSC frequency is stable
    '              0 = HFINTOSC frequency is not stable
    '  bit 1-0 SCS<1:0>: System Clock Select bit
    '             1x = Internal oscillator block
    '             01 = Secondary (SOSC) oscillator
    '             00 = Primary clock (determined by FOSC<3:0> in CONFIG1H).
    
    OSCCON2 = %00000100              ' OSCILLATOR CONTROL REGISTER 2
    '  bit 7   PLLRDY: PLL Run Status bit
    '               1 = System clock comes from 4xPLL
    '               0 = System clock comes from an oscillator, other than 4xPLL
    '  bit 6   SOSCRUN: SOSC Run Status bit
    '               1 = System clock comes from secondary SOSC
    '               0 = System clock comes from an oscillator, other than SOSC
    '  bit 5   Unimplemented: Read as ‘0’.
    '  bit 4   MFIOSEL: MFINTOSC Select bit
    '               1 = MFINTOSC is used in place of HFINTOSC frequencies of 500 kHz, 250 kHz and 31.25 kHz
    '               0 = MFINTOSC is not used
    '  bit 3   SOSCGO(1): Secondary Oscillator Start Control bit
    '               1 = Secondary oscillator is enabled.
    '               0 = Secondary oscillator is shut off if no other sources are requesting it.
    '  bit 2   PRISD: Primary Oscillator Drive Circuit Shutdown bit
    '               1 = Oscillator drive circuit on
    '               0 = Oscillator drive circuit off (zero power)
    '  bit 1   MFIOFS: MFINTOSC Frequency Stable bit
    '               1 = MFINTOSC is stable
    '               0 = MFINTOSC is not stable
    '  bit 0   LFIOFS: LFINTOSC Frequency Stable bit
    '               1 = LFINTOSC is stable
    '               0 = LFINTOSC is not stable
    
    OSCTUNE = %00000000             ' OSCILLATOR TUNING REGISTER
    '  bit 7   INTSRC: Internal Oscillator Low-Frequency Source Select bit
    '               1 = 31.25 kHz device clock derived from the MFINTOSC or HFINTOSC source
    '               0 = 31.25 kHz device clock derived directly from LFINTOSC internal oscillator
    '  bit 6   PLLEN: Frequency Multiplier 4xPLL for HFINTOSC Enable bit(1)
    '               1 = PLL enabled
    '               0 = PLL disabled
    '  bit 5-0 TUN<5:0>: Frequency Tuning bits – use to adjust MFINTOSC and HFINTOSC frequencies
    '          011111 = Maximum frequency
    '          011110 =
    '          • • •
    '          000001 =
    '          000000 = Oscillator module (HFINTOSC and MFINTOSC) are running at the factory
    '                   calibrated frequency.
    '          111111 =
    '          • • •
    '          100000 = Minimum frequency
    
    PMD0 = %11111111                ' PERIPHERAL MODULE DISABLE REGISTER 0
    '  bit 7   UART2MD: UART2 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    '  bit 6   UART1MD: UART1 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    '  bit 5   TMR6MD: Timer6 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    '  bit 4   TMR5MD: Timer5 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    '  bit 3   TMR4MD: Timer4 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    '  bit 2   TMR3MD: Timer3 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    '  bit 1   TMR2MD: Timer2 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    '  bit 0   TMR1MD: Timer1 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    
    PMD1 = %11111111                ' PERIPHERAL MODULE DISABLE REGISTER 1
    '  bit 7   MSSP2MD: MSSP2 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    '  bit 6   MSSP1MD: MSSP1 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    '  bit 5   Unimplemented: Read as ‘0’
    '  bit 4   CCP5MD: CCP5 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    '  bit 3   CCP4MD: CCP4 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    '  bit 2   CCP3MD: CCP3 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    '  bit 1   CCP2MD: CCP2 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    '  bit 0   CCP1MD: CCP1 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    
    PMD2 = %00001111                ' PERIPHERAL MODULE DISABLE REGISTER 2
    '  bit 7-4 Unimplemented: Read as ‘0’
    '  bit 3   CTMUMD: CTMU Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    '  bit 2   CMP2MD: Comparator C2 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    '  bit 1   CMP1MD: Comparator C1 Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    '  bit 0   ADCMD: ADC Peripheral Module Disable Control bit
    '               1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
    '               0 = Module is enabled, Clock Source is connected, module draws digital power
    
    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 = %00000011                   ' Set port B pins 0,1 to input, others output
    TRISC = %00000100                   ' Set port C pin 2 to input, others output
    
    ;--- Pins ------------------------------------------------------------------
    
    WiperA      VAR PortB.0             ' Input, Interrupt 0
    WiperB      VAR PortC.2             ' Input
    
    Switch      VAR PortB.1             ' Input, Interrupt 1 low priority
    
    LedPower    VAR PortB.5             ' Output
    
    Leds        VAR PortA               ' Output, 8 Leds connected on Port A
    
    ;--- Variables -------------------------------------------------------------
    
    ;--- Constants -------------------------------------------------------------
    
    StateOff con 0                      ' Off
    StateOn  con 1                      ' On
    
    ShiftOne con 1                      ' One
    
    DebounceTime  con 250               ' Debounce time
    StabilizeTime con 200               ' Stabilize time
    
    ;--- Initialize pins -------------------------------------------------------
    
    Ledpower = StateOff                 ' Turn OFF switch Led
    Leds = %00000001                    ' Turn ON first Led
    
    ;--- Initialize variables --------------------------------------------------
    
    ;--- Subroutines -----------------------------------------------------------
    
    goto Start                          ' Jump over sub-routines
    
    ExternalInterrupt0:                 ' [INT0 - interrupt handler]
      if wiperb = StateOn then          ' Check if turning counter-clockwise
        if leds.7 = StateOff then            ' Check if reached last LED
          LEDS = LEDs << ShiftOne       ' Blink Led
        endif
      else                              ' Check if turning clockwise
        if leds.0 = StateOff then            ' Check if reached first LED
          leds = leds >> ShiftOne       ' Blink Led
        endif
      endif
    @ INT_RETURN
    
    ExternalInterrupt1:                 ' [INT1 - interrupt handler]
      Ledpower = StateOn                ' Turn ON switch Led
      pause DebounceTime                ' Basic debounce
    @ INT_RETURN
    
    ;--- Program Start ---------------------------------------------------------
    
    Start:
    
    Pause StabilizeTime                 ' Let PIC stabilize
    
    ;--- The Main Loop ---------------------------------------------------------
    
    mainloop:
    
      Ledpower = StateOff               ' Turn switch Led back OFF
      Goto mainloop
    
    end
    Last edited by Demon; - 4th October 2016 at 17:36.

  4. #84
    Join Date
    Sep 2009
    Posts
    23


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    My own code for reading Rotary Encoder




    Code:
    '****************************************************************
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 16/2/2013                                         *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    TRISA  = %11111    '
    TRISB  = %00000000 '8 LEDs.
    
    PORTB=0            '
    
    x    VAR   Byte  '
    Aux  VAR   Byte  ' 
    Enc  VAR   Byte  '
    
    
    
    x=0                ' 
    Aux=0              
    Enc=0              
    
    
    aaa:                    
        Aux = Enc                         '               
         Enc = (PORTA & 3)                 ' RA0  RA1 2 bits variable 'ENC'.
         If Aux = 2 And Enc = 3 Then x=x+1 ' 
         If Aux = 3 And Enc = 2 Then x=x-1 ' 
         PORTB =  x                         ' 8 LED 
         
        goto aaa
    
    End
    Attached Images Attached Images  

  5. #85
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    How does this work? Since Enc=Aux=0, How can the If clause be true? There must be some pull-ups in PORTA pins.

    Ioannis
    Last edited by Ioannis; - 18th February 2013 at 10:02.

  6. #86
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Here's the initial approach, I've modified for my needs. It does not works, what may be the reason?

    Code:
    LEFT var GPIO.0
    RIGHT var GPIO.1
    COUNTER VAR word
    counter=128
    
    cycl:
    if LEFT=0 then                 'here is switch 1 of the rotary encoder
        counter=counter+1
        goto lcd
    endif
     
    if RIGHT=0 then                 'here is switch 2 of the rotary encoder
        counter=counter-1
        goto lcd
    endif
     
    goto cycl
     
    lcd:
    hpwm 1,counter,5000
    while (LEFT=0 or RIGHT=0):pause 10:wend
    goto cycl
    MCU is PIC12F683

  7. #87
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    well, I think I found the problem - it never treats inputs as inputs, despite a high level on LEFT or RIGHT, it still =0

    Seems like I need some command to configure pins as input?

  8. #88
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    TRISIO=%11111111

    does not helps

  9. #89
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    ANSEL = %00000000
    CMCON0 = 7
    this helped

  10. #90
    Join Date
    Sep 2009
    Posts
    23


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    Quote Originally Posted by Ioannis View Post
    How does this work? Since Enc=Aux=0, How can the If clause be true? There must be some pull-ups in PORTA pins.

    Ioannis

    Sorry for the late reply
    See here and we will resolve the questions

    https://sites.google.com/site/proyec...le-con-16f628a

  11. #91
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

    Default Re: New approach to Rotary Encoder

    It seems very strange that I got an e-mail notification only today about this thread by dovegroup's reply.

    I am not very good at C but from your example, dovegroup, I think the variable x might change more than one at every step in rotating the encoder because of the bouncing effects.

    Ioannis

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