Reading incremental encoder


Closed Thread
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Reading incremental encoder

    Quote Originally Posted by HenrikOlsson View Post
    You have limited time to work with the hardware and you don't have time to even compile the code to see if there are any syntax errors

    /Henrik.
    You beat me to it...

  2. #2
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Reading incremental encoder

    Code:
    'LCD
    disp_lcd
    serout2 LCD_pin, N9600, [$FE, 1$] 'clear display
    pause 1
    serout2 LCD_pin, N9600, [$FE, 2$] 'cursor returns home
    pause 1
    serout LCD_pin, N9600, ["TellerC= ", DEC Counter]
    pause 1
    SEROUT2 LCD_pin,N9600, [$FE, $C0] 'cursor to 2nd line
    pause 1
    SEROUT2 LCD,N9600, ["TellerCC= ", dec CounterCC]
    return
    OK you may of got some syntax wrong, but at least check for obvious omissions first !

  3. #3
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Reading incremental encoder

    You also need to set the PIC registers and to get reliable 9600 baud rate you need to ensure the PIC is running at 4Mhz as serial out assumes a 4mhz timing base

    From the BPB manual

    SEROUT2 assumes a 4MHz oscillator when generating its bit timing. To maintain the proper baud rate timing rate with other oscillator values, be sure to DEFINE the OSC setting to the new Oscillator value. An oscillator speed faster than 4MHz may be required for reliable operation at 9600 baud and above
    You need to read section 3.5.2 of the datasheet on how to configure the internal oscillator HFINTOSC which is factory calibrated to 8Mhz, The frequency of the HFINTOSC can be altered via software using the OSCTUNE register (see the Register section 3-2).

    The point of mentioning this is that you really need to spend time developing the hardware as well as the software... the two go hand in hand.

    It may seem that we've come down hard on you, but then you must appreciate that the guys here see 100's of posts from new members who ask for help, but don't want to put the effort in from the start. The guys here (Henrik, Tabsoft and Richard to name a few) are very helpful and have lots of coding experience, and it's only fair to have done the ground work first, and then they are only too pleased to help (and I'm talking from experience here)

  4. #4
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Reading incremental encoder

    Ok this compiles... but it won't work as there are lots missing from the code, such as the "afterbranch" subroutine hasn't been included (I just gave it a label and return) I've also added some config settings, and a few missing variables...

    Code:
    '************************************************* ***************
    '* Name : RotaryEncoder.BAS *
    '* Author : Robbe Degezelle *
    '* Notice : Copyright (c) 2016 [select VIEW...EDITOR OPTIONS] *
    '* : All Rights Reserved *
    '* Date : 12/04/2016 *
    '* Version : 1.0 *
    '* Notes : *
    '* : *
    '************************************************* ***************
    
    
    'initialisation------
    
    
    @ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BOD_ON 
              ' Internal Oscillator
              ' Enable watch dog timer
              ' Enable power up timer
              ' Disable MCLR pin
              ' Enable brown out detect
    
    
    ANSEL = 0      ' Set all digital
    CMCON0 = 7     ' Analog comparators off
    
    LCD_pin var GPIO.1 
    
    'variables-----------
    NewVal VAR word 
    OldVal VAR word
    Counter VAR word
    CounterCC var word
    word1 var word 'C
    word2 var word 'CC
    
    CurVal var word '  This seemed to be missing from your original code... again I presume it should be a word variable as you have statements like OldVal = CurVal
    
    P4800 CON 188 '4800 baud true
    N9600 con $4054 '9600 baud rate inverted
    
    word2=000
    NewVal = (GPIO & 000) >> 3
    OldVal = NewVal
    Counter = 000
    CounterCC = 500 
    word1=000
    
    'Program----------------
    Main:
    NewVal = (GPIO & 000) >> 3 ; zou 0,1,2 of 3 moeten geven
    If NewVal <> OldVal Then
    Branch CurVal, [S0,S1,S2,S3] ; indien waarde veranderd, vergelijken met oud
    EndIF
    goto AfterBranch
    Goto Main
    
    
    S0:
    If OldVal = 2 Then
    GoSub Clockwise
    ELSE
    GoSub CounterClockwise
    EndIF
    Goto AfterBranch
    
    S1:
    if OldVal = 0 Then
    GoSub Clockwise
    ELSE
    GoSub CounterClockwise
    EndIF
    Goto AfterBranch
    
    S2:
    if OldVal = 3 Then
    GoSub Clockwise
    Else
    GoSub CounterClockwise
    EndIf
    Goto AfterBranch
    
    S3:
    if OldVal = 1 Then
    GoSub Clockwise
    Else
    GoSUb CounterClockwise
    Endif
    Goto AfterBranch
    
    
    
    Clockwise:
    OldVal = CurVal
    Counter = Counter + 1 
    word1 = Counter
    serout2 GPIO.0,p4800, ["B1", DEC3 word1]
    gosub disp_lcd 
    Return 
    
    CounterClockwise:
    OldVal = CurVal
    CounterCC = CounterCC + 1
    word2 = Countercc
    serout2 GPIO.0,p4800, ["B1", DEC3 word2] 
    gosub disp_lcd
    return
    
    
    'LCD
    disp_lcd:
    serout2 LCD_pin, N9600, [$FE, $1] 'clear display
    pause 1
    serout2 LCD_pin, N9600, [$FE, $2] 'cursor returns home
    pause 1
    serout2 LCD_pin, N9600, ["TellerC= ", DEC Counter]
    pause 1
    SEROUT2 LCD_pin,N9600, [$FE, $C0] 'cursor to 2nd line
    pause 1
    SEROUT2 LCD_pin,N9600, ["TellerCC= ", dec CounterCC] ' corrected this line
    return
    
    
    AfterBranch:             ' no idea what afterbranch is meant to do, as you have several goto's afterbranch but there is no such subroutine
    Return
    There may be other coding errors, but the syntax seems OK and like I said this now compiles, but until you add the missing subroutines, and take on board some of the comments Henrik has mentioned it won't work. Also, your post seemed to have good command of the English language, but some of the comments (hi-lighted in green) are in a different language, which makes me wonder if the code is your own work ?

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Reading incremental encoder

    The most efficient method for an encoder that I've encountered is a "current state/next state" combination of 4 bits that you can use for a lookup function. The result of the lookup defines the action, where a result of 1 could be "increment", 2 could be "decrement", 0 could be "do nothing/not valid". Super slick, minimal code space. Don't forget to allow for debouncing.
    Last edited by picster; - 4th May 2016 at 14:50. Reason: additional note

Similar Threads

  1. Reading incremental encoder interupt
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 1st March 2017, 12:50
  2. Using an incremental rotary encoder?
    By keithv in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th January 2016, 22:23
  3. Encoder velocity....
    By thasatelliteguy in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 21st April 2014, 00:33
  4. Rentron Encoder
    By lerameur in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 27th June 2013, 07:28
  5. encoder wowes
    By wallaby in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 6th December 2005, 21:56

Members who have read this thread : 0

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