Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

optical encoders?

Status
Not open for further replies.
Mike said:
I guess it's a matter of opinion and preference (grin)...

Why throw expensive hardware at a problem that can be solved with a dozen lines of 'free' code (grin)?

Kind regards, Mike


I didn't state my preference if that's what you had in mind, but you are right, he might be more familiar with the hardware. I did offer alternative to someone who was stuck, and couldn't get his code to work. I guess ASM code writen by someone who is maybe just learning to program is not quite efficient or easy to follow. While 4 states are not much to decode, someone who struggles with a problem, might appreciate alternative solution even if it is more expensive ($0.50...?) and that was the point (no need for grin).

I'm way over my head here.
I can't seem to grasp the concept.

The algorithym is also challenging,
and I've spent many hours getting it right.
I know there is always a better way.

Kind regards,

Panic Mode
 
Mike said:
philba said:
I'd definitely use RBIF but timer driven sampling should work as well. You can pretty easily estimate the speed of of the quad encoder pulses. 1mS might be a bit slow as you should be sampling at least 2X the max rate.
He'd have to spin the knob on his 25-pulse/revolution encoder more than 40 times per second (2400 rpm) to miss a pulse when polling with 1-msec interrupts...

Kind regards, Mike

Uh, it's some sort of thumb pressure gizmo. I think he can get instaneous acceleration of a lot more than you are thinking.

With your example, the limit is 10 RPS for a 1khz (1 mS) sample rate, by the way.

Let's do the math. 25 ppr = 100 edges per rev in a 90 degree phase relationship. depending on his gearing/linkage from the lever device to the encoder disk he could see a pretty fast pulse rate. How fast can you move your thumb? Remember, its about max instantaneous rate, not steady state. If you guess too low, you get errors (invalid grey code transitions). I still think 1K may be a bit low but it's easy enough to determine. The good news is timer interrupt overhead for this is tiny so the penalty for sampling too often is pretty low.
 
Finally, I had time to play with this.

Please don't be to critical, as I'm relatively new at Pic programming.
Remember, I'm trying to increase DC motor speed via an optical encoder which is more or less fashioned like a gas pedal in a car. When you apply pressure to accelerator speed goes up, when you let off accel, speed decrease.

Here is my code.
Code:
;16F628 ENCODER READING
;
;DEVICE 16F628
      LIST P=16F628,
      #INCLUDE P16F628.inc
      



RP1                          EQU     0x06       
RP0                          EQU     0x05        
PORTA                        EQU     0x05        
PORTB                        EQU     0x06        
STATUS                       EQU     0x03        
TRISA                        EQU     0x85        
TRISB                        EQU     0x86        
SPEED                        EQU     0x30        ; variable used for context saving
COUNT1                       EQU     0x32        ; variable used for context saving 
COUNT2                       EQU     0x33        ; variable used for context saving
COUNT3                       EQU     0x34        ; variable used for context saving
COUNT4                       EQU     0x35        ; variable used for context saving
COUNT5                       EQU     0x36        ; variable used for context saving
COUNT6                       EQU     0x37        ; variable used for context saving 
_ENCA                        EQU     0x53        ; variable used for context saving
_ENCB                        EQU     0x54        ; variable used for context saving
PC                           EQU     0x02


   

     

;PORT SETUP

SETUP    
     BSF STATUS, RP0
     MOVLW 31H       ;PORTA RB0-RB4 INPUT
     MOVWF TRISA
     MOVLW 00H
     MOVWF TRISB     ;PORTB ALL OUTPUTS
     BCF STATUS, RP1
     movlw 0x60
     movwf 0X32 ;COUNT1
     movlw 0X60
     movwf 0X33 ;COUNT2
     movlw 0x99
     movwf 0X34 ;COUNT3
     movlw 0X99
     movwf 0X35 ;COUNT4
     movlw 0x10
     movwf 0X36 ;COUNT5
     movlw 0X10
     movwf 0X37 ;COUNT6
     CLRW
     CLRF PORTA
     CLRF PORTB
     
    
   
    
MAIN
    
    BTFSC PORTA, 0  ;BRAKES RA0
    GOTO  LED1
    NOP
    BTFSS PORTA, 1  ;SAFETY SWITCH UNDER SEAT RA1
    GOTO  LED2
    NOP
    BTFSC PORTA, 2  ;REVERSE RA2
    GOTO  REVERSE
    GOTO  START
    
    
    
START
    BTFSS PORTA,3; test file RA3 'A' pin of encoder. 
    XORWF _ENCA ; compare if same with _ENCA
    BTFSS STATUS,2 ;check Z flag see if set or not
    GOTO SPEED_FWD 
    GOTO NEXT 
    
