Menu

Having trouble learning how to use the ec11 encoder

2025-05-28
2025-06-02
  • Gert Klemmensen

    Gert Klemmensen - 2025-05-28

    Hi,

    I am trying to understand how to use an ec11 encoder with push button function.
    I have used the demo software and adapted it to my display.

    It works fine with turning the encoder. But when I press the push button function nothing happens unless I turn the button at the same time. So I have to hold the button down while turning.

    I expected that just pressing the push button is what switches between Count enable and Counting disabled in the program.

    What I would like to learn is how to jump to a menu by pressing the push button, change and save a value in a variable, and then jump back to the main program by pressing the push button again.

    How do I do that?

    '''A demonstration program for GCBASIC.
    '''--------------------------------------------------------------------------------------------------------------------------------
    ''' This program:
    ''' Mechanical Rotary Encoder-ALPS EC11-w_o-Interrupts_LCD_18F2520.gcb
    ''' demonstrates the use of mechanical rotary encoder
    ''' without using interrupts
    ''' with the ALPS-EC11.h include file.
    '''
    '''@author  Immo, DL5KB
    '''@licence GPL
    '''@version 1.0b
    '''@date    Dec. 2019
    ''' *****************************************
    ''' Program: Reads a rotary encoder ALPS EC11
    '''
    ''' Turning the knob counts a number up or down in single steps,
    ''' corresponding to turning sense.
    ''' Pushing encoder switch, disables counting.
    ''' Pushing encoder switch again, enables counting again.
    ''' Encoder works to Vss (0V), ports are pulled up internal.
    ''' LCD is 2x16 connected as 4 Wire Data (LCD_IO 4).
    ''' *****************************************
    ''' CHANGE CHIP AND PORTS ACCORDING TO YOUR TESTING BOARD!
    ''' *****************************************
    
     ; ---- configuration
     #chip 16F18877
    
     'load encoder header file
     #include <alps-ec11.h>
    
     #include <glcd.h>
    
      #startup InitPPS, 85
      #define PPSToolPart 16F18877
    
        Sub InitPPS
    
                // Module: MSSP1
                //SSP1DATPPS = 0x0014    // RC4 > SDA1
               //RC4PPS = 0x0015    // SDA1 > RC4 (bi-directional)
                //RC3PPS = 0x0014    // SCL1 > RC3
                //SSP1CLKPPS = 0x0013    // RC3 > SCL1 (bi-directional)
                SSP1DATPPS = 0x0013    // RC3 > SDA1
                RC3PPS = 0x0015    // SDA1 > RC3 (bi-directional)
                RC2PPS = 0x0014    // SCL1 > RC2
                SSP1CLKPPS = 0x0012    // RC2 > SCL1 (bi-directional)
    
            End Sub
    
    
        ;ANSELC = 0x00
    
      ; ----- Define I2C Hardware settings
        #define HI2C_BAUD_RATE 400
        #define HI2C_DATA PORTC.3
        #define HI2C_CLOCK PORTC.2
      ;  Initialise I2C Master
        Dir HI2C_DATA in
        Dir HI2C_CLOCK in
      ;  MASTER
        HI2CMode Master
    
      ; ----- Define GLCD Hardware settings
        #define GLCD_TYPE GLCD_TYPE_SH1106
        #define GLCD_I2C_Address 0x78
    
      ; ----- Define ports I/O
        Dir PORTA.0 In
        Dir PORTA.1 In
        Dir PORTA.2 In
        Dir PORTA.3 In
        Dir PORTA.4 In
        Dir PORTA.5 In
        Dir PORTB.0 In
        Dir PORTB.1 In 
        Dir PORTB.2 In 
        Dir PORTB.3 In 
        Dir PORTD.0 Out
        Dir PORTD.1 Out
    
      ; ----- Name PORTS
    
        #DEFINE RELAY1 PORTD.0
        #DEFINE RELAY2 PORTD.1
    
    
    
    
    
     ;set weak pullups on inputs port B
     WPUB = 0xFF
    
    
    
     ; ---- LCD-Settings
    
    
     'encoder connections defined here
     #define ALPS_ENCODER_A PortB.2  'clk
    
     #define ALPS_ENCODER_B PortB.0  'cw
    
     #define ALPS_ENCODER_SW PortB.3 'encoder-switch
    '''--------------------------------------------
    '''-------End of board-specific settings-------
    '''--------------------------------------------
    
     ; ---- variables
     'value to be displayed
     DIM result as integer
    
     ; ---- main body of program begins here
     main:
     'StartTimer-value to count
       result = 0
     'switch not pressed
       Alps_ESB_Bit = OFF
    
      GLCDPrint(15, 5, "INVERTED TEXT", 0)
      GLCDPrint(15, 20, "NORMAL TEXT", 1)
      wait 5 s  
      GLCDCLS
      GLCDPrint(1, 0 , "Rotary Enc. Demo", 0)
      GLCDPrint(1, 10 , "GCBASIC", 0)
      wait 2 s
      GLCDCLS
      GLCDPrint(1, 0 , "Dec 2023")
      GLCDPrint(1, 10 , "Immo's&Ano Code")
      wait 2 s
      GLCDCLS
      GLCDBackground = 0
      GLCDForeground = 1
    
     'Show what happening with encoder on the LCD
    
    
      GLCDPrint(1, 0 , "Counting enabled")
    
      GLCDPrint(1, 5 , "Rotate encoder  ")
    
     'now begin working...
    count:
    
     'get encoder values (from include File)
     'returns only, when encoder is turned
     'or encoder_sw has been pressed (Alps_ESB_Bit = 1)
     if ! Alps_ESB_Bit then Alps_Encoder_Read
     '--------------------------
     'here you can insert any stuff you want
     '
     '
     'to get back to counting, press button again
     '--------------------------
     'switch was pressed to escape reading loop
     if Alps_ESB_Bit = 1 then
       ' show me
    
       GLCDPrint(1, 0 , " count disabled ")
       GLCDPrint(1, 5 , "  press button  ")
       GLCDPrint (5, 40, Alps_ESB_Bit)
    
      'toggle Alps_ESB_Bit, if you want/need
       Alps_Switch_Read
    
    
        if ! Alps_ESB_Bit Then
         'switch was pressed again
         'show me old display
         GLCDCLS
         GLCDPrint(1, 0 , "Counting enabled")
         GLCDPrint(1, 10 , "                ")
         if result > -1 Then
         GLCDPrint(1, 20 , " ")
         end if
    
         GLCDPrint(5, 20 , result)
         GLCDPrint (5, 40, Alps_ESB_Bit)
    
        end if
      'start work - loop counting
      goto count
    
     end if
    
     'show detected direction of turning
    
       if Alps_directN = 1 then
       'if enc_directN = 1 then
         GLCDPrint(45, 20 , " CW (+) ")
       Else
         GLCDPrint(45, 20 , "CCW (-) ")
       end if
    
     'make new result
       result = result + Alps_Count_delta
    
     'show me at same place as negatives
       if result > -1 Then
        GLCDPrint(1, 30 , " ")
       end if
    
       GLCDPrint(5, 30 , result)
    
       GLCDPrint (5, 40, Alps_ESB_Bit)
    
    
     goto count
    
     end
    
     
  • Geoffrey Younger

    Hi Gert,
    You might be chasing a red herring. Some manufacturers of the ECS11 do not bring out the switch connection to the pcb. You need to check this. You can activate the switch, but nothing happens.
    Keyes model KY-040 has the switch available.
    Cheers,
    Geoffrey Younger

     
    • Gert Klemmensen

      Gert Klemmensen - 2025-06-02

      Hi Geoffrey,

      Thank you for your reply.

      I have checked the pin for the switch, and it work as expected.

      Cheers
      Gert

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.