DialEncoder

During a wait in an airport lounge I decided to knock up a new module. Its for those rotary encoders used in place of a pot.

These devices work by giving a 2 bit encoder output and requires the CPU to decode the signals to tell you of the direction of rotation. Being able to monitor the movement as a count is one thing but its better if you can do that over time to establish how fast your rotating the dial.

This modules does that, by setting up a 100hz interrupt it will monitor the inputs and when there has been a change it will note the direction and increment or decrement a counter. Then every 2 hz will convert that to a figure that can be used as a speed indication.

How to use

1 Include the module
Include "DialEncoder.Bas"
2 Tell it what the A and B pins are
(Hash)option EncoderA = PORTB.0
(Hash)option EncoderB = PORTB.1
3 Before you use it Initialise the module
IntialiseDialEncoder()
4 Read the current speed and the direction (byte & bit)
RotationSpeed = DialEncoder.Speed
RotationDirection = DialEncoder.Direction

Thats it.

BTW the Code was written for an Grayhill Optical Encoder (Series 62) and debugged/sim'ed all in ISIS by LabCenter Electronics

First is a demo prog followed by the module

    #option EncoderA = PORTB.0
    #option EncoderB = PORTB.1

    Include "Usart.bas"
    Include "convert.bas"
    Include "DialEncoder.bas"
    SetBaudrate(br19200)
    IntialiseDialEncoder()

    Repeat
        USART.Write (DecToStr (DialEncoder.Speed),32,DecToStr (DialEncoder.Direction),13,10)
        DelayMS(500)
    Until 1 = 2    
    End
Module DialEncoder 

    #option EncoderA= PORTB.0   
    #option EncoderB = PORTB.1

    // validate pin...
    #if IsOption(EncoderA) And Not IsValidPortPin(EncoderA) 
       #error EncoderA, "Invalid option. EncoderA must be a valid port pin."
    #endif

    // validate pin...
    #if IsOption(EncoderB) And Not IsValidPortPin(EncoderB) 
       #error EncoderB, "Invalid option. EncoderB must be a valid port pin."
    #endif

    Dim EncoderPinA As [email protected]
    Dim EncoderPinB As [email protected]

    Dim EncoderNew As Byte
    Dim EncoderOld As Byte
    Dim TimerCounter As Byte
    Dim EncoderCounter As Byte
    Public Dim Speed As Byte
    Public Dim Direction As Bit
    Dim Timer1Reg As TMR1L.asWord
    Const FudgeFactor = 5
    Const Tmr1Val =((65536)-(_clock*2500))+FudgeFactor

    interrupt Encoder ()  
        T1CON.0 = 0  
        Timer1Reg = Timer1Reg + Word(Tmr1Val)
        T1CON.0 = 1                                
        EncoderNew.0 = EncoderPinA
        EncoderNew.1 = EncoderPinB        
        If EncoderNew <> EncoderOld Then  
            EncoderOld = EncoderOld >> 1 
            EncoderNew = EncoderNew Xor EncoderOld
            If EncoderNew.0 = 1 Then 
                Inc (EncoderCounter)
            Else 
                Dec (EncoderCounter)
            EndIf
            EncoderOld.0 = EncoderPinA
            EncoderOld.1 = EncoderPinB 
        EndIf
        If TimerCounter > 0 Then
            Dec (TimerCounter)
        Else
            TimerCounter = 49
            If EncoderCounter.7 = 0 Then
               Speed = EncoderCounter
               Direction = 1
            Else
               EncoderCounter.7 = 0     
               Speed = 128 - EncoderCounter
               Direction = 0
            EndIf
            EncoderCounter = 0
        EndIf        
        PIR1.0 = 0     
    End interrupt 


    Public Sub IntialiseDialEncoder()
        EncoderOld = 0
        EncoderNew = 0
        Timer1Reg = Tmr1Val
        T1CON = 0
        PIR1.0 = 0
        INTCON.6 = 1
        PIE1.0 = 1  
        T1CON.0 = 1
        Enable (Encoder)
    End Sub
End