NEXT 
    BTFSS PORTA,4 ; test file RA4'B' pin of encoder. 
    XORWF _ENCB ; again compare with _ENCB 
    BTFSS STATUS,2 ;again test Z flag
    GOTO SPEED_RVRS
    GOTO MAIN   
       
SPEED_FWD
    INCF SPEED,1 ;increment file SPEED and put result back in SPEED register
    MOVF SPEED,0 ;move to W register
    CALL TABLE
    MOVWF PORTB
    GOTO MAIN
      
SPEED_RVRS
    DECF SPEED,1 ; decrement file SPEED and place result back in SPEED register.
    MOVF SPEED,0  ;move to W register
    CALL TABLE
    MOVWF PORTB
    GOTO MAIN
    
; ALARM Subroutines

        
DELAY
     
LOOP1  DECFSZ COUNT1,1 
       GOTO LOOP1
       DECFSZ COUNT2,1
       GOTO LOOP1
       GOTO MAIN

DELAY1
     
LOOP2  DECFSZ  COUNT3,1
       GOTO LOOP2
       DECFSZ COUNT4,1
       GOTO LOOP2
       GOTO MAIN
       
DELAY2
     
LOOP3  DECFSZ COUNT5,1
       GOTO LOOP3
       DECFSZ COUNT6,1
       GOTO LOOP3
       RETURN
       
       
             

LED1
     MOVLW 0x01
     MOVWF PORTB ;RB0
     CALL DELAY
     MOVLW 0x00
     MOVWF PORTB ;RB0
     CALL DELAY
     BTFSC PORTA,0
     GOTO LED1
     RETURN
     
LED2
     MOVLW 0x02
     MOVWF PORTA ;RB1
     CALL DELAY1
     MOVLW 0x00
     MOVWF PORTB ;RB1
     CALL DELAY1
     BTFSC PORTA, 1
     GOTO LED2
     RETURN 
     
REVERSE
     
     BSF PORTA,3
     GOTO MAIN
     
     
     
     
TABLE
     ADDWF PC
     retlw 0x01
     retlw 0x02
     retlw 0x04
     retlw 0x08
     retlw 0x10
     retlw 0x20
     retlw 0x40
     retlw 0x80
     return
      

     END

Please, just verify, and critisize as needed. All constructive critisism will be welcome.
Code is not complete, just need to weed out the bad.

Thank You.
 
hai nice to meet u all..
i'm a newbie

i've try to measuring how long my robot have walking arround with mouse encoder too..i use an ATMEL microcontroller as a proccessor...

n this is my source:

cek:
;to check first condition of encoder before
;it has been turning
clr a
mov a,p2
anl a,#00110000b
mov R4,a

kanan2:
lcall delay10
clr a
mov a,p2
anl a,#00110000b
cjne R4,#00110000b,satu
cjne a,#00110000b,cek03
mov r4,a
ljmp kanan2
cek03:
cjne a,#00100000b,cek02
ljmp emaka
cek02:
cjne a,#00010000b,sdtjx
ljmp emuka
satu:
cjne R4,#00100000b,dua
cjne a,#00100000b,cek13
mov r4,a
ljmp kanan2
cek13:
cjne a,#00000000b,cek12
ljmp emaka
cek12:
cjne a,#00110000b,sdtjx
ljmp emuka
dua:
cjne R4,#00000000b,tiga
cjne a,#00000000b,cek23
mov r4,a
ljmp kanan2
cek23:
cjne a,#00010000b,cek22
ljmp emaka
cek22:
cjne a,#00100000b,sdtjx
ljmp emuka
tiga:
cjne R4,#00010000b,kanan2
cjne a,#00010000b,cek33
mov r4,a
ljmp kanan2
cek33:
cjne a,#00110000b,cek32
ljmp emaka
cek32:
cjne a,#00000000b,sdtjx
ljmp emuka

sdtjx:
ljmp kanan2

emaka:
mov R4,a
clr a
inc data5
mov p1,data5
clr a
inc data1
mov p3,data1
mov a,data4
cjne a,#1,trus
dec 40h
mov a,40h
cjne a,#0,sdtjx
mov data4,#0
mov 40h,#41
ljmp kanan2
trus:
clr a
mov a,data5
cjne a,#108,sdtjx
mov data5,#0
;mov p1,data5
ljmp kanan2
cekj:
mov a,data1
cjne a,#0ffH,sdtjx
inc data3
mov data1,#0
mov a,data3
cjne a,#4,sdtjx
mov data4,#1
mov data3,#0
ljmp kanan2

emuka:
mov R4,a
dec data1
mov p3,data1
mov p3,#11110000b
inc data2
mov a,data2
cjne a,#54,sdtjx
mov data2,#0
ljmp kanan2

this source is working..
but may i know where you get datasheet of that mouse encoder??
the mouse that i use have 48 step/revolution..
i need that datasheet to get sure that my source algoritm is already right..
thx for you all..
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top