I used mpgmike's suggestion and added a 200ms counter for the LCD display. The read or write (i'm not sure which one) statement still isn't working correctly. I was wrong before when I said it worked when I removed the interrupt. After fiddling with it some more, I realized it doesn't work correctly. So I'm not sure what I'm doing wrong. Hopefully it's something obvious. Here's what's going on:
I'm using a PIC16F887. I have an LCD display connected. There are also 3 incremental rotary encoders connected. Each of the rotary encoders also has a pushbutton switch. I want to display each rotary encoder's "position" of 0 - 255 on the LCD. This is part is working.
I want to be able to save and recall the settings of the rotary encoders. I am using the button on encoder2 as a "save" button. I'm using the button on encoder1 to move backwards through the saved settings and the button on encoder3 to move forward through the saved settings. This is the part that is not working. I'm not sure if it isn't writing correctly or isn't reading correctly...well...I'm sure it's doing what it's supposed to do correctly, I just did something wrong.
Code:
CLEAR
define LOADER_USED 1
define OSC 4
define LCD_DREG PORTD
define LCD_DBIT 0
define LCD_RSREG PORTE
define LCD_RSBIT 0
define LCD_EREG PORTE
define LCD_EBIT 1
define LCD_RWREG PORTE
define LCD_RWBIT 2
define LCD_BITS 4
define LCD_LINES 4
define LCD_COMMANDUS 2000
define LCD_DATAUS 50
ANSEL = %00000000
ANSELH = %00000000
TRISD = %00000000
TRISE = %00000000
TRISB = %11111111
TRISC = %10000000
encoder1_counter var byte
encoder1_LastStateA var bit
encoder1_StateA var bit
encoder2_counter var byte
encoder2_LastStateA var bit
encoder2_StateA var bit
encoder3_counter var byte
encoder3_LastStateA var bit
encoder3_StateA var bit
preset_counter var byte
saved_counter var byte
display_counter var byte
encoder1_pinA var PORTB.7
encoder1_pinB var PORTB.6
encoder2_pinA var PORTB.5
encoder2_pinB var PORTB.4
encoder3_pinA var PORTB.3
encoder3_pinB var PORTB.2
button1 var PORTB.1
button2 var PORTB.0
button3 var PORTC.7
encoder1_preset var byte
encoder2_preset var byte
encoder3_preset var byte
encoder1_counter = 50
encoder2_counter = 50
encoder3_counter = 50
preset_counter = 1
display_counter = 0
saved_counter = 0
encoder1_LastStateA = encoder1_pinA
encoder2_LastStateA = encoder2_pinA
encoder3_LastStateA = encoder3_pinA
pause 500
'=====================MAIN LOOP TO READ INCREMENTAL ROTARY ENCODERS========================
mainloop:
encoder1_StateA = encoder1_pinA ' read first encoder
if encoder1_StateA != encoder1_LastStateA then
if encoder1_pinB != encoder1_StateA then
encoder1_counter = encoder1_counter + 1
else
encoder1_counter = encoder1_counter - 1
endif
encoder1_LastStateA = encoder1_StateA
endif
encoder2_StateA = encoder2_pinA ' read second encoder
if encoder2_StateA != encoder2_LastStateA then
if encoder2_pinB != encoder2_StateA then
encoder2_counter = encoder2_counter + 1
else
encoder2_counter = encoder2_counter - 1
endif
encoder2_LastStateA = encoder2_StateA
endif
encoder3_StateA = encoder3_pinA ' read third encoder
if encoder3_StateA != encoder3_LastStateA then
if encoder3_pinB != encoder3_StateA then
encoder3_counter = encoder3_counter + 1
else
encoder3_counter = encoder3_counter - 1
endif
encoder3_LastStateA = encoder3_StateA
endif
'=====================MAIN LOOP TO RECOGNIZE BUTTON PRESSES==============================
if button1 = 0 then 'when preset down button on encoder1 is pressed
preset_counter = preset_counter - 1
gosub read_preset
pause 25
do until button1 = 1 ' debounce
pause 25
loop
pause 25
endif
if button2 = 0 then 'when save button on encoder2 is pressed
gosub save_preset
pause 25
do until button1 = 1 ' debounce
pause 25
loop
pause 25
endif
if button3 = 0 then 'when preset up button on encoder3 is pressed
preset_counter = preset_counter + 1
gosub read_preset
pause 25
do until button3 = 1 'debounce
pause 25
loop
pause 25
endif
display_counter = display_counter + 1 '200ms delay between LCD updates
pause 1
if display_counter >= 200 then
gosub display
display_counter = 0
endif
goto mainloop
end
read_preset: ' read presets from EEPROM and change values for encoder positions
read preset_counter, encoder1_preset, encoder2_preset, encoder3_preset
pause 25
encoder1_counter = encoder1_preset
encoder2_counter = encoder2_preset
encoder3_counter = encoder3_preset
return
save_preset: 'save encoder positions to EEPROM and display "saved" message
lcdout $fe, 1
lcdout $fe, $c0, " PRESET ", dec2 preset_counter
lcdout $fe, $94, " SAVED"
write preset_counter, encoder1_counter, encoder2_counter, encoder3_counter
pause 1500
return
display: ' LCD display text
lcdout $fe, 1
lcdout $fe, $80, "*****PRESET ", dec2 preset_counter, "******"
lcdout $fe, $94 + 1, "NCDR1"
lcdout $fe, $d4 + , dec encoder1_counter
lcdout $fe, $94 + 8, "NCDR2"
lcdout $fe, $d4 + 9, dec encoder2_counter
lcdout $fe, $94 + 14, "NCDR3"
lcdout $fe, $d4 + 15, dec encoder3_counter
return
Bookmarks