Hi guys,
I've decided to use a quadrature rotary encoder for my next project and the encoder itself will be used to control some settings via a 20x4 LCD. The idea is to have the knob 'scroll' through the available settings (3 for now) and the button to select the setting you want. After pressing the button you access the setting and further knob turns will scroll through the available options for that setting (usually ON or OFF). So it's like a two level menu accessible via encoder.
To handle the encoder I've written a short function which uses a primitive delay method to debounce the contacts:
[NOTE: the function grabs the PORTC value (only bits 0-2 are used for the encoder) and compares it to the last scan value. Tables turn_left, turn_right are not shown, but they hold the next correct values when turning the knob. The right/left/press parts don't hold any code yet of course]
Now the encoder will be turned by hand and obviously if you turn it too fast it will skip a step and not do anything. I'm curious if the 50ms delay for debounce is not going to force very slow turns in order to change the settings. Perhaps someone here tried something similar?
Regards,
dsc.
I've decided to use a quadrature rotary encoder for my next project and the encoder itself will be used to control some settings via a 20x4 LCD. The idea is to have the knob 'scroll' through the available settings (3 for now) and the button to select the setting you want. After pressing the button you access the setting and further knob turns will scroll through the available options for that setting (usually ON or OFF). So it's like a two level menu accessible via encoder.
To handle the encoder I've written a short function which uses a primitive delay method to debounce the contacts:
Code:
[SIZE="2"][FONT="Times New Roman"]; ######################### CHECK ENCODER ##########################
Rot_Enc
movf PORTC, W ;copy PORTC to W
andwf b'00000111',W ;mask bits3-7 so only 0,1,2 are saved (the rotary enc inputs)
movwf PORTC_new ;copy to variable
subwf PORTC_last, W ;do (PORTC_last - PORTC_new) => W
btfsc STATUS, Z ;check zero bit
goto Rot_Enc_end ;values the same because result = 0
btfsc deb_flag ;test debounce flag to see if this is first change detected
goto Rot_Enc_change ;if PORTC is different than last value and again different after 50ms
call Delay50 ;primitive debounce by waiting 50ms
bsf deb_flag, 0 ;set debounce flag
goto Rot_Enc ;after debouncing check port again
Rot_Enc_change
; check if turned right
movlw HIGH turn_right ;preload PCLATH with
movwf PCLATH ;high byte of turn_right table
movf PORTC_last, W ;move PORTC_last to W
call turn_right ;get the next correct value from the table
subwf PORTC_new, W ;do (PORTC_new - next correct value) => W
btfsc STATUS, Z ;check zero bit
goto Rot_Enc_right ;values the same because result = 0
; check if turned left
movlw HIGH turn_left ;preload PCLATH with
movwf PCLATH ;high byte of turn_left table
movf PORTC_last, W ;move PORTC_last to W
call turn_left ;get the next correct value from the table
subwf PORTC_new, W ;do (PORTC_new - next correct value) => W
btfsc STATUS, Z ;check zero bit
goto Rot_Enc_left ;values the same because result = 0
; check if pressed
btfss PORTC_new, 2 ;check bit 2 = knob press
goto Rot_Enc_press ;values the same because result = 0
goto Rot_Enc_update ;if not it means that there was a skip in steps so just update the value
Rot_Enc_right
; knob turned right
goto Rot_Enc_update
Rot_Enc_left
; knob turned left
goto Rot_Enc_update
Rot_Enc_press
; knob pressed
Rot_Enc_update
; update the last scan value
movf PORTC_new, W ;move new value to W
movwf PORTC_last ;update last scan value
Rot_Enc_end
retlw 0x00[/FONT][/SIZE]
[NOTE: the function grabs the PORTC value (only bits 0-2 are used for the encoder) and compares it to the last scan value. Tables turn_left, turn_right are not shown, but they hold the next correct values when turning the knob. The right/left/press parts don't hold any code yet of course]
Now the encoder will be turned by hand and obviously if you turn it too fast it will skip a step and not do anything. I'm curious if the 50ms delay for debounce is not going to force very slow turns in order to change the settings. Perhaps someone here tried something similar?
Regards,
dsc.