
Originally Posted by
HenrikOlsson
Very good, then there's plenty of margin.
Next challenge: Velocity sensing, ie a way to detect that the user wants the value to change by a lot and not have to turn the knob 600 turns. Getting that just right seems to be tricky, even the big boys don't always... (I've never tried).
I'm in the middle of a project requiring a rotary encoder to set a weight target for a loadcell. I plan on using Richard's graphical LCD driver for a dot matrix display (not superfast update refreshing), so I actually needed something like what you've described there Henrik. Using Demon's code, I twiddled it a bit to utilise (elapsed) Timer0 on an 18F26K22 to provide the screen refreshing. This is what I basically came up with (which I'm happy with):
Code:
'REMOVED ALL THE INIALIZATION STUFF FOR THIS PASTING
Enc1_counter = 1000
SETFONT FONT5x7
'SETFONT bignum5
LATC.4 = 0 'using a pic pin output as a temporary ground rail for the rotary encoder
gosub grf_clr
T0CON.7 = 1 'enable timer 0
T0CON.6 = 0 'timer 0 in 16 bit mode
T0CON.5 = 0 'timer 0 clock source = internal instruction clock
'T0CON.4
T0CON.3 = 0 'timer prescaler assigned
T0CON.2 = 0 ' prescaler assigned to 1:4
T0CON.1 = 0 ' "
T0CON.0 = 1 ' "
Timer_Counter = 0
Change = 0
MainLoop:
Timer_Counter.lowbyte = TMR0L
Timer_Counter.highbyte = TMR0H
New_Encoder = (PortB.4 << 1) + PortB.5
IF New_Encoder != Old_Encoder THEN
IF New_Encoder = %00000011 THEN
IF Old_Encoder = %00000010 THEN
Change = Change+1
Enc1_counter = Enc1_counter + Change
ELSE
IF Old_Encoder = %00000001 THEN
Change = Change+1
Enc1_counter = Enc1_counter - Change
ENDIF
ENDIF
ELSE
IF New_Encoder = %00000000 THEN
IF Old_Encoder = %00000001 THEN
Change = Change+1
Enc1_counter = Enc1_counter + Change
ELSE
IF Old_Encoder = %00000010 THEN
Change = Change+1
Enc1_counter = Enc1_counter - Change
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
if change >= 1 and Timer_Counter>60000 then
TMR0H=0
TMR0L=0
ARRAYWRITE BUFF,[sdec Enc1_counter,0]
gosub grf_clr
'DMDSTR 10,10, BUFFp,1
DMDSTR 10,10, BUFF,1
gosub show
Change = 0
else
pause 5
endif
Old_Encoder = New_Encoder
goto MainLoop:
Troy
Bookmarks