Hi All,
As the title says, i need some input for my 4x3 keypad routine.
I started putting together a code routine to read a 4x3 (12 button) keypad, but i'm having trouble working out how i can store up to 3 key-presses and how to keep track of how many key-presses were detected....
Overall, i want to to enter a single digit, or up to 3 digits, where each keypress (data) is then stored into 3 locations, such as b10, b11, b12 variables for 1's, 10's and 100's key press values.
So, pressing "123" would write to those 3 variables etc.
Based on 3 stored NIBBLES, i would then assign the Nibble data to output pins to light up (upto 12) LEDs etc.
I'm not strict about any changes to my simple code, i know many people out there could reduce my code to 10 or less lines lol.
As the title says, i need some input for my 4x3 keypad routine.
I started putting together a code routine to read a 4x3 (12 button) keypad, but i'm having trouble working out how i can store up to 3 key-presses and how to keep track of how many key-presses were detected....
Overall, i want to to enter a single digit, or up to 3 digits, where each keypress (data) is then stored into 3 locations, such as b10, b11, b12 variables for 1's, 10's and 100's key press values.
So, pressing "123" would write to those 3 variables etc.
Based on 3 stored NIBBLES, i would then assign the Nibble data to output pins to light up (upto 12) LEDs etc.
I'm not strict about any changes to my simple code, i know many people out there could reduce my code to 10 or less lines lol.
Code:
#PICAXE 28X2
#NO_DATA
#NO_TABLE
#Terminal 9600
;### 4x3 KEYPAD scanning trial
; Define Keypad I/O pins
symbol row1 = C.0 ;INPUT row, Keypad Pin #2.
symbol row2 = C.1 ;INPUT row, Keypad Pin #7.
symbol row3 = C.2 ;INPUT row, Keypad Pin #6.
symbol row4 = C.5 ;INPUT row, Keypad Pin #4.
symbol col1 = A.1 ;OUTPUT column, Keypad Pin #3.
symbol col2 = A.2 ;OUTPUT column, Keypad Pin #1.
symbol col3 = A.3 ;OUTPUT column, Keypad Pin #5.
; Define variables
symbol key_pressed = b9
symbol _1s = b10
symbol _10s = b11
symbol _100s = b12
dirsA = %11111111 ;Set A.0-A.7 to OUTPUTS(1)
dirsB = %11111111 ;Set B.0-B.7 to OUTPUTS(1)
dirsC = %00011000 ;Set C.0-C.7 to INPUTS(0), EXCEPT C.3 & C.4 reserved for i2c for later use.
;---------------------------------------------------------------------
; Initialize
low col1 ;Turn ALL 3 COLUMNS "OFF" (0 value).
low col2
low col3
main:
; Start Scanning the keypad for a button-press.
high col1 ;Ready to read Keypress on 1,4,7 or "*".
low col2 ;Turn OFF Column 2.
low col3 ;Turn OFF Column 3.
if pinC.0 = 1 then
key_pressed = "1"
goto key_pressed_detected
endif
if pinC.1 = 1 then
key_pressed = "4"
goto key_pressed_detected
endif
if pinC.2 = 1 then
key_pressed = "7"
goto key_pressed_detected
endif
if pinC.5 = 1 then
key_pressed = "*"
goto key_pressed_detected
endif
;---------------------------------------------------------------------
low col1 ;Turn OFF Column 1.
high col2 ;Ready to read Keypress on 2,5,8 or 0.
low col3 ;Turn OFF Column 3.
if pinC.0 = 1 then
key_pressed = "2"
goto key_pressed_detected
endif
if pinC.1 = 1 then
key_pressed = "5"
goto key_pressed_detected
endif
if pinC.2 = 1 then
key_pressed = "8"
goto key_pressed_detected
endif
if pinC.5 = 1 then
key_pressed = "0"
goto key_pressed_detected
endif
;---------------------------------------------------------------------
low col1 ;Turn OFF Column 1.
low col2 ;Turn OFF Column 2.
high col3 ;Ready to read Keypress on 3,6,9 or "#".
if pinC.0 = 1 then
key_pressed = "3"
goto key_pressed_detected
endif
if pinC.1 = 1 then
key_pressed = "6"
goto key_pressed_detected
endif
if pinC.2 = 1 then
key_pressed = "9"
goto key_pressed_detected
endif
if pinC.5 = 1 then
key_pressed = "#"
goto key_pressed_detected
endif
;---------------------------------------------------------------------
; Key press detected
key_pressed_detected:
pause 100 ;Debouncing delay
SerTxd( key_pressed, CR, LF ) ;/send Key_pressed data to serial window to confirm values are correct.
key_pressed = "" ;Set Key_pressed value to blank, in between key presses that are detected.
goto main ;goto beginning to look for next key-press.
Last edited: