a little help with keypad on 18f4520 please
Hi there
I'm at wit's end ...
I have been battling to get my matrix keypad(s) working, I have both a 4x3 and a 4x4 keypad.
Two nights ago I stunbled across some postings and code in the forums which looked like they may be what I am looking for (with a few changes perhaps).
After trying out both (namely the one from mister e and Steve) I still have not come right and can't understand what I am doing wrong.
Please see my other posts on mister e's keypad code pages here
http://www.picbasic.co.uk/forum/show...?t=3250&page=3
as well as Steve's on this post(hope cross posting is ok):
http://www.picbasic.co.uk/forum/show...0999#post80999.
With Steve's implemetation I get only a "||" character on the LCD.
Attached is my code.
I am almost convinced I am not setting the registers correctly or calling a variable correctly :-( I was quite happy working with a 16f628 but now that I've re-kindled PIC's as hobby again I decided to go with an 18F series and it's taking some getting used to.
If some kind soul would help me I would really appreciate it.
Comments and suggestion or tips and tricks would be more than welcomed too.
Kind regards
Dennis
Code:
'*************************************
'LCD code for 16 X 2 HD4x lcd
'*************************************
'Ocsillator selections here
OSCCON = $70 'Int CLK 8MHz
OSCTUNE.6 = 1 'PLL 4x
ADCON1= %00001111 '$0F = disable A/D converter
'END of oscillator selections
'Port IO directions and presets for port pins begin here
'TRISA = %11111111 'All pins are outputs
'TRISB = %00000000
'for keypad
'// Define port pins as inputs and outputs ...
TRISA = %00001000
TRISB = %00001101
TRISC = %00000000
TRISD = %00000000
TRISE.0 = 0
TRISE.1 = 0
TRISE.2 = 0
'End of Port IO directions and presets for port pins begin here
'timer/oscillator defines
DEFINE OSC 32 '4x 8MHz
'END of timer/oscillator defines
'variables begin here
myvar var byte
'LCD defines begin here
DEFINE LCD_BITS 4 'defines the number of data interface lines (4 or 8)
DEFINE LCD_DREG PORTD 'defines the port where data lines are connected to
DEFINE LCD_DBIT 4 'defines the position of data lines for 4-bit interface (0 or 4)
DEFINE LCD_RSREG PORTD 'defines the port where RS line is connected to
DEFINE LCD_RSBIT 2 'defines the pin where RS line is connected to
DEFINE LCD_EREG PORTD 'defines the port where E line is connected to
DEFINE LCD_EBIT 3 'defines the pin where E line is connected
DEFINE LCD_RWREG 0 'defines the port where R/W line is connected to (set to 0 if not used)
DEFINE LCD_RWBIT 0 'defines the pin where R/W line is connected to (set to 0 if not used)
DEFINE LCD_COMMANDUS 2000 'defines the delay after LCDOUT statement
DEFINE LCD_DATAUS 200 'delay in micro seconds
'END of LCD DEFINES
'keypad code here
'// 3x3 Keypad - Rows as inputs, Cols as outputs...
'Rows: RA3 Cols: RB1
' RB3 RA2
' RB2 RA4
' RB0
'//
' RB5: LED & Buzzer ...
' RA0: TX to PC's Serial Port
'// Declare Variables...
Col_A VAR PORTB.1
Col_B VAR PORTA.2
Col_C VAR PORTA.4
Row_A VAR PORTA.3
Row_B VAR PORTB.3
Row_C VAR PORTB.2
Row_D VAR PORTB.0
Buzzer VAR PORTB.5
RX_To_PC VAR PORTA.0
Scan_Col VAR BYTE ' Counter - denoting current col in scan
Key_Press VAR BYTE ' Contains value of key (0-9) & * + #
Key_Down VAR BYTE ' Flag set true when key is depressed
Allow_Key VAR BYTE ' Flag - disallow multiple keys being pressed
I VAR byte ' General working var
Scan_Keypad:
'// Scan cols
@ incf _Scan_Col, 1 ' Inc col pos...
SELECT CASE Scan_Col ' Col (1-3)
CASE 1
Col_A = 0 ' Switch on col (active low)
Col_B = 1 ' Col off
Col_C = 1 ' Col off
'// 3 Key
IF Row_A = 0 THEN ' Key down? ...
IF Allow_Key = 0 THEN ' Any other key down?
Key_Press = 3 ' Load var w/value of key
Allow_Key = 1 ' Disallow other keys
ENDIF
ENDIF
'// 6 Key
IF Row_B = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 6
Allow_Key = 1
ENDIF
ENDIF
'// 9 Key
IF Row_C = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 9
Allow_Key = 1
ENDIF
ENDIF
'// # Key
IF Row_D = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 35
Allow_Key = 1
ENDIF
ENDIF
CASE 2
Col_A = 1
Col_B = 0
Col_C = 1
'// 2 Key
IF Row_A = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 2
Allow_Key = 1
ENDIF
ENDIF
'// 5 Key
IF Row_B = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 5
Allow_Key = 1
ENDIF
ENDIF
'// 8 Key
IF Row_C = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 8
Allow_Key = 1
ENDIF
ENDIF
'// 0 Key
IF Row_D = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 0
Allow_Key = 1
ENDIF
ENDIF
CASE 3
Col_A = 1
Col_B = 1
Col_C = 0
'// 1 Key
IF Row_A = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 1
Allow_Key = 1
ENDIF
ENDIF
'// 4 Key
IF Row_B = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 4
Allow_Key = 1
ENDIF
ENDIF
'// 7 Key
IF Row_C = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 7
Allow_Key = 1
ENDIF
ENDIF
'// * Key
IF Row_D = 0 THEN
IF Allow_Key = 0 THEN
Key_Press = 42
Allow_Key = 1
ENDIF
ENDIF
end select
'end keypad code
'*********************Includes******
'Includes end here
Pause 500 ' Wait for LCD to startup
'loop1:
' Lcdout $fe, 1 ' Clear LCD screen
' Lcdout "Hello" ' Display Hello
'Pause 1000 ' Wait .5 second
'Lcdout $fe, $C0 ' Clear LCD screen
'Lcdout "World"
'pAUSE 1000
'Lcdout $fe, 1 ' Clear LCD screen
'Pause 1000 ' Wait .5 second
'Goto loop1 ' Do it forever
'start:
'@ READKEYPAD _myvar
'lcdout $fe, 1
lcdout Key_Press
'goto start
not compiling and not sure why ?
Hi Al
How are you ?
I am experimenting with your code snippet .. here's what I have so far.
Code:
'keypad variables for array keypress capture begin here
Retain var byte
Ret_Char var byte [10]
Ret_Count var byte
Ret_Prnt var byte
Ret_Clst var byte
Ret_Prnt = "#"
Ret_Clst = "*"
'keypad capture code begins here
Ret_Char[Ret_Count]=datatx ' here you transfer the char of the key pressed
If Retain = 0 then ' Retain is a flag then if set will do your job
For A0 = 1 to Ret_count -1
Serout portc.6,T2400,baud,[Ret_Char [A0]]
NEXT A0
lcdout datatx 'display the variable on the LCD
pause 1000
lcdout $fe,1 'clear lcd screen
else
If Ret_Char[Ret_Count] = Ret_Clst then ' if key = "*" then step-back counter
Ret_Count = Ret_Count - 1
goto Clear_Count
endif
If Ret_Char[Ret_Count] = Ret_Prnt then ' if key = "#" then send string
For A0 = 1 to Ret_count -1
Serout portc.6,T2400,[Ret_Char [A0]]
NEXT A0
lcdout datatx 'display the variable on the LCD
pause 1000
lcdout $fe,1 'clear lcd screen
RetCount = 0
goto Clear_Count
endif
Ret_Count = Ret_Count + 1
Clear_Count:
endif
goto start
As i try to compile I encounter an error here
For A0 = 1 to Ret_count -1
Not quite sure why ?
Do you think I could attain the same thing with either a case select statement or a do...while loop ?
So if I were using a do ... while (or while wend) for example
do
keep allowing keypress
While
key_press_variable is not equal to '#" 'until we see #
then serout
Kind regards
Dennis