rotary encoder code debugger

Discussion in 'Embedded Systems and Microcontrollers' started by a_sorien, May 3, 2015.

  1. a_sorien

    Thread Starter New Member

    May 23, 2009
    3
    0
    Hello
    I am new here , my english is very bad , but somehow maybe i will make me understood .
    So my problem is to decode a rotary encoder and set two outputs : one for cCW and the other to CCW .
    I use an pic16f628 , and RA0, RA1 is for inputs encoder with two resistors 10k up to +5v .
    RB0 , RB1 i am used for output CW and CCW . I dont need precision so if apeear some slipage , ..it s ok .
     
  2. a_sorien

    Thread Starter New Member

    May 23, 2009
    3
    0
    I use a TOKY encoder with 300 pulses/rotation . The algorithm must decode inpust from encoder and if encoder not move , the outputs must stay clear .

    [​IMG]


    CODE :



    list p=16f628A

    #include <p16F628A.inc>

    errorlevel -302

    __CONFIG _CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTOSC_OSC_NOCLKOUT








    ORG 0x000
    goto setup


    setup ; init PIC16F628A

    banksel CMCON
    movlw 0x07 ; turn offcomparatoRS
    movwf CMCON
    bsf STATUS,RP0
    movlw b'00000011' ; RA0, RA1 input
    movwf TRISA
    clrf TRISB
    bcf STATUS,RP0 ; bank 0
    clrf PORTA
    clrf PORTB

    encoder
    clrf PORTB ; this make me STOP (no movements encoder)
    btfss PORTA,RA0 ; poll if it some movement RA0
    goto $+2
    goto encoder
    btfss PORTA,RA1 ; if it is movement to RA1 then must decode
    goto orar
    goto antiorar


    orar
    movlw b'00000001' ; set to CW
    movwf PORTB
    btfss PORTA,RA0 ; if it is still some movement
    goto $+2 ; if YES poll RA1
    goto encoder ; if NOT then goto STOP
    btfss PORTA,RA1 ; i detect some movement so must poll to see it is the same direction
    goto orar ; it is the same so keep going the same
    goto encoder ;another direction detect so jump
    antiorar

    movlw b'00000010' ; set CCW movement
    movwf PORTB
    btfss PORTA,RA0 ; if it still movement
    goto $+2 ; if YES poll RA1
    goto encoder ; if NOT go to STOP
    btfss PORTA,RA1 ; poll RA1 to see direction
    goto encoder
    goto antiorar


    end


    P.S. I make presumtion if RA0 change state , encoder have movment , if not encoder have no movment and PORTB must cleared
     
  3. a_sorien

    Thread Starter New Member

    May 23, 2009
    3
    0
    sory about formatting the text , but the follows labels are: setup , encoder , orar , antiorar .
    orar = CW , antiorar = CCW in my language
     
  4. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,510
    2,369
    The CCP module would be an optimal way of capturing this if either every rising edge or the rising/falling edge of RA0 is captured and used with TMR1, the direction would be simply the state of RA1 when the rising edge occurs.
    Max.
     
  5. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,675
    2,722
Loading